summaryrefslogtreecommitdiff
path: root/tests/test_auth_oauth.py
blob: 9c44f29d45a7e2f65abf53fe86032379b001d2e8 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import copy
import pytest
import time
from fxa.errors import ClientError

class TestOauth:
    @pytest.mark.parametrize("client_id,scopes", [
        # firefox
        ("5882386c6d801776", "profile:write https://identity.mozilla.com/apps/oldsync https://identity.mozilla.com/tokens/session"),
        # fenix
        ("a2270f727f45f648", "profile https://identity.mozilla.com/apps/oldsync https://identity.mozilla.com/tokens/session"),
    ])
    def test_oauth_client_scopes(self, account, client_id, scopes):
        body = {
            "client_id": client_id,
            "ttl": 60,
            "grant_type": "fxa-credentials",
            "access_type": "online",
            "scope": scopes,
        }
        s = account.post_a("/oauth/token", body)['access_token']
        account.post_a("/oauth/destroy", { "client_id": client_id, "token": s })

    def test_oauth_authorization_noauth(self, account):
        body = {
            "client_id": "5882386c6d801776",
            "ttl": 60,
            "grant_type": "fxa-credentials",
            "access_type": "online",
            "scope": "profile",
        }
        with pytest.raises(ClientError) as e:
            account.post("/oauth/authorization", body)
        assert e.value.details == {
            'code': 401,
            'errno': 109,
            'error': 'Unauthorized',
            'message': 'invalid request signature'
        }

    @pytest.mark.parametrize("args", [
        { "client_id": "5882386c6d801776", "state": "", "scope": "profile", "access_type": "invalid",
        "code_challenge": "", "code_challenge_method": "S256", "response_type": "code" },
        { "client_id": "5882386c6d801776", "state": "", "scope": "profile", "access_type": "online",
        "code_challenge": "", "code_challenge_method": "invalid", "response_type": "code" },
        { "client_id": "5882386c6d801776", "state": "", "scope": "profile", "access_type": "online",
        "code_challenge": "", "code_challenge_method": "S256", "response_type": "invalid" },
    ])
    def test_oauth_authorization_invalid(self, account, args):
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/authorization", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

    def test_oauth_authorization_badclientid(self, account):
        args = { "client_id": "invalid", "state": "", "scope": "profile", "access_type": "online",
                "code_challenge": "", "code_challenge_method": "S256", "response_type": "code" }
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/authorization", args)
        assert e.value.details == {
            'code': 400,
            'errno': 162,
            'error': 'Bad Request',
            'message': 'unknown client_id'
        }

    def test_oauth_authorization_badscope(self, account):
        args = { "client_id": "5882386c6d801776", "state": "", "scope": "invalid", "access_type": "online",
                "code_challenge": "", "code_challenge_method": "S256", "response_type": "code" }
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/authorization", args)
        assert e.value.details == {
            'code': 400,
            'errno': 169,
            'error': 'Bad Request',
            'message': 'requested scopes not allowed'
        }

    # see below for combined /authorization + unauthed /token

    def test_oauth_destroy_notoken(self, account):
        args = { "client_id": "5882386c6d801776", "token": "00" * 32 }
        with pytest.raises(ClientError) as e:
            account.post("/oauth/destroy", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

    @pytest.mark.xfail
    def test_oauth_destroy_badclient(self, account, refresh_token):
        args = { "client_id": "other", "token": refresh_token.bearer }
        with pytest.raises(ClientError) as e:
            account.post("/oauth/destroy", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

    def test_oauth_scoped_keys_badclient(self, account):
        with pytest.raises(ClientError) as e:
            account.post_a("/account/scoped-key-data", {
                "client_id": "invalid",
                "scope": "https://identity.mozilla.com/apps/oldsync"
            })
        assert e.value.details == {
            'code': 400,
            'errno': 162,
            'error': 'Bad Request',
            'message': 'unknown client_id'
        }

    def test_oauth_scoped_keys_badscope(self, account):
        with pytest.raises(ClientError) as e:
            account.post_a("/account/scoped-key-data", {
                "client_id": "5882386c6d801776",
                "scope": "scope"
            })
        assert e.value.details == {
            'code': 400,
            'errno': 169,
            'error': 'Bad Request',
            'message': 'requested scopes not allowed'
        }

    def test_oauth_scoped_keys(self, account):
        resp = account.post_a("/account/scoped-key-data", {
            "client_id": "5882386c6d801776",
            "scope": "https://identity.mozilla.com/apps/oldsync"
        })
        assert resp == {
            "https://identity.mozilla.com/apps/oldsync": {
                "identifier": "https://identity.mozilla.com/apps/oldsync",
                "keyRotationSecret": "00" * 32,
                "keyRotationTimestamp": 0,
            },
        }

    @pytest.mark.parametrize("access_type", ["online", "offline"])
    def test_oauth_token_fxa_badclient(self, account, access_type):
        body = { "client_id": "invalid", "ttl": 60, "grant_type": "fxa-credentials",
                "access_type": access_type, "scope": "profile" }
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/token", body)
        assert e.value.details == {
            'code': 400,
            'errno': 162,
            'error': 'Bad Request',
            'message': 'unknown client_id'
        }

    @pytest.mark.parametrize("access_type", ["online", "offline"])
    def test_oauth_token_fxa_badscope(self, account, access_type):
        body = { "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "fxa-credentials",
                "access_type": access_type, "scope": "scope" }
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/token", body)
        assert e.value.details == {
            'code': 400,
            'errno': 169,
            'error': 'Bad Request',
            'message': 'requested scopes not allowed'
        }

    @pytest.mark.parametrize("args,code,error,errno,message", [
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "authorization_code",
           "code": "invalid", "code_verifier": "test" },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "authorization_code",
           "code_verifier": "test", "extra": 0 },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "authorization_code",
           "code": "00" * 32, "code_verifier": "test" },
         401, 'Unauthorized', 110, 'invalid authentication token'),
        ({ "client_id": "invalid", "ttl": 60, "grant_type": "authorization_code",
           "code_verifier": "test" },
         400, 'Bad Request', 162, 'unknown client_id'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "authorization_code",
           "code_verifier": "invalid" },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
    ])
    def test_oauth_token_other_invalidcode(self, account, args, code, error, errno, message, auth_code):
        args = copy.deepcopy(args)
        if 'code' not in args: args['code'] = auth_code
        with pytest.raises(ClientError) as e:
            account.post("/oauth/token", args)
        assert e.value.details == { 'code': code, 'errno': errno, 'error': error, 'message': message }

    @pytest.mark.parametrize("args,code,error,errno,message", [
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "fxa-credentials",
           "scope": "profile", "access_type": "online" },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "refresh_token",
           "refresh_token": "invalid", "code_verifier": "test" },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "refresh_token",
           "scope": "profile", "extra": 0 },
         400, 'Bad Request', 107, 'invalid parameter in request body'),
        ({ "client_id": "invalid", "ttl": 60, "grant_type": "refresh_token",
           "scope": "profile" },
         400, 'Bad Request', 162, 'unknown client_id'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "refresh_token",
           "scope": "foo" },
         400, 'Bad Request', 169, 'requested scopes not allowed'),
        ({ "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "refresh_token",
           "scope": "profile:write" },
         400, 'Bad Request', 169, 'requested scopes not allowed'),
    ])
    def test_oauth_token_other_invalidrefresh(self, account, args, code, error, errno, message, refresh_token):
        args = copy.deepcopy(args)
        if 'refresh_token' not in args: args['refresh_token'] = refresh_token.bearer
        with pytest.raises(ClientError) as e:
            account.post("/oauth/token", args)
        assert e.value.details == { 'code': code, 'errno': errno, 'error': error, 'message': message }

    @pytest.mark.parametrize("refresh", [False, True])
    def test_oauth_fxa(self, account, refresh):
        body = {
            "client_id": "5882386c6d801776",
            "ttl": 60,
            "grant_type": "fxa-credentials",
            "access_type": "offline" if refresh else "online",
            "scope": "profile",
        }
        resp = account.post_a("/oauth/token", body)

        assert 'access_token' in resp
        assert ('refresh_token' in resp) == refresh
        assert 'session_token' not in resp
        assert resp['scope'] == 'profile'
        assert resp['token_type'] == 'bearer'
        assert resp['expires_in'] <= 60
        assert (resp['auth_at'] - time.time()) < 10
        assert 'keys_jwe' not in resp

    @pytest.mark.parametrize("keys_jwe", [None, "keyskeyskeys"])
    @pytest.mark.parametrize("refresh,session", [
        (False, False),
        (True, False),
        (True, True),
    ])
    def test_oauth_auth_code(self, account, keys_jwe, refresh, session):
        scope = "profile" + (" https://identity.mozilla.com/tokens/session" if session else "")
        body = {
            "client_id": "5882386c6d801776",
            "state": "test",
            "keys_jwe": keys_jwe,
            "scope": scope,
            "access_type": "offline" if refresh else "online",
            "code_challenge": "n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg", # "test"
            "code_challenge_method": "S256",
            "response_type": "code",
        }
        resp = account.post_a("/oauth/authorization", body)
        assert resp['state'] == "test"

        body = {
            "client_id": "5882386c6d801776",
            "ttl": 60,
            "grant_type": "authorization_code",
            "code": resp['code'],
            "code_verifier": "test",
        }
        resp = account.post("/oauth/token", body)
        assert 'access_token' in resp
        assert ('refresh_token' in resp) == refresh
        assert ('session_token' in resp) == session
        assert resp['scope'] == 'profile'
        assert resp['token_type'] == 'bearer'
        assert resp['expires_in'] <= 60
        assert (resp['auth_at'] - time.time()) < 10
        assert keys_jwe is None or (resp['keys_jwe'] == keys_jwe)

    def test_oauth_refresh(self, account, refresh_token):
        body = {
            "client_id": "5882386c6d801776",
            "ttl": 60,
            "grant_type": "refresh_token",
            "refresh_token": refresh_token.bearer,
            "scope": "profile",
        }
        resp = account.post("/oauth/token", body)

        assert 'access_token' in resp
        assert 'refresh_token' not in resp
        assert 'session_token' not in resp
        assert resp['scope'] == 'profile'
        assert resp['token_type'] == 'bearer'
        assert resp['expires_in'] <= 60
        assert (resp['auth_at'] - time.time()) < 10
        assert 'keys_jwe' not in resp

    @pytest.mark.parametrize("grant_type,access_type", [
        ("authorization_code", "online"),
        ("refresh_token", "online"),
        ("fxa-credentials", "foo"),
    ])
    def test_oauth_token_fxa_invalid(self, account, grant_type, access_type):
        body = { "client_id": "5882386c6d801776", "ttl": 60, "grant_type": grant_type,
                "access_type": access_type, "scope": "scope" }
        with pytest.raises(ClientError) as e:
            account.post_a("/oauth/token", body)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

def test_oauth_authorization_unverified(unverified_account):
    body = {
        "client_id": "5882386c6d801776",
        "ttl": 60,
        "grant_type": "fxa-credentials",
        "access_type": "online",
        "scope": "profile",
    }
    with pytest.raises(ClientError) as e:
        unverified_account.post_a("/oauth/authorization", body)
    assert e.value.details == {
        'code': 400,
        'errno': 138,
        'error': 'Bad Request',
        'message': 'unverified session'
    }

def test_oauth_scoped_unverified(unverified_account):
    with pytest.raises(ClientError) as e:
        unverified_account.post_a("/account/scoped-key-data", {
                "client_id": "5882386c6d801776",
                "scope": "https://identity.mozilla.com/apps/oldsync"
        })
    assert e.value.details == {
        'code': 400,
        'errno': 138,
        'error': 'Bad Request',
        'message': 'unverified session'
    }

def test_oauth_token_unverified(unverified_account):
    body = { "client_id": "5882386c6d801776", "ttl": 60, "grant_type": "fxa-credentials",
             "access_type": "online", "scope": "profile" }
    with pytest.raises(ClientError) as e:
        unverified_account.post_a("/oauth/token", body)
    assert e.value.details == {
        'code': 400,
        'errno': 138,
        'error': 'Bad Request',
        'message': 'unverified session'
    }

@pytest.fixture
def auth_code(account):
    body = {
        "client_id": "5882386c6d801776",
        "state": "test",
        "scope": "profile",
        "access_type": "online",
        "code_challenge": "n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg", # "test"
        "code_challenge_method": "S256",
        "response_type": "code",
    }
    return account.post_a("/oauth/authorization", body)['code']