Skip to content

Commit 48e054e

Browse files
author
Josh Hoyt
committed
[project @ Move signature checking logic into association]
1 parent 15bc369 commit 48e054e

3 files changed

Lines changed: 60 additions & 27 deletions

File tree

‎openid/association.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from openid import cryptutil
99
from openid import kvform
1010
from openid import oidutil
11+
from openid.message import OPENID_NS
1112

1213
all_association_types = ['HMAC-SHA1', 'HMAC-SHA256']
1314
if hasattr(cryptutil, 'hmacSha256'):
@@ -430,3 +431,42 @@ def checkSignature(self, data, prefix='openid.'):
430431
return False
431432

432433
return request_sig == expected_sig
434+
435+
def getMessageSignature(self, message):
436+
"""Return the signature of a message with a signed list
437+
438+
XXX: this is association-type specific
439+
"""
440+
signed = message.getArg(OPENID_NS, 'signed')
441+
if not signed:
442+
raise ValueError('Message has no signed list')
443+
444+
signed_list = signed.split(',')
445+
args = message.toPostArgs()
446+
return self.signDict(signed_list, args)
447+
448+
def signMessage(self, message):
449+
"""Add a signature (and a signed list) to a message
450+
451+
XXX: this is association-type specific
452+
"""
453+
if (message.hasKey(OPENID_NS, 'sig') or
454+
message.hasKey(OPENID_NS, 'signed')):
455+
raise ValueError('Message already has signed list or signature')
456+
457+
signed_message = message.copy()
458+
args = message.toPostArgs()
459+
signed_list = [k[7:] for k in args if k.startswith('openid.')]
460+
signed_list.append('signed')
461+
signed_message.setArg(OPENID_NS, 'signed', ','.join(signed))
462+
sig = self.getMessageSignature(signed_message)
463+
signed_message.setArg(OPENID_NS, 'sig', sig)
464+
return signed_message
465+
466+
def checkMessageSignature(self, message):
467+
"""Given a message with a signature, calculate a new signature
468+
and return whether it matches the signature in the message.
469+
"""
470+
calculated_sig = self.getMessageSignature(message)
471+
message_sig = message.getArg(OPENID_NS, 'sig')
472+
return calculated_sig == message_sig

‎openid/consumer/consumer.py‎

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,9 @@ def _doIdRes(self, message, endpoint):
526526
if user_setup_url is not None:
527527
return SetupNeededResponse(endpoint, user_setup_url)
528528

529-
signed = message.getArg(OPENID_NS, 'signed')
530-
if signed:
531-
signed_list = signed.split(',')
532-
else:
533-
signed_list = []
534-
535529
try:
530+
signed_list = self._idResCheckSignature(message,
531+
endpoint.server_url)
536532
self._idResCheckForFields(message, signed_list)
537533
except ValueError, e:
538534
return FailureResponse(endpoint, e.args[0])
@@ -553,13 +549,14 @@ def _doIdRes(self, message, endpoint):
553549
return FailureResponse(
554550
endpoint, fmt % (endpoint.delegate, server_id2))
555551

556-
assoc_handle = message.getArg(OPENID_NS, 'assoc_handle')
557-
assoc = self.store.getAssociation(endpoint.server_url, assoc_handle)
552+
# XXX: association-type specific
553+
signed_fields = ['openid.' + f for f in signed_list]
554+
return SuccessResponse(endpoint, message, signed_fields)
558555

559-
# Fail if the identity field is present but not signed
560-
if endpoint.identity_url is not None and 'identity' not in signed_list:
561-
msg = '"openid.identity" not signed'
562-
return FailureResponse(endpoint, msg)
556+
557+
def _idResCheckSignature(self, message, server_url):
558+
assoc_handle = message.getArg(OPENID_NS, 'assoc_handle')
559+
assoc = self.store.getAssociation(server_url, assoc_handle)
563560

564561
if assoc:
565562
if assoc.getExpiresIn() <= 0:
@@ -568,26 +565,22 @@ def _doIdRes(self, message, endpoint):
568565
# automatically opens the possibility for
569566
# denial-of-service by a server that just returns expired
570567
# associations (or really short-lived associations)
571-
msg = 'Association with %s expired' % (endpoint.server_url,)
572-
return FailureResponse(endpoint, msg)
573-
574-
# Check the signature
575-
v_sig = assoc.signDict(signed_list, message.toPostArgs())
568+
raise ValueError('Association with %s expired' % (server_url,))
576569

577-
if v_sig != message.getArg(OPENID_NS, 'sig'):
578-
return FailureResponse(endpoint, 'Bad signature')
570+
if not assoc.checkMessageSignature(message):
571+
raise ValueError('Bad signature')
579572

580573
else:
581574
# It's not an association we know about. Stateless mode is our
582575
# only possible path for recovery.
583576
# XXX - async framework will not want to block on this call to
584577
# _checkAuth.
585-
if not self._checkAuth(message, endpoint.server_url):
586-
return FailureResponse(endpoint,
587-
'Server denied check_authentication')
578+
if not self._checkAuth(message, server_url):
579+
raise ValueError('Server denied check_authentication')
588580

589-
signed_fields = ['openid.' + f for f in signed_list]
590-
return SuccessResponse(endpoint, message, signed_fields)
581+
# XXX: association-type specific
582+
signed = message.getArg(OPENID_NS, 'signed')
583+
return signed.split(',')
591584

592585

593586
def _idResCheckForFields(self, message, signed_list):

‎openid/test/test_consumer.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ class GoodAssociation:
7676
def getExpiresIn(self):
7777
return self.expiresIn
7878

79-
def signDict(fields, data, prefix="openid."):
80-
return GOODSIG
81-
79+
def checkMessageSignature(self, message):
80+
return message.getArg(OPENID_NS, 'sig') == GOODSIG
8281

8382

8483
class GoodAssocStore(_memstore.MemoryStore):
@@ -324,6 +323,7 @@ def test_idResURLMismatch(self):
324323
'openid.sig': GOODSIG,
325324
'openid.signed': 'identity,return_to',
326325
})
326+
self.consumer.store = GoodAssocStore()
327327
r = self.consumer.complete(message, self.endpoint)
328328
self.failUnlessEqual(r.status, FAILURE)
329329
self.failUnlessEqual(r.identity_url, self.consumer_id)

0 commit comments

Comments
 (0)