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)
| 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. |