MCPcopy
hub / github.com/authlib/authlib / encode

Method encode

authlib/jose/rfc7519/jwt.py:53–78  ·  view source on GitHub ↗

Encode a JWT with the given header, payload and key. :param header: A dict of JWS header :param payload: A dict to be encoded :param key: key used to sign the signature :param check: check if sensitive data in payload :return: bytes

(self, header, payload, key, check=True)

Source from the content-addressed store, hash-verified

51 raise InsecureClaimError(k)
52
53 def encode(self, header, payload, key, check=True):
54 """Encode a JWT with the given header, payload and key.
55
56 :param header: A dict of JWS header
57 :param payload: A dict to be encoded
58 :param key: key used to sign the signature
59 :param check: check if sensitive data in payload
60 :return: bytes
61 """
62 header.setdefault("typ", "JWT")
63
64 for k in ["exp", "iat", "nbf"]:
65 # convert datetime into timestamp
66 claim = payload.get(k)
67 if isinstance(claim, datetime.datetime):
68 payload[k] = calendar.timegm(claim.utctimetuple())
69
70 if check:
71 self.check_sensitive_data(payload)
72
73 key = find_encode_key(key, header)
74 text = to_bytes(json_dumps(payload))
75 if "enc" in header:
76 return self._jwe.serialize_compact(header, text, key)
77 else:
78 return self._jws.serialize_compact(header, text, key)
79
80 def decode(self, s, key, claims_cls=None, claims_options=None, claims_params=None):
81 """Decode the JWT with the given key. This is similar with

Callers 15

test_init_algorithmsFunction · 0.95
test_use_jweFunction · 0.95
generateMethod · 0.80
encode_intFunction · 0.80
__call__Method · 0.80
encode_id_tokenMethod · 0.80
generate_id_tokenFunction · 0.80
to_bytesFunction · 0.80
test_missint_claimsFunction · 0.80

Calls 6

check_sensitive_dataMethod · 0.95
to_bytesFunction · 0.90
json_dumpsFunction · 0.90
find_encode_keyFunction · 0.85
getMethod · 0.45
serialize_compactMethod · 0.45