-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.py
42 lines (31 loc) · 994 Bytes
/
jwt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio
import pydantic
from jwcrypto import jwk, jwt
from fasteda import adapter, app, entity, middleware
private_key = jwk.JWK.generate(kty="RSA", size=1024)
public_key = jwk.JWK.from_json(private_key.export_public())
apps = app.FastEDA(
adapter=adapter.pydantic,
middlewares=[
middleware.JWTValidate(public_key),
middleware.JWTExtractPayload(),
middleware.DatabusExtractVersion("1.0.0"),
],
)
class Client(pydantic.BaseModel):
id: int
name: str
@apps.add("client.create.v1")
def create_client(client: Client) -> None:
print(f"Client {client.id} created") # noqa: T201
if __name__ == "__main__":
msg = {"1.0.0": {"id": 1, "name": "John Doe"}}
token = jwt.JWT(header={"alg": "RS256"}, claims=msg)
token.make_signed_token(private_key)
body = token.serialize()
event = entity.Event(
topic="client.create.v1",
headers={},
value=body.encode(),
)
asyncio.run(apps.handle(event))