Debian Codegraph @

docs/examples/datastructures/secrets/secret_header.py

raw
1 from __future__ import annotations
2
3 from dataclasses import dataclass
4 from secrets import compare_digest
5
6 from typing_extensions import Annotated
7
8 from litestar import get
9 from litestar.datastructures.secret_values import SecretString
10 from litestar.exceptions import NotAuthorizedException
11 from litestar.params import HeaderParameter
12
13 SECRET_VALUE = "super-secret" # An example secret value - this should be stored securely in production.
14
15
16 @dataclass
17 class Sensitive:
18 value: str
19
20
21 @get(sync_to_thread=False)
22 def get_handler(secret: Annotated[SecretString, HeaderParameter(name="x-secret")]) -> Sensitive:
23 if not compare_digest(secret.get_secret(), SECRET_VALUE):
24 raise NotAuthorizedException
25 return Sensitive(value="sensitive data")