File: test_u2f.py

package info (click to toggle)
python-u2flib-server 5.0.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: python: 1,561; makefile: 9
file content (209 lines) | stat: -rw-r--r-- 8,121 bytes parent folder | download | duplicates (3)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from u2flib_server.u2f import (begin_registration, complete_registration,
                               begin_authentication, complete_authentication)
from u2flib_server.model import (U2fRegisterRequest, U2fSignRequest,
                                 RegisterResponse)
from u2flib_server.utils import websafe_decode, websafe_encode
from .soft_u2f_v2 import SoftU2FDevice
import unittest
import six

APP_ID = 'https://www.example.com'
APP_ID = 'http://www.example.com/appid'
FACET = 'http://www.example.com'
FACETS = [FACET]


def register_token(devices=[]):
    token = SoftU2FDevice()
    request = begin_registration(APP_ID, devices)
    data = request.data_for_client
    response = token.register(FACET, data['appId'], data['registerRequests'][0])
    device, cert = complete_registration(request.json, response)
    return device, token


class U2fTest(unittest.TestCase):

    def test_register_fixed_values(self):
        req = U2fRegisterRequest.create(
            'http://localhost:8081',
            [],
            websafe_decode('KEzvDDdHwnXtPHIMb0Uh43hgOJ-wQTsdLujGkeg6JxM')
        )
        reg, cert = req.complete({
            "version": "U2F_V2",
            "registrationData": "BQS94xQL46G4vheJPkYSuEteM6Km4-MwgBAu1zZ6MAbjDD"
            "gqhYbpHuIhhGOKjedeDd58qqktqOJsby9wMdHGnUtVQD8ISPywVi3J6SaKebCVQdHP"
            "u3_zQigRS8LhoDwKT5Ed3tg8AWuNw9XBZEh4doEDxKGuInFazirUw8acOu2qDcEwgg"
            "IjMIIBDaADAgECAgRyuHt0MAsGCSqGSIb3DQEBCzAPMQ0wCwYDVQQDEwR0ZXN0MB4X"
            "DTE1MDkwNDA3MTAyNloXDTE2MDkwMzA3MTAyNlowKjEoMCYGA1UEAxMfWXViaWNvIF"
            "UyRiBFRSBTZXJpYWwgMTkyNDY5Mjg1MjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA"
            "BC37i_h-xmEtGfWnuvj_BmuhtU18MKShNP_vZ7C2WJwj8OHaSLnzAfha14CMUPaKPt"
            "RFfP6w9CFGhvEizH33XZKjOzA5MCIGCSsGAQQBgsQKAgQVMS4zLjYuMS40LjEuNDE0"
            "ODIuMS4yMBMGCysGAQQBguUcAgEBBAQDAgQwMAsGCSqGSIb3DQEBCwOCAQEAab7fWl"
            "J-lOR1sqIxawPU5DWZ1b9nQ0QmNNoetPHJ_fJC95r0esRq5axfmGufbNktNWanHww7"
            "i9n5WWxSaMTWuJSF0eAXUajo8odYA8nB4_0I6z615MWa9hTU64Pl9HlqkR5ez5jndm"
            "JNuAfhaIF4h062Jw051kMo_aENxuLixnybTfJG7Q5KRE00o2MFs5b9L9fzhDtBzv5Z"
            "-vGOefuiohowpwnxIA9l0tGqrum9plUdx06K9TqKMRDQ8naosy01rbouA6i5xVjl-t"
            "HT3z-r__FYcSZ_dQ5-SCPOh4F0w6T0UwzymQmeqYN3pP-UUgnJ-ihD-uhEWklKNYRy"
            "0K0G0jBGAiEA7rbbx2jwC1YGICkZMR07ggKWaHCwFBxNDW3OwhLNNzUCIQCSq0sjGS"
            "UnWMQgPEImrmd3tMKcbrjI995rti6UYozqsg",
            "clientData": "eyJvcmlnaW4iOiAiaHR0cDovL2xvY2FsaG9zdDo4MDgxIiwgImNo"
            "YWxsZW5nZSI6ICJLRXp2RERkSHduWHRQSElNYjBVaDQzaGdPSi13UVRzZEx1akdrZW"
            "c2SnhNIiwgInR5cCI6ICJuYXZpZ2F0b3IuaWQuZmluaXNoRW5yb2xsbWVudCJ9"
        })
        assert reg

    def test_authenticate_fixed_values(self):
        device = {
            'version': 'U2F_V2',
            'publicKey': 'BBCcnAOknoMgokEGuTdfpNLQ-uylwlKp_xbEW8urjJsXKv9XZSL-V'
            '8C2nwcPEckav1mKZFr5K96uAoLtuxOUf-E',
            'keyHandle': 'BIarIKfyMqyf4bEI6tOqGInAfHrrQkMA2eyPJlNnInbAG1tXNpdRs'
            '48ef92_b1-mfN4VhaTWxo1SGoxT6CIanw',
            'appId': 'http://www.example.com/appid'
        }
        response = {
            'keyHandle': 'BIarIKfyMqyf4bEI6tOqGInAfHrrQkMA2eyPJlNnInbAG1tXNpdRs'
            '48ef92_b1-mfN4VhaTWxo1SGoxT6CIanw',
            'signatureData': 'AAAAAAEwRQIhAJrcBSpaDprFzXmVw60r6x-_gOZ0t-8v7DGii'
            'Kmar0SAAiAYKKEX41nWUCLLoKiBYuHYdPP1MPPNQ0cX_JIybPtThA',
            'clientData': 'eyJvcmlnaW4iOiAiaHR0cHM6Ly93d3cuZXhhbXBsZS5jb20iLCAi'
            'Y2hhbGxlbmdlIjogIm9JZXUtblB4eDlEY0Y3TF9EQ0Uza3ZZb3gtYzRVdXZGYjhsTk'
            'c2dGgxMG8iLCAidHlwIjogIm5hdmlnYXRvci5pZC5nZXRBc3NlcnRpb24ifQ'
        }
        req = U2fSignRequest.create(
            'http://www.example.com/appid',
            [device],
            websafe_decode('oIeu-nPxx9DcF7L_DCE3kvYox-c4UuvFb8lNG6th10o')
        )
        req.complete(response)

    def test_register_soft_u2f(self):
        device, token = register_token()
        assert device

    def test_authenticate_single_soft_u2f(self):
        # Register
        device, token = register_token()

        # Authenticate
        request = begin_authentication(APP_ID, [device])
        data = request.data_for_client

        response = token.getAssertion(
            FACET,
            data['appId'],
            data['challenge'],
            data['registeredKeys'][0]
        )

        complete_authentication(request.json, response)

    def test_authenticate_multiple_soft_u2f(self):
        # Register
        device1, token1 = register_token()
        device2, token2 = register_token([device1])

        # Authenticate
        request = begin_authentication(APP_ID, [device1, device2])
        data = request.data_for_client
        response = token1.getAssertion(
            FACET,
            data['appId'],
            data['challenge'],
            data['registeredKeys'][0]
        )

        complete_authentication(request.json, response)

    def test_authenticate_soft_u2f(self):
        device, token = register_token()

        challenge1 = U2fSignRequest.create(APP_ID, [device])
        data1 = challenge1.data_for_client
        challenge2 = U2fSignRequest.create(APP_ID, [device])
        data2 = challenge2.data_for_client

        response2 = token.getAssertion(
            FACET,
            data2['appId'],
            data2['challenge'],
            data2['registeredKeys'][0]
        )
        response1 = token.getAssertion(
            FACET,
            data1['appId'],
            data1['challenge'],
            data1['registeredKeys'][0]
        )

        challenge1.complete(response1)
        challenge2.complete(response2)

        self.assertRaisesRegex(ValueError, 'challenge', challenge1.complete,
                               response2)

        self.assertRaisesRegex(ValueError, 'challenge', challenge2.complete,
                               response1)

    def test_wrong_facet(self):
        token = SoftU2FDevice()
        request = U2fRegisterRequest.create(APP_ID, [])
        data = request.data_for_client
        response = token.register(
            "http://wrongfacet.com",
            data['appId'],
            data['registerRequests'][0]
        )

        self.assertRaisesRegex(ValueError, 'facet', request.complete, response,
                               FACETS)

        response2 = token.register(
            FACET,
            data['appId'],
            data['registerRequests'][0]
        )
        device, cert = request.complete(response2, FACETS)

        signreq = U2fSignRequest.create(APP_ID, [device])
        data = signreq.data_for_client
        response = token.getAssertion(
            'http://notright.com',
            data['appId'],
            data['challenge'],
            data['registeredKeys'][0]
        )

        self.assertRaisesRegex(ValueError, 'facet', signreq.complete, response,
                               FACETS)

    def test_wrong_challenge(self):
        device = SoftU2FDevice()
        request = begin_registration(APP_ID)
        data = request.data_for_client
        response = device.register(FACET, data['appId'],
                                   data['registerRequests'][0])
        request2 = begin_registration(APP_ID)
        self.assertRaisesRegex(ValueError, 'challenge', complete_registration,
                               request2.json, response)

    def test_invalid_signature(self):
        device = SoftU2FDevice()
        request = begin_registration(APP_ID)
        data = request.data_for_client
        response = device.register(FACET, data['appId'],
                                   data['registerRequests'][0])
        response = RegisterResponse.wrap(response)
        raw_data = response.registrationData.bytes
        raw_data = raw_data[:-4] + b'\0\0\0\0'
        response['registrationData'] = websafe_encode(raw_data)
        response = response.json

        self.assertRaisesRegex(ValueError, 'signature', complete_registration,
                               request.json, response)


if six.PY2:
    U2fTest.assertRaisesRegex = U2fTest.assertRaisesRegexp