|
9 | 9 | from uuid import uuid4 |
10 | 10 |
|
11 | 11 | import jwskate |
12 | | -from attrs import define, field, frozen, setters |
| 12 | +from attrs import asdict, define, field, frozen, setters |
13 | 13 | from binapy import BinaPy |
14 | 14 | from furl import furl # type: ignore[import-untyped] |
15 | 15 | from requests import codes |
16 | 16 | from typing_extensions import Self |
17 | 17 |
|
18 | | -from .tokens import AccessTokenTypes, BearerToken, IdToken, id_token_converter |
| 18 | +from .tokens import AccessTokenTypes, BearerToken, BearerTokenSerializer, IdToken, id_token_converter |
19 | 19 | from .utils import accepts_expires_in |
20 | 20 |
|
21 | 21 | if TYPE_CHECKING: |
@@ -349,6 +349,71 @@ def handle_rs_provided_dpop_nonce(self, response: requests.Response) -> None: |
349 | 349 | self.rs_nonce = nonce |
350 | 350 |
|
351 | 351 |
|
| 352 | +class DPoPTokenSerializer(BearerTokenSerializer): |
| 353 | + """A helper class to serialize `DPoPToken`s. |
| 354 | +
|
| 355 | + This may be used to store DPoPTokens in session or cookies. |
| 356 | +
|
| 357 | + It needs a `dumper` and a `loader` functions that will respectively serialize and deserialize |
| 358 | + DPoPTokens. Default implementations are provided with use gzip and base64url on the serialized |
| 359 | + JSON representation. |
| 360 | +
|
| 361 | + Args: |
| 362 | + dumper: a function to serialize a token into a `str`. |
| 363 | + loader: a function to deserialize a serialized token representation. |
| 364 | +
|
| 365 | + """ |
| 366 | + |
| 367 | + @staticmethod |
| 368 | + def default_dumper(token: DPoPToken) -> str: |
| 369 | + """Serialize a token as JSON, then compress with deflate, then encodes as base64url. |
| 370 | +
|
| 371 | + WARNING: This does not serialize custom `jti_generator`, `iat_generator` or `dpop_token_class` in `DPoPKey`! |
| 372 | +
|
| 373 | + Args: |
| 374 | + token: the `DPoPToken` to serialize |
| 375 | +
|
| 376 | + Returns: |
| 377 | + the serialized value |
| 378 | +
|
| 379 | + """ |
| 380 | + d = asdict(token) |
| 381 | + d.update(**d.pop("kwargs", {})) |
| 382 | + d["dpop_key"]["private_key"] = token.dpop_key.private_key.to_pem() |
| 383 | + d["dpop_key"].pop("jti_generator", None) |
| 384 | + d["dpop_key"].pop("iat_generator", None) |
| 385 | + d["dpop_key"].pop("dpop_token_class", None) |
| 386 | + return ( |
| 387 | + BinaPy.serialize_to("json", {k: w for k, w in d.items() if w is not None}).to("deflate").to("b64u").ascii() |
| 388 | + ) |
| 389 | + |
| 390 | + @staticmethod |
| 391 | + def default_loader(serialized: str, token_class: type[DPoPToken] = DPoPToken) -> DPoPToken: |
| 392 | + """Deserialize a `DPoPToken`. |
| 393 | +
|
| 394 | + This does the opposite operations than `default_dumper`. |
| 395 | +
|
| 396 | + Args: |
| 397 | + serialized: the serialized token |
| 398 | + token_class: class to use to deserialize the Token |
| 399 | +
|
| 400 | + Returns: |
| 401 | + a DPoPToken |
| 402 | +
|
| 403 | + """ |
| 404 | + attrs = BinaPy(serialized).decode_from("b64u").decode_from("deflate").parse_from("json") |
| 405 | + |
| 406 | + expires_at = attrs.get("expires_at") |
| 407 | + if expires_at: |
| 408 | + attrs["expires_at"] = datetime.fromtimestamp(expires_at, tz=timezone.utc) |
| 409 | + |
| 410 | + if dpop_key := attrs.pop("dpop_key", None): |
| 411 | + dpop_key["private_key"] = jwskate.Jwk.from_pem(dpop_key["private_key"]) |
| 412 | + attrs["_dpop_key"] = DPoPKey(**dpop_key) |
| 413 | + |
| 414 | + return token_class(**attrs) |
| 415 | + |
| 416 | + |
352 | 417 | def validate_dpop_proof( # noqa: C901 |
353 | 418 | proof: str | bytes, |
354 | 419 | *, |
|
0 commit comments