MCPcopy
hub / github.com/authlib/authlib / serialize_compact

Method serialize_compact

authlib/jose/rfc7515/jws.py:52–79  ·  view source on GitHub ↗

Generate a JWS Compact Serialization. The JWS Compact Serialization represents digitally signed or MACed content as a compact, URL-safe string, per `Section 7.1`_. .. code-block:: text BASE64URL(UTF8(JWS Protected Header)) || '.' || BASE64URL(JWS Pay

(self, protected, payload, key)

Source from the content-addressed store, hash-verified

50 cls.ALGORITHMS_REGISTRY[algorithm.name] = algorithm
51
52 def serialize_compact(self, protected, payload, key):
53 """Generate a JWS Compact Serialization. The JWS Compact Serialization
54 represents digitally signed or MACed content as a compact, URL-safe
55 string, per `Section 7.1`_.
56
57 .. code-block:: text
58
59 BASE64URL(UTF8(JWS Protected Header)) || '.' ||
60 BASE64URL(JWS Payload) || '.' ||
61 BASE64URL(JWS Signature)
62
63 :param protected: A dict of protected header
64 :param payload: A bytes/string of payload
65 :param key: Private key used to generate signature
66 :return: byte
67 """
68 jws_header = JWSHeader(protected, None)
69 self._validate_private_headers(protected)
70 self._validate_crit_headers(protected)
71 algorithm, key = self._prepare_algorithm_key(protected, payload, key)
72
73 protected_segment = json_b64encode(jws_header.protected)
74 payload_segment = urlsafe_b64encode(to_bytes(payload))
75
76 # calculate signature
77 signing_input = b".".join([protected_segment, payload_segment])
78 signature = urlsafe_b64encode(algorithm.sign(signing_input, key))
79 return b".".join([protected_segment, payload_segment, signature])
80
81 def deserialize_compact(self, s, key, decode=None):
82 """Exact JWS Compact Serialization, and validate with the given key.

Callers 5

serializeMethod · 0.95
encodeMethod · 0.45
test_invalid_assertionFunction · 0.45

Calls 8

json_b64encodeFunction · 0.90
urlsafe_b64encodeFunction · 0.90
to_bytesFunction · 0.90
JWSHeaderClass · 0.85
signMethod · 0.45

Tested by 3

test_invalid_assertionFunction · 0.36