Skip to content

Commit 56b93c9

Browse files
committed
Add DPoP key support to AuthorizationRequestSerializer (#119)
1 parent e0ce730 commit 56b93c9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

requests_oauth2client/authorization_request.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ def default_dumper(azr: AuthorizationRequest) -> str:
940940
941941
"""
942942
d = asdict(azr)
943+
if azr.dpop_key:
944+
d["dpop_key"]["private_key"] = azr.dpop_key.private_key.to_pem()
943945
d.update(**d.pop("kwargs", {}))
944946
return BinaPy.serialize_to("json", d).to("deflate").to("b64u").ascii()
945947

@@ -961,6 +963,14 @@ def default_loader(
961963
962964
"""
963965
args = BinaPy(serialized).decode_from("b64u").decode_from("deflate").parse_from("json")
966+
967+
if dpop_key := args.get("dpop_key"):
968+
dpop_key["private_key"] = Jwk.from_pem(dpop_key["private_key"])
969+
dpop_key.pop("jti_generator", None)
970+
dpop_key.pop("iat_generator", None)
971+
dpop_key.pop("dpop_token_class", None)
972+
args["dpop_key"] = DPoPKey(**dpop_key)
973+
964974
return azr_class(**args)
965975

966976
def dumps(self, azr: AuthorizationRequest) -> str:

tests/unit_tests/test_authorization_request.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
AuthorizationRequestSerializer,
1313
AuthorizationResponse,
1414
AuthorizationResponseError,
15+
DPoPKey,
1516
InvalidMaxAgeParam,
1617
MismatchingIssuer,
1718
MismatchingState,
@@ -193,6 +194,23 @@ def test_authorization_request_serializer(authorization_request: AuthorizationRe
193194
assert serializer.loads(serialized) == authorization_request
194195

195196

197+
def test_authorization_request_serializer_with_dpop_key() -> None:
198+
dpop_key = DPoPKey.generate()
199+
authorization_request = AuthorizationRequest(
200+
"https://as.local/authorize",
201+
client_id="foo",
202+
redirect_uri="http://localhost/local",
203+
scope="openid",
204+
dpop_key=dpop_key,
205+
)
206+
207+
serialized = AuthorizationRequestSerializer.default_dumper(authorization_request)
208+
deserialized_request = AuthorizationRequestSerializer.default_loader(serialized)
209+
210+
assert isinstance(deserialized_request.dpop_key, DPoPKey)
211+
assert deserialized_request.dpop_key.private_key == dpop_key.private_key
212+
213+
196214
def test_request_acr_values() -> None:
197215
# you may provide acr_values as a space separated list or as a real list
198216
assert AuthorizationRequest(

0 commit comments

Comments
 (0)