Debian Codegraph @

tests/test_hashing.py

raw
1 """
2 test_hashing
3 ~~~~~~~~~~~~
4
5 hashing tests
6
7 :copyright: (c) 2019-2025 by J. Christopher Wagner (jwag).
8 :license: MIT, see LICENSE for more details.
9 """
10
11 import timeit
12
13 import pytest
14 from pytest import raises
15 from tests.test_utils import authenticate, init_app_with_options
16 from passlib.hash import argon2, pbkdf2_sha256, django_pbkdf2_sha256, plaintext
17
18 from flask_security.core import _get_pwd_context, _get_hashing_context
19 from flask_security.utils import (
20 hash_password,
21 verify_password,
22 get_hmac,
23 verify_and_update_password,
24 )
25
26
27 def test_verify_password_double_hash_argon2(app, sqlalchemy_datastore):
28 init_app_with_options(
29 app,
30 sqlalchemy_datastore,
31 **{
32 "SECURITY_PASSWORD_HASH": "argon2",
33 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 1},
34 "SECURITY_PASSWORD_SALT": "salty",
35 "SECURITY_PASSWORD_SINGLE_HASH": False,
36 },
37 )
38 with app.app_context():
39 hashed_pwd = hash_password("pass")
40 assert verify_password("pass", hashed_pwd)
41 assert "t=1" in hashed_pwd
42
43 # Verify double hash
44 assert verify_password("pass", argon2.hash(get_hmac("pass")))
45
46
47 def test_verify_password_double_hash_bcrypt(app, sqlalchemy_datastore):
48 init_app_with_options(
49 app,
50 sqlalchemy_datastore,
51 **{
52 "SECURITY_PASSWORD_HASH": "bcrypt",
53 "SECURITY_PASSWORD_SALT": "salty",
54 "SECURITY_PASSWORD_SINGLE_HASH": False,
55 },
56 )
57 with app.app_context():
58 hashed_pwd = hash_password("pass")
59 assert verify_password("pass", hashed_pwd)
60 assert hashed_pwd.startswith("$2")
61
62
63 def test_verify_password_single_hash(app, sqlalchemy_datastore):
64 init_app_with_options(
65 app,
66 sqlalchemy_datastore,
67 **{
68 "SECURITY_PASSWORD_HASH": "argon2",
69 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 1},
70 "SECURITY_PASSWORD_SALT": None,
71 "SECURITY_PASSWORD_SINGLE_HASH": True,
72 },
73 )
74 with app.app_context():
75 assert verify_password("pass", hash_password("pass"))
76
77
78 def test_verify_password_single_hash_list(app, sqlalchemy_datastore):
79 init_app_with_options(
80 app,
81 sqlalchemy_datastore,
82 **{
83 "SECURITY_PASSWORD_HASH": "argon2",
84 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 1},
85 "SECURITY_PASSWORD_SALT": "salty",
86 "SECURITY_PASSWORD_SINGLE_HASH": ["django_pbkdf2_sha256", "plaintext"],
87 "SECURITY_PASSWORD_SCHEMES": [
88 "argon2",
89 "pbkdf2_sha256",
90 "django_pbkdf2_sha256",
91 "plaintext",
92 ],
93 },
94 )
95 with app.app_context():
96 # double hash
97 assert verify_password("pass", hash_password("pass"))
98 assert verify_password("pass", pbkdf2_sha256.hash(get_hmac("pass")))
99 # single hash
100 assert verify_password("pass", django_pbkdf2_sha256.hash("pass"))
101 assert verify_password("pass", plaintext.hash("pass"))
102
103
104 def test_verify_password_backward_compatibility(app, sqlalchemy_datastore):
105 init_app_with_options(
106 app,
107 sqlalchemy_datastore,
108 **{
109 "SECURITY_PASSWORD_HASH": "argon2",
110 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 1},
111 "SECURITY_PASSWORD_SINGLE_HASH": False,
112 "SECURITY_PASSWORD_SCHEMES": ["argon2", "plaintext"],
113 },
114 )
115 with app.app_context():
116 # double hash
117 assert verify_password("pass", hash_password("pass"))
118 # single hash
119 assert verify_password("pass", plaintext.hash("pass"))
120
121
122 def test_login_with_bcrypt_enabled(app, sqlalchemy_datastore):
123 init_app_with_options(
124 app,
125 sqlalchemy_datastore,
126 **{
127 "SECURITY_PASSWORD_HASH": "bcrypt",
128 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {
129 "bcrypt__rounds": 4, # minimum so test is faster
130 },
131 "SECURITY_PASSWORD_SALT": "salty",
132 "SECURITY_PASSWORD_SINGLE_HASH": False,
133 },
134 )
135 response = authenticate(app.test_client(), follow_redirects=True)
136 assert b"Home Page" in response.data
137
138 # Above created all accounts using bcrypt - now change over to argon
139 # and verify that verify_and_update_password works
140 app.config["SECURITY_PASSWORD_HASH"] = "argon2"
141 app.config["SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS"] = dict(argon2__rounds=1)
142 app.config["SECURITY_PASSWORD_SCHEMES"] = ["argon2", "bcrypt"]
143 app.security.pwd_context = _get_pwd_context(app)
144 app.security.hashing_context = _get_hashing_context(app)
145 with app.app_context():
146 user1 = sqlalchemy_datastore.find_user(email="[email protected]")
147 assert app.security.pwd_context.identify(user1.password) == "bcrypt"
148 assert verify_and_update_password("password", user1)
149 # above should have rewritten password to argon1
150 sqlalchemy_datastore.commit()
151 assert verify_and_update_password("password", user1)
152 user1 = sqlalchemy_datastore.find_user(email="[email protected]")
153 assert app.security.pwd_context.identify(user1.password) == "argon2"
154
155
156 def test_missing_hash_salt_option(app, sqlalchemy_datastore):
157 with raises(RuntimeError):
158 init_app_with_options(
159 app,
160 sqlalchemy_datastore,
161 **{
162 "SECURITY_PASSWORD_HASH": "argon2",
163 "SECURITY_PASSWORD_SALT": None,
164 "SECURITY_PASSWORD_SINGLE_HASH": False,
165 },
166 )
167
168
169 def test_verify_password_argon2_opts(app, sqlalchemy_datastore):
170 init_app_with_options(
171 app,
172 sqlalchemy_datastore,
173 **{
174 "SECURITY_PASSWORD_HASH": "argon2",
175 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {
176 "argon2__rounds": 1,
177 "argon2__salt_size": 16,
178 "argon2__hash_len": 16,
179 },
180 },
181 )
182 with app.app_context():
183 hashed_pwd = hash_password("pass")
184 assert "t=1" in hashed_pwd
185 assert verify_password("pass", hashed_pwd)
186
187
188 @pytest.mark.skip
189 def test_bcrypt_speed(app, sqlalchemy_datastore):
190 init_app_with_options(
191 app,
192 sqlalchemy_datastore,
193 **{
194 "SECURITY_PASSWORD_HASH": "bcrypt",
195 "SECURITY_PASSWORD_SALT": "salty",
196 "SECURITY_PASSWORD_SINGLE_HASH": False,
197 },
198 )
199 with app.app_context():
200 print(timeit.timeit(lambda: hash_password("pass"), number=100))
201
202
203 @pytest.mark.skip
204 def test_argon2_speed(app, sqlalchemy_datastore):
205 init_app_with_options(
206 app,
207 sqlalchemy_datastore,
208 **{
209 "SECURITY_PASSWORD_HASH": "argon2",
210 "SECURITY_PASSWORD_HASH_PASSLIB_OPTIONS": {"argon2__rounds": 10},
211 },
212 )
213 with app.app_context():
214 print(
215 "Hash time for {} iterations: {}".format(
216 100, timeit.timeit(lambda: hash_password("pass"), number=100)
217 )
218 )