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