Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_jwt(self):
token = RequestToken(id=1, scope="foo").save()
admin = RequestTokenAdmin(RequestToken, None)
self.assertEqual(admin.jwt(token), token.jwt())
token = RequestToken()
self.assertRaises(MissingRequiredClaimError, token.jwt)
self.assertEqual(admin.jwt(token), None)
def test_fail_issuer_missing(self):
payload = utils.jwt_payload_handler(self.user)
del payload['iss']
token = utils.jwt_encode_handler(payload)
with self.assertRaises(jwt.exceptions.MissingRequiredClaimError):
utils.jwt_decode_handler(token)
def test_decode_should_raise_error_if_iat_required_but_not_present(
self, jwt
):
payload = {
"some": "payload",
# iat not present
}
token = jwt.encode(payload, "secret")
with pytest.raises(MissingRequiredClaimError) as exc:
jwt.decode(token, "secret", options={"require_iat": True})
assert exc.value.claim == "iat"
token = token.decode()
bheader, bpayload, bsignature = token.split('.')
header = json.loads(bheader.decode())
payload = json.loads(bpayload.decode())
# claim check
payload_keys = list(payload.keys())
if 'jti' not in payload_keys:
raise exceptions.MissingRequiredClaimError('jti')
if 'exp' not in payload_keys:
raise exceptions.MissingRequiredClaimError('exp')
if 'type' not in payload_keys:
raise exceptions.MissingRequiredClaimError('type')
if 'identity' not in payload_keys:
raise exceptions.MissingRequiredClaimError('identity')
if 'iat' not in payload_keys:
raise exceptions.MissingRequiredClaimError('iat')
# signature compare
if bsignature is not make_signature(bheader, bpayload, secret):
raise exceptions.InvalidSignatureError('Invalid token signature')
# expire check
if payload['exp'] > int(time.time()):
raise exceptions.ExpiredSignatureError('Token has been expired')
return header, payload
def decode_token(token, secret):
if type(token) is bytes:
token = token.decode()
bheader, bpayload, bsignature = token.split('.')
header = json.loads(bheader.decode())
payload = json.loads(bpayload.decode())
# claim check
payload_keys = list(payload.keys())
if 'jti' not in payload_keys:
raise exceptions.MissingRequiredClaimError('jti')
if 'exp' not in payload_keys:
raise exceptions.MissingRequiredClaimError('exp')
if 'type' not in payload_keys:
raise exceptions.MissingRequiredClaimError('type')
if 'identity' not in payload_keys:
raise exceptions.MissingRequiredClaimError('identity')
if 'iat' not in payload_keys:
raise exceptions.MissingRequiredClaimError('iat')
# signature compare
if bsignature is not make_signature(bheader, bpayload, secret):
raise exceptions.InvalidSignatureError('Invalid token signature')
# expire check
if payload['exp'] > int(time.time()):
raise exceptions.ExpiredSignatureError('Token has been expired')
def decode_token(token, secret):
if type(token) is bytes:
token = token.decode()
bheader, bpayload, bsignature = token.split('.')
header = json.loads(bheader.decode())
payload = json.loads(bpayload.decode())
# claim check
payload_keys = list(payload.keys())
if 'jti' not in payload_keys:
raise exceptions.MissingRequiredClaimError('jti')
if 'exp' not in payload_keys:
raise exceptions.MissingRequiredClaimError('exp')
if 'type' not in payload_keys:
raise exceptions.MissingRequiredClaimError('type')
if 'identity' not in payload_keys:
raise exceptions.MissingRequiredClaimError('identity')
if 'iat' not in payload_keys:
raise exceptions.MissingRequiredClaimError('iat')
# signature compare
if bsignature is not make_signature(bheader, bpayload, secret):
raise exceptions.InvalidSignatureError('Invalid token signature')
# expire check
if payload['exp'] > int(time.time()):
raise exceptions.ExpiredSignatureError('Token has been expired')
return header, payload
bheader, bpayload, bsignature = token.split('.')
header = json.loads(bheader.decode())
payload = json.loads(bpayload.decode())
# claim check
payload_keys = list(payload.keys())
if 'jti' not in payload_keys:
raise exceptions.MissingRequiredClaimError('jti')
if 'exp' not in payload_keys:
raise exceptions.MissingRequiredClaimError('exp')
if 'type' not in payload_keys:
raise exceptions.MissingRequiredClaimError('type')
if 'identity' not in payload_keys:
raise exceptions.MissingRequiredClaimError('identity')
if 'iat' not in payload_keys:
raise exceptions.MissingRequiredClaimError('iat')
# signature compare
if bsignature is not make_signature(bheader, bpayload, secret):
raise exceptions.InvalidSignatureError('Invalid token signature')
# expire check
if payload['exp'] > int(time.time()):
raise exceptions.ExpiredSignatureError('Token has been expired')
return header, payload
def check_mandatory_claims(
payload: dict, claims: Sequence[str] = MANDATORY_CLAIMS
) -> None:
"""Check dict for mandatory claims."""
for claim in claims:
if claim not in payload:
raise exceptions.MissingRequiredClaimError(claim)