-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpasskeys.py
256 lines (194 loc) · 7.49 KB
/
passkeys.py
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
import base64
import json
from base64 import urlsafe_b64encode
import copy
import pytest
import requests
from soft_webauthn import SoftWebauthnDevice
BASE_URL = "http://localhost:8080/rest"
# insert into data.user (use_name, password) values ('user_1', 'password-password');
USERNAME_1 = "alice"
USERNAME_2 = "bob"
PASSWORD = "pass"
@pytest.fixture(name="session")
def session_fixture():
return login(USERNAME_1, PASSWORD)
def login(user_name, password):
session = requests.Session()
login_data = {
"user_name": user_name,
"password": password,
}
response = session.post(f"{BASE_URL}/login", json=login_data)
assert response.status_code == 200, "Login failed"
return session
# Poor man's SETUP
def test_passkey_endpoint_delete(session):
res = session.delete(f"{BASE_URL}/passkeys")
assert res.status_code == 204
def test_login_success(session):
response = session.get(BASE_URL + "/recipes")
assert response.status_code == 200
def test_login_required_for_recipes():
response = requests.get(BASE_URL + "/recipes")
assert response.status_code == 401
# ----------------------|
# Passkey registration |
# ----------------------|
def test_registration_begin(session):
"""
Get passkey registration options from server by calling registration/begin
"""
response = session.get(BASE_URL + "/passkeys/registration/begin")
assert response.status_code == 200
res_json = response.json()
assert "rp" in res_json
assert "challenge" in res_json
assert "user" in res_json
def serialize_passkey(key):
s = copy.deepcopy(key)
s["id"] = s["id"].decode("utf-8").rstrip("=")
s["rawId"] = urlsafe_b64encode(s["rawId"]).decode("utf-8").rstrip("=")
s["response"]["clientDataJSON"] = base64.b64encode(
s["response"]["clientDataJSON"]
).decode("utf-8")
if "attestationObject" in s["response"]:
s["response"]["attestationObject"] = base64.b64encode(
s["response"]["attestationObject"]
).decode("utf-8")
if "signature" in s["response"]:
s["response"]["signature"] = base64.b64encode(
s["response"]["signature"]
).decode("utf-8")
if "authenticatorData" in s["response"]:
s["response"]["authenticatorData"] = base64.b64encode(
s["response"]["authenticatorData"]
).decode("utf-8")
return json.dumps(s)
# our fake device
device = SoftWebauthnDevice()
session_1 = login(USERNAME_1, PASSWORD)
session_2 = login(USERNAME_2, PASSWORD)
@pytest.fixture(params=[session_1, session_2])
def passkey_with_session(request):
"""
Fixture that creates a passkey using registration options from the server.
Returns (passkey, session)
"""
session = request.param
response = session.get(BASE_URL + "/passkeys/registration/begin")
assert response.status_code == 200
# decode response
res_json = response.json()
options = {"publicKey": res_json}
challenge = options["publicKey"]["challenge"]
options["publicKey"]["challenge"] = base64.urlsafe_b64decode(challenge + "==")
# create a passkey (aka public key credential - attestation) akin to `navigator.credentials.create()`
passkey = device.create(options, "http://localhost:1234")
return passkey, session
def test_registration_complete(passkey_with_session):
passkey, session = passkey_with_session
response = session.post(
f"{BASE_URL}/passkeys/registration/complete",
json={"credential": serialize_passkey(passkey), "name": "my passkey"},
)
body = response.json()
assert response.status_code == 200
assert "credential_id" in body
assert body["user_verified"] is False # soft webauthn performs no user verification
# get the passkeys from the server
passkey, session = passkey_with_session
res = session.get(f"{BASE_URL}/passkeys")
assert res.status_code == 200
body = res.json()
last_passkey = body[-1]
passkey_json = json.loads(serialize_passkey(passkey))
# check we can only see our own passkeys
user_ids = [x["user_id"] for x in body]
assert len(set(user_ids)) == 1
assert last_passkey["data"]["credential_id"] == passkey_json["id"]
assert last_passkey["name"] == "my passkey"
def test_bogus_registration_complete(session):
response = session.post(
f"{BASE_URL}/passkeys/registration/complete", json={"hej": False}
)
assert response.status_code == 400
assert response.json()
# ------------------------|
# Passkey authentication |
# ------------------------|
@pytest.fixture
def auth_options_w_session():
"""
Fixture to get authentication options from the server at /passkeys/authentication/begin
Returns the authentication options as well as the session associated (needed for the challenge)
"""
# the server needs to know the username in order to fetch allowed credentials
payload = {"user_name": USERNAME_1}
session = requests.Session()
response = session.post(f"{BASE_URL}/passkeys/authentication/begin", json=payload)
assert response.status_code == 200
body = response.json()
# Decode the challenge from base64-url and convert to bytes
challenge = base64.urlsafe_b64decode(body["challenge"] + "==")
# Update the body with the byte-encoded challenge
body["challenge"] = challenge
assert "challenge" in body
assert body["rpId"] == "localhost"
assert "allowCredentials" in body
assert len(body["allowCredentials"]) == 1
return {"publicKey": body}, session
def test_auth_begin_no_username(session):
res = session.post(
f"{BASE_URL}/passkeys/authentication/begin", json={"noop": True}
)
assert res.status_code == 200
assert res.json()
@pytest.fixture
def get_passkey_w_session(auth_options_w_session):
"""
Fixture to get the passkey from soft_webauthn device, also returns the session
"""
auth_options, session = auth_options_w_session
# get authentication credential aka assertion
passkey = device.get(options=auth_options, origin="http://localhost:1234")
assert passkey["type"] == "public-key", session
return passkey, session
def test_incomplete_passkey_auth(auth_options_w_session):
"""
The session we get from passkeys/begin should only allow us to call passkeys/complete, no other endpoints.
"""
_, session = auth_options_w_session
res = session.get(f"{BASE_URL}/recipes")
assert res.status_code == 401
def test_bogus_auth_complete(session):
res = session.post(
f"{BASE_URL}/passkeys/authentication/complete", json={"bogus": "hej"}
)
assert res.status_code == 400
assert res.json()
def test_authentication_complete(get_passkey_w_session):
passkey, session = get_passkey_w_session
payload = serialize_passkey(passkey)
response = session.post(
f"{BASE_URL}/passkeys/authentication/complete", json=payload
)
assert response.status_code == 200
response_json = response.json()
assert "me" in response_json
with pytest.raises(KeyError):
# token should be stripped by openresty
response_json["token"]
res = session.get(f"{BASE_URL}/recipes")
assert res.status_code == 200
assert res.json()
# check last_used_at has been updated
res = session.get(f"{BASE_URL}/passkeys")
assert res.status_code == 200
body = res.json()
assert body[1]["last_used_at"]
assert body[1]["data"]["sign_count"] == 1
# Poor man's teardown
def test_teardown(session):
res = session.delete(f"{BASE_URL}/passkeys")
assert res.status_code == 204