generated from martinthomson/internet-draft-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreferenced_token.py
46 lines (37 loc) · 1.03 KB
/
referenced_token.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
43
44
45
46
from datetime import datetime
from cbor2 import dumps
from cwt import COSE, COSEAlgs, COSEHeaders, COSEKey, CWTClaims
from jwcrypto import jwk
def CWT(
jwk: jwk.JWK,
iat: datetime,
sub: str,
iss: str,
status_url: str,
status_idx: int,
exp: datetime | None = None,
):
claims = {}
claims[CWTClaims.SUB] = sub
claims[CWTClaims.ISS] = iss
claims[CWTClaims.IAT] = int(iat.timestamp())
if exp is not None:
claims[CWTClaims.EXP] = int(exp.timestamp())
claims[65535] = {
"status_list": {
"idx": status_idx,
"uri": status_url,
}
}
protected_header = {}
unprotected_header = {}
if jwk.key_id:
unprotected_header[COSEHeaders.KID] = jwk.key_id.encode("utf-8")
protected_header[COSEHeaders.ALG] = COSEAlgs.ES256
key = COSEKey.from_jwk(jwk)
sender = COSE.new()
encoded = sender.encode(
dumps(claims), key, protected=protected_header, unprotected=unprotected_header
)
return encoded
return encoded