File: test_recovery_codes.py

package info (click to toggle)
flask-security 5.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,420 kB
  • sloc: python: 23,164; javascript: 204; makefile: 138
file content (337 lines) | stat: -rw-r--r-- 12,547 bytes parent folder | download | duplicates (2)
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
"""
test_recovery_codes
~~~~~~~~~~~~~~~~~

recovery code tests

:copyright: (c) 2022-2024 by J. Christopher Wagner (jwag).
:license: MIT, see LICENSE for more details.
"""

import re

import pytest

from tests.test_two_factor import tf_authenticate
from tests.test_utils import (
    authenticate,
    logout,
    reset_fresh,
    setup_tf_sms,
)

pytestmark = pytest.mark.two_factor()


@pytest.mark.settings(multi_factor_recovery_codes=True)
def test_rc_json(app, clients, get_message):
    # Test recovery codes
    # gal has two-factor already setup for 'sms'
    client = clients
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    tf_authenticate(app, client)

    response = client.get("/mf-recovery-codes", headers=headers)
    assert response.status_code == 200
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 0

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5

    response = client.get("/mf-recovery-codes", headers=headers)
    recodes = response.json["response"]["recovery_codes"]
    assert len(recodes) == 5 and codes[0] == recodes[0]

    response = client.get("/mf-recovery-codes", headers=headers)
    assert response.status_code == 200
    assert not hasattr(response.json["response"], "recovery_codes")

    # endpoint is for unauthenticated only
    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 400
    logout(client)

    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 200

    # verify actually logged in
    response = client.get("/profile", follow_redirects=False)
    assert response.status_code == 200

    # logout and try again - first code shouldn't work again
    logout(client)
    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 400
    assert response.json["response"]["errors"][0].encode("utf-8") == get_message(
        "INVALID_RECOVERY_CODE"
    )
    response = client.post("/mf-recovery", json=dict(code=codes[1]))
    assert response.status_code == 200


@pytest.mark.settings(multi_factor_recovery_codes=True)
def test_rc(app, client, get_message):
    # Test recovery codes
    # gal has two-factor already setup for 'sms'
    tf_authenticate(app, client)

    response = client.get("/mf-recovery-codes?show_codes=hi")
    assert response.status_code == 200
    assert get_message("NO_RECOVERY_CODES_SETUP") in response.data

    response = client.post("/mf-recovery-codes")
    rd = response.data.decode("utf-8")
    codes = re.findall(r"[a-f,\d]{4}-[a-f,\d]{4}-[a-f,\d]{4}", rd)
    assert len(codes) == 5

    response = client.get("/mf-recovery-codes?show_codes=hi")
    assert response.status_code == 200
    assert b"Recovery Codes" in response.data
    rd = response.data.decode("utf-8")
    codes = re.findall(r"[a-f,\d]{4}-[a-f,\d]{4}-[a-f,\d]{4}", rd)
    assert len(codes) == 5

    # endpoint is for unauthenticated only
    response = client.post(
        "/mf-recovery", data=dict(code=codes[0]), follow_redirects=False
    )
    assert response.status_code == 302
    logout(client)

    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post(
        "/mf-recovery", data=dict(code=codes[0]), follow_redirects=True
    )
    assert response.status_code == 200

    # verify actually logged in
    response = client.get("/profile", follow_redirects=False)
    assert response.status_code == 200

    # logout and try again - first code shouldn't work again
    logout(client)
    response = client.post("/login", data=dict(email="gal@lp.com", password="password"))

    response = client.post("/mf-recovery", data=dict(code=codes[0]))
    assert get_message("INVALID_RECOVERY_CODE") in response.data
    response = client.post(
        "/mf-recovery", data=dict(code=codes[1]), follow_redirects=True
    )
    assert response.status_code == 200
    # verify actually logged in
    response = client.get("/profile", follow_redirects=False)
    assert response.status_code == 200


@pytest.mark.settings(multi_factor_recovery_codes=True)
def test_rc_reset(app, client, get_message):
    # test that reset_user_access, removes recovery codes
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    tf_authenticate(app, client)

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5

    with app.app_context():
        user = app.security.datastore.find_user(email="gal@lp.com")
        app.security.datastore.reset_user_access(user)
        app.security.datastore.commit()

    client.post("/login", json=dict(email="gal@lp.com", password="password"))
    response = client.get("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 0


@pytest.mark.settings(multi_factor_recovery_codes=True, url_prefix="/api")
def test_rc_bad_state(app, client, get_message):
    response = client.post("/api/mf-recovery", json=dict(code="hi"))
    assert response.status_code == 400
    assert response.json["response"]["errors"][0].encode("utf=8") == get_message(
        "TWO_FACTOR_PERMISSION_DENIED"
    )

    response = client.post(
        "/api/mf-recovery", data=dict(code="hi"), follow_redirects=False
    )
    assert response.status_code == 302
    assert "/api/login" in response.location


@pytest.mark.settings(multi_factor_recovery_codes=True)
def test_rc_rescue(app, client, get_message):
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    tf_authenticate(app, client)

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5
    logout(client)

    data = dict(email="gal@lp.com", password="password")
    response = client.post("/login", json=data)
    assert response.json["response"]["tf_required"]

    response = client.get("/tf-rescue")
    assert b"Use previously downloaded recovery code" in response.data

    response = client.get("/tf-rescue", headers=headers)
    options = response.json["response"]["recovery_options"]
    assert "recovery_code" in options.keys()
    assert "/mf-recovery" in options["recovery_code"]

    response = client.post(
        "/tf-rescue", data=dict(help_setup="recovery_code"), follow_redirects=False
    )
    assert "/mf-recovery" in response.location


@pytest.mark.settings(multi_factor_recovery_codes=True)
def test_fresh(app, client):
    # Make sure fetching recovery codes is protected with a freshness check.
    headers = {"Accept": "application/json", "Content-Type": "application/json"}

    authenticate(client)
    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5

    reset_fresh(client, app.config["SECURITY_FRESHNESS"])
    response = client.post("/mf-recovery-codes", headers=headers)
    assert response.status_code == 401
    assert response.json["response"]["reauth_required"]

    response = client.post("/verify", json=dict(password="password"))
    assert response.status_code == 200

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5


@pytest.mark.settings(
    multi_factor_recovery_codes=True,
    multi_factor_recovery_codes_keys=[b"6_WhDTwI1RKJ3_ra9nADCBQbrRywlA88psGsq21xcsU="],
)
def test_rc_json_encrypted(app, client, get_message):
    # Test recovery codes with encryption option
    # gal has two-factor already setup for 'sms'
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    tf_authenticate(app, client)

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5

    # endpoint is for unauthenticated only
    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 400
    logout(client)

    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 200

    # verify actually logged in
    response = client.get("/profile", follow_redirects=False)
    assert response.status_code == 200

    # logout and try again - first code shouldn't work again
    logout(client)
    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 400
    assert response.json["response"]["errors"][0].encode("utf-8") == get_message(
        "INVALID_RECOVERY_CODE"
    )
    response = client.post("/mf-recovery", json=dict(code=codes[1]))
    assert response.status_code == 200

    # make sure DB actually has encrypted codes!
    with app.test_request_context("/"):
        user = app.security.datastore.find_user(email="gal@lp.com")
        codes = user.mf_recovery_codes
        assert len(codes) == 3
        assert len(codes[0]) == 100


@pytest.mark.settings(
    multi_factor_recovery_codes=True,
    multi_factor_recovery_codes_keys=[b"6_WhDTwI1RKJ3_ra9nADCBQbrRywlA88psGsq21xcsU="],
)
def test_rc_json_encrypted_multi(app, client, get_message):
    # Test recovery codes with encryption option and multiple keys
    # gal has two-factor already setup for 'sms'
    from cryptography.fernet import Fernet

    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    tf_authenticate(app, client)

    response = client.post("/mf-recovery-codes", headers=headers)
    codes = response.json["response"]["recovery_codes"]
    assert len(codes) == 5
    logout(client)

    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]

    response = client.post("/mf-recovery", json=dict(code=codes[0]))
    assert response.status_code == 200

    # verify actually logged in
    response = client.get("/profile", follow_redirects=False)
    assert response.status_code == 200
    logout(client)

    # add a new key to cryptor and verify old code still works
    key2 = Fernet.generate_key()
    newkeys = [key2, b"6_WhDTwI1RKJ3_ra9nADCBQbrRywlA88psGsq21xcsU="]
    app.security.mf_recovery_codes_util.setup_cryptor(newkeys)

    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]
    response = client.post("/mf-recovery", json=dict(code=codes[1]))
    assert response.status_code == 200
    logout(client)

    # creat codes for new user - this should use the new primary key.
    response = client.post(
        "/login", json=dict(email="matt@lp.com", password="password")
    )
    setup_tf_sms(client)
    response = client.post("/mf-recovery-codes", headers=headers)
    matt_codes = response.json["response"]["recovery_codes"]
    logout(client)

    # Now remove older key and codes for gal@lp.com shouldn't work
    app.security._mf_recovery_codes_util.setup_cryptor([key2])
    response = client.post("/login", json=dict(email="gal@lp.com", password="password"))
    assert response.json["response"]["tf_required"]
    response = client.post("/mf-recovery", json=dict(code=codes[2]))
    assert response.status_code == 400
    assert response.json["response"]["errors"][0].encode("utf-8") == get_message(
        "INVALID_RECOVERY_CODE"
    )

    # matts codes should work
    response = client.post(
        "/login", json=dict(email="matt@lp.com", password="password")
    )
    assert response.json["response"]["tf_required"]
    response = client.post("/mf-recovery", json=dict(code=matt_codes[0]))
    assert response.status_code == 200