Skip to content

Commit 956192f

Browse files
author
Kevin Turner
committed
[project @ association.Association.sign: add mac definition for HMAC-SHA1-SIGNALL type.]
1 parent 4453b9f commit 956192f

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎openid/association.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ class Association(object):
190190
]
191191

192192

193+
_macs = {
194+
'HMAC-SHA1': cryptutil.hmacSha1,
195+
'HMAC-SHA256-SIGNALL': cryptutil.hmacSha256,
196+
}
197+
_macs['HMAC-SHA1-SIGNALL'] = _macs['HMAC-SHA1']
198+
199+
193200
def fromExpiresIn(cls, expires_in, handle, secret, assoc_type):
194201
"""
195202
This is an alternate constructor used by the OpenID consumer
@@ -402,13 +409,12 @@ def sign(self, pairs):
402409
@rtype: str
403410
"""
404411
kv = kvform.seqToKV(pairs)
405-
if self.assoc_type == 'HMAC-SHA1':
406-
mac = cryptutil.hmacSha1
407-
elif self.assoc_type == 'HMAC-SHA256-SIGNALL':
408-
mac = cryptutil.hmacSha256
409-
else:
412+
413+
try:
414+
mac = self._macs[self.assoc_type]
415+
except KeyError:
410416
raise ValueError(
411-
'Unkown association type: %r' % (self.assoc_type,))
417+
'Unknown association type: %r' % (self.assoc_type,))
412418

413419
return mac(self.secret, kv)
414420

‎openid/test/test_association.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,45 @@ def test_sha1signAll(self):
124124
self.failUnlessEqual(self.assoc.sign_all, True)
125125

126126

127+
class TestMac(unittest.TestCase):
128+
def setUp(self):
129+
self.pairs = [('key1', 'value1'),
130+
('key2', 'value2')]
131+
132+
133+
def test_sha1(self):
134+
assoc = association.Association.fromExpiresIn(
135+
3600, '{sha1}', 'very_secret', "HMAC-SHA1")
136+
expected = ('\xe0\x1bv\x04\xf1G\xc0\xbb\x7f\x9a\x8b'
137+
'\xe9\xbc\xee}\\\xe5\xbb7*')
138+
sig = assoc.sign(self.pairs)
139+
self.failUnlessEqual(sig, expected)
140+
141+
142+
def test_sha1signAll(self):
143+
assoc = association.Association.fromExpiresIn(
144+
3600, '{sha1SA}', 'very_secret', "HMAC-SHA1-SIGNALL")
145+
expected = ('\xe0\x1bv\x04\xf1G\xc0\xbb\x7f\x9a\x8b'
146+
'\xe9\xbc\xee}\\\xe5\xbb7*')
147+
sig = assoc.sign(self.pairs)
148+
self.failUnlessEqual(sig, expected)
149+
150+
try:
151+
from openid import cryptutil
152+
cryptutil.sha256('')
153+
except NotImplementedError:
154+
import warnings
155+
warnings.warn("Not running SHA256 tests.")
156+
else:
157+
def test_sha256signAll(self):
158+
assoc = association.Association.fromExpiresIn(
159+
3600, '{sha256SA}', 'very_secret', "HMAC-SHA256-SIGNALL")
160+
expected = ('\xfd\xaa\xfe;\xac\xfc*\x988\xad\x05d6-\xeaVy'
161+
'\xd5\xa5Z.<\xa9\xed\x18\x82\\$\x95x\x1c&')
162+
sig = assoc.sign(self.pairs)
163+
self.failUnlessEqual(sig, expected)
164+
165+
127166

128167
def pyUnitTests():
129168
return datadriven.loadTests(__name__)

0 commit comments

Comments
 (0)