1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
from u2flib_server.utils import websafe_decode
from u2flib_server.model import (JSONDict, RegistrationData, SignatureData,
U2fRegisterRequest, U2fSignRequest)
from binascii import b2a_hex
import unittest
SAMPLE_REG_DATA = websafe_decode(
'BQRFJ5xApW6uBsuSJ_FgUcL-seKecha71q8evgqfQwc5QuqQhv4qJoL3KpSUKX1T6XVJEqyOkn'
'Ok397y2Rh5m_zkQD9wrlgMW9EGOGmBQ0lKIa79vcQl2tMF7hlhNjIGrJHw7Rqg8wbnKWSvQjs5'
'ytK3s3X9YRuNcY3ht5pJYZbGa-4wggGHMIIBLqADAgECAgkAmb7osQyi7BwwCQYHKoZIzj0EAT'
'AhMR8wHQYDVQQDDBZZdWJpY28gVTJGIFNvZnQgRGV2aWNlMB4XDTEzMDcxNzE0MjEwM1oXDTE2'
'MDcxNjE0MjEwM1owITEfMB0GA1UEAwwWWXViaWNvIFUyRiBTb2Z0IERldmljZTBZMBMGByqGSM'
'49AgEGCCqGSM49AwEHA0IABDvhl91zfpg9n7DeCedcQ8gGXUnemiXoi-JEAxz-EIhkVsMPAyzh'
'tJZ4V3CqMZ-MOUgICt2aMxacMX9cIa8dgS2jUDBOMB0GA1UdDgQWBBQNqL-TV04iaO6mS5tjGE'
'6ShfexnjAfBgNVHSMEGDAWgBQNqL-TV04iaO6mS5tjGE6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iCJsq0LfeRA36X7aXi32Ap0rt_wyfnRef59YYr7SmwaMuXKjbIZS'
'LesscZZTMzXd-uuLb6DbUCasqEVBkGGqTRfAcOmPov1nHUrNDCkOR0obR4PsJG4PiamIfApNeo'
'XGYpGbok6nucMEUCIQCCL2jamBxyJQ6ktxgJVNFRKf4pUHvlvFgyXTQ6NOYlAwIgSQ1TB64V25'
'deHKak1UEZA2AbkR9znO2XJKd93v1BY9Y'
)
SAMPLE_SIG_DATA = websafe_decode(
'AAAAAAEwRgIhAPVBA3i9Ag6UOe9Jv-fz0J8HLIGfAS26eP8m0FRoZvufAiEAzmxU_mJxDJXOvV'
'-FMl_Ug2qYUGFZ6hu9m2VYdTkbvgA'
)
class RegistrationDataTest(unittest.TestCase):
def test_invalid_data(self):
self.assertRaises(ValueError, RegistrationData, b'abc')
def test_str(self):
rawresponse = RegistrationData(SAMPLE_REG_DATA)
self.assertEqual(b'050445279c', b2a_hex(rawresponse.bytes)[:10])
self.assertEqual(SAMPLE_REG_DATA, rawresponse.bytes)
def test_str_needs_fix(self):
rawresponse = RegistrationData(SAMPLE_REG_DATA_NEEDS_FIX)
self.assertEqual(b'050411d90f', b2a_hex(rawresponse.bytes)[:10])
self.assertNotEqual(SAMPLE_REG_DATA_NEEDS_FIX, rawresponse.bytes)
class SignatureDataTest(unittest.TestCase):
def test_str(self):
rawresponse = SignatureData(SAMPLE_SIG_DATA)
self.assertEqual(b'0000000001', b2a_hex(rawresponse.bytes)[:10])
self.assertEqual(SAMPLE_SIG_DATA, rawresponse.bytes)
class JSONDictTest(unittest.TestCase):
def test_create(self):
self.assertEqual({}, JSONDict())
def test_create_from_bytes(self):
self.assertEqual({'a': 1, 'b': 2}, JSONDict(b'{"a":1,"b":2}'))
def test_create_from_unicode(self):
self.assertEqual({'a': 1, 'b': 2}, JSONDict(u'{"a":1,"b":2}'))
def test_create_from_dict(self):
self.assertEqual({'a': 1, 'b': 2}, JSONDict({'a': 1, 'b': 2}))
def test_create_from_kwargs(self):
self.assertEqual({'a': 1, 'b': 2}, JSONDict(a=1, b=2))
def test_create_from_list(self):
self.assertEqual({}, JSONDict([]))
self.assertEqual({'a': 1, 'b': 2}, JSONDict([('a', 1), ('b', 2)]))
def test_create_wrong_nargs(self):
self.assertRaises(TypeError, JSONDict, {}, {})
self.assertRaises(TypeError, JSONDict, {'a': 1}, {'b': 2})
def test_json(self):
self.assertEqual('{}', JSONDict().json)
self.assertEqual('{"a": 1}', JSONDict(a=1).json)
def test_wrap(self):
self.assertTrue(isinstance(JSONDict.wrap({}), JSONDict))
x = JSONDict()
self.assertTrue(x is JSONDict.wrap(x))
def test_getattr_unknown(self):
self.assertRaises(AttributeError, lambda: JSONDict().foo)
def test_getattr(self):
self.assertEqual(1, JSONDict(a=1).a)
def test_required_fields(self):
class Foo(JSONDict):
_required_fields = ['foo', 'bar']
Foo({'foo': 1, 'bar': 2})
self.assertRaises(ValueError, Foo, {'foo': 1})
self.assertRaises(ValueError, Foo, {'bar': 1})
self.assertRaises(ValueError, Foo)
class U2fRegisterRequestTest(unittest.TestCase):
def test_u2f_register_request(self):
challenge = "Jtb6wLXjMHN67fV1BVNivz-qnAnD8OOqFju49RDBJro"
req = U2fRegisterRequest.create(
'https://example.com',
[],
websafe_decode(challenge)
)
self.assertEqual(U2fRegisterRequest.wrap({
"registeredKeys": [],
"appId": "https://example.com",
"registerRequests": [{
"version": "U2F_V2",
"challenge": "Jtb6wLXjMHN67fV1BVNivz-qnAnD8OOqFju49RDBJro"
}]
}), req)
self.assertEqual(U2fRegisterRequest.wrap(req.json), req)
self.assertEqual(
websafe_decode('EAaArVRs5qV39C9S3zO0z9ynVoWeZkuNfeMpsVDQnOk'),
req.applicationParameter
)
self.assertEqual([], req.registeredKeys)
self.assertEqual(1, len(req.registerRequests))
reg_req = req.get_request('U2F_V2')
self.assertEqual(reg_req.challenge, websafe_decode(challenge))
self.assertEqual(reg_req.version, 'U2F_V2')
class U2fSignRequestTest(unittest.TestCase):
def test_missing_keys(self):
self.assertRaises(ValueError, U2fSignRequest.wrap, {
"appId": "https://example.com",
"challenge": "0000",
"registeredKeys": []
})
def test_u2f_sign_request(self):
challenge = "Jtb6wLXjMHN67fV1BVNivz-qnAnD8OOqFju49RDBJro"
req = U2fSignRequest.wrap(
{
"appId": "https://example.com",
"registeredKeys": [{
"publicKey": "BBCcnAOknoMgokEGuTdfpNLQ-uylwlKp_xbEW8urjJsXK"
"v9XZSL-V8C2nwcPEckav1mKZFr5K96uAoLtuxOUf-E",
"version": "U2F_V2",
"keyHandle": "BIarIKfyMqyf4bEI6tOqGInAfHrrQkMA2eyPJlNnInbAG"
"1tXNpdRs48ef92_b1-mfN4VhaTWxo1SGoxT6CIanw"
}],
"challenge": challenge
}
)
self.assertEqual(U2fSignRequest.wrap(req.json), req)
self.assertEqual(
req.applicationParameter,
websafe_decode('EAaArVRs5qV39C9S3zO0z9ynVoWeZkuNfeMpsVDQnOk')
)
self.assertEqual(req.challenge, websafe_decode(challenge))
|