14
14
from cryptography import x509
15
15
from cryptography .hazmat .primitives .hashes import SHA256
16
16
from jwcrypto import jwk , jws , jwt
17
+ from jwcrypto .common import base64url_encode
17
18
from starlette .testclient import TestClient
18
19
19
20
from auth_server .api import init_auth_server_api
@@ -337,6 +338,12 @@ def test_transaction_jwsd(self):
337
338
}
338
339
339
340
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
340
347
_jws = jws .JWS (payload = payload )
341
348
_jws .add_signature (
342
349
key = self .client_jwk ,
@@ -346,7 +353,7 @@ def test_transaction_jwsd(self):
346
353
347
354
# Remove payload from serialized jws
348
355
header , _ , signature = data .split ("." )
349
- client_header = {"Detached-JWS" : f"{ header } ..{ signature } " }
356
+ client_header = {"Detached-JWS" : f"{ header } .{ payload_hash } .{ signature } " }
350
357
351
358
response = self .client .post (
352
359
"/transaction" , content = req .model_dump_json (exclude_unset = True ), headers = client_header
@@ -1168,7 +1175,14 @@ def test_transaction_jwsd_continue(self):
1168
1175
"uri" : "http://testserver/transaction" ,
1169
1176
"created" : int (utc_now ().timestamp ()),
1170
1177
}
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 )
1172
1186
_jws .add_signature (
1173
1187
key = self .client_jwk ,
1174
1188
protected = json .dumps (jws_header ),
@@ -1177,7 +1191,7 @@ def test_transaction_jwsd_continue(self):
1177
1191
1178
1192
# Remove payload from serialized jws
1179
1193
header , _ , signature = data .split ("." )
1180
- client_header = {"Detached-JWS" : f"{ header } ..{ signature } " }
1194
+ client_header = {"Detached-JWS" : f"{ header } .{ payload_hash } .{ signature } " }
1181
1195
1182
1196
response = self .client .post (
1183
1197
"/transaction" , content = req .model_dump_json (exclude_unset = True ), headers = client_header
@@ -1213,7 +1227,11 @@ def test_transaction_jwsd_continue(self):
1213
1227
# calculate ath header value
1214
1228
access_token_hash = hash_with (SHA256 (), continue_response ["access_token" ]["value" ].encode ())
1215
1229
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 )
1217
1235
_jws .add_signature (
1218
1236
key = self .client_jwk ,
1219
1237
protected = json .dumps (jws_header ),
@@ -1222,11 +1240,11 @@ def test_transaction_jwsd_continue(self):
1222
1240
1223
1241
# Remove payload from serialized jws
1224
1242
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 } " }
1226
1244
1227
1245
authorization_header = f'GNAP { continue_response ["access_token" ]["value" ]} '
1228
1246
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 )
1230
1248
1231
1249
assert response .status_code == 200
1232
1250
assert "access_token" in response .json ()
0 commit comments