Skip to content

Commit e224c29

Browse files
committed
update tests
1 parent 054885f commit e224c29

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/auth_server/tests/test_app.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from cryptography import x509
1515
from cryptography.hazmat.primitives.hashes import SHA256
1616
from jwcrypto import jwk, jws, jwt
17+
from jwcrypto.common import base64url_encode
1718
from starlette.testclient import TestClient
1819

1920
from auth_server.api import init_auth_server_api
@@ -337,6 +338,12 @@ def test_transaction_jwsd(self):
337338
}
338339

339340
payload = req.model_dump_json(exclude_unset=True)
341+
342+
# create a hash of payload to send in payload place
343+
payload_digest = hash_with(SHA256(), payload.encode())
344+
payload_hash = base64url_encode(payload_digest)
345+
346+
# create detached jws
340347
_jws = jws.JWS(payload=payload)
341348
_jws.add_signature(
342349
key=self.client_jwk,
@@ -346,7 +353,7 @@ def test_transaction_jwsd(self):
346353

347354
# Remove payload from serialized jws
348355
header, _, signature = data.split(".")
349-
client_header = {"Detached-JWS": f"{header}..{signature}"}
356+
client_header = {"Detached-JWS": f"{header}.{payload_hash}.{signature}"}
350357

351358
response = self.client.post(
352359
"/transaction", content=req.model_dump_json(exclude_unset=True), headers=client_header
@@ -1168,7 +1175,14 @@ def test_transaction_jwsd_continue(self):
11681175
"uri": "http://testserver/transaction",
11691176
"created": int(utc_now().timestamp()),
11701177
}
1171-
_jws = jws.JWS(payload=req.model_dump_json(exclude_unset=True))
1178+
1179+
payload = req.model_dump_json(exclude_unset=True)
1180+
1181+
# create a hash of payload to send in payload place
1182+
payload_digest = hash_with(SHA256(), payload.encode())
1183+
payload_hash = base64url_encode(payload_digest)
1184+
1185+
_jws = jws.JWS(payload=payload)
11721186
_jws.add_signature(
11731187
key=self.client_jwk,
11741188
protected=json.dumps(jws_header),
@@ -1177,7 +1191,7 @@ def test_transaction_jwsd_continue(self):
11771191

11781192
# Remove payload from serialized jws
11791193
header, _, signature = data.split(".")
1180-
client_header = {"Detached-JWS": f"{header}..{signature}"}
1194+
client_header = {"Detached-JWS": f"{header}.{payload_hash}.{signature}"}
11811195

11821196
response = self.client.post(
11831197
"/transaction", content=req.model_dump_json(exclude_unset=True), headers=client_header
@@ -1213,7 +1227,11 @@ def test_transaction_jwsd_continue(self):
12131227
# calculate ath header value
12141228
access_token_hash = hash_with(SHA256(), continue_response["access_token"]["value"].encode())
12151229
jws_header["ath"] = base64.urlsafe_b64encode(access_token_hash).decode("ascii").rstrip("=")
1216-
_jws = jws.JWS(payload="{}")
1230+
# create hash of empty payload to send in payload place
1231+
payload = "{}"
1232+
payload_digest = hash_with(SHA256(), payload.encode())
1233+
payload_hash = base64url_encode(payload_digest)
1234+
_jws = jws.JWS(payload=payload)
12171235
_jws.add_signature(
12181236
key=self.client_jwk,
12191237
protected=json.dumps(jws_header),
@@ -1222,11 +1240,11 @@ def test_transaction_jwsd_continue(self):
12221240

12231241
# Remove payload from serialized jws
12241242
continue_header, _, continue_signature = continue_data.split(".")
1225-
client_header = {"Detached-JWS": f"{continue_header}..{continue_signature}"}
1243+
client_header = {"Detached-JWS": f"{continue_header}.{payload_hash}.{continue_signature}"}
12261244

12271245
authorization_header = f'GNAP {continue_response["access_token"]["value"]}'
12281246
client_header["Authorization"] = authorization_header
1229-
response = self.client.post(continue_response["uri"], json=dict(), headers=client_header)
1247+
response = self.client.post(continue_response["uri"], content=payload, headers=client_header)
12301248

12311249
assert response.status_code == 200
12321250
assert "access_token" in response.json()

0 commit comments

Comments
 (0)