Skip to content

Commit c9561c1

Browse files
author
Josh Hoyt
committed
[project @ Added documentation and API for controlling consumer association negotiation]
1 parent 37393c2 commit c9561c1

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎openid/consumer/consumer.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@
189189
from openid import cryptutil
190190
from openid import kvform
191191
from openid import oidutil
192-
from openid.association import Association, default_negotiator
192+
from openid.association import Association, default_negotiator, \
193+
SessionNegotiator
193194
from openid.dh import DiffieHellman
194195
from openid.store.nonce import mkNonce, split as splitNonce
195196
from openid.yadis.manager import Discovery
@@ -370,6 +371,28 @@ def complete(self, query, return_to=None):
370371

371372
return response
372373

374+
def setAssociationPreference(self, association_preferences):
375+
"""Set the order in which association types/sessions should be
376+
attempted. For instance, to only allow HMAC-SHA256
377+
associations created with a DH-SHA256 association session:
378+
379+
>>> consumer.setAssociationPreference([('HMAC-SHA256', 'DH-SHA256')])
380+
381+
Any association type/association type pair that is not in this
382+
list will not be attempted at all.
383+
384+
@param association_preferences: The list of allowed
385+
(association type, association session type) pairs that
386+
should be allowed for this consumer to use, in order from
387+
most preferred to least preferred.
388+
@type association_preferences: [(str, str)]
389+
390+
@returns: None
391+
392+
@see: C{L{openid.association.SessionNegotiator}}
393+
"""
394+
self.consumer.negotiator = SessionNegotiator(association_preferences)
395+
373396
class DiffieHellmanSHA1ConsumerSession(object):
374397
session_type = 'DH-SHA1'
375398
hash_func = staticmethod(cryptutil.sha1)
@@ -457,6 +480,13 @@ class GenericConsumer(object):
457480
"""This is the implementation of the common logic for OpenID
458481
consumers. It is unaware of the application in which it is
459482
running.
483+
484+
@ivar negotiator: An object that controls the kind of associations
485+
that the consumer makes. It defaults to
486+
C{L{openid.association.default_negotiator}}. Assign a
487+
different negotiator to it if you have specific requirements
488+
for how associations are made.
489+
@type negotiator: C{L{openid.association.SessionNegotiator}}
460490
"""
461491

462492
# The name of the query parameter that gets added to the return_to

‎openid/test/test_consumer.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,16 @@ def setUp(self):
13461346
self.identity_url,
13471347
self.consumer.session_key_prefix)
13481348

1349+
def test_setAssociationPreference(self):
1350+
self.consumer.setAssociationPreference([])
1351+
self.failUnless(isinstance(self.consumer.consumer.negotiator,
1352+
association.SessionNegotiator))
1353+
self.failUnlessEqual([],
1354+
self.consumer.consumer.negotiator.allowed_types)
1355+
self.consumer.setAssociationPreference([('FOO', 'BAR')])
1356+
self.failUnlessEqual([('FOO', 'BAR')],
1357+
self.consumer.consumer.negotiator.allowed_types)
1358+
13491359
def withDummyDiscovery(self, callable, dummy_getNextService):
13501360
class DummyDisco(object):
13511361
def __init__(self, *ignored):

0 commit comments

Comments
 (0)