Skip to content

Commit

Permalink
Merge branch 'master' into bump_version
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiibuniak-okta authored Nov 16, 2021
2 parents 6ad883b + 52ead0b commit b1c2642
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 6 additions & 6 deletions okta_jwt_verifier/jwt_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ async def verify_access_token(self, token, claims_to_verify=('iss', 'aud', 'exp'
if headers.get('alg') != 'RS256':
raise JWTValidationException('Header claim "alg" is invalid.')

okta_jwk = await self.get_jwk(headers['kid'])
self.verify_signature(token, okta_jwk)

self.verify_claims(claims,
claims_to_verify=claims_to_verify,
leeway=self.leeway)

okta_jwk = await self.get_jwk(headers['kid'])
self.verify_signature(token, okta_jwk)
except JWTValidationException:
raise
except Exception as err:
Expand Down Expand Up @@ -125,13 +125,13 @@ async def verify_id_token(self, token, claims_to_verify=('iss', 'exp'), nonce=No
if headers.get('alg') != 'RS256':
raise JWTValidationException('Header claim "alg" is invalid.')

okta_jwk = await self.get_jwk(headers['kid'])
self.verify_signature(token, okta_jwk)

self.verify_claims(claims,
claims_to_verify=claims_to_verify,
leeway=self.leeway)

okta_jwk = await self.get_jwk(headers['kid'])
self.verify_signature(token, okta_jwk)

# verify client_id and nonce
self.verify_client_id(claims['aud'])
if 'nonce' in claims and claims['nonce'] != nonce:
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_jwt_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,36 @@ def test_verify_claims_invalid():
jwt_verifier.verify_claims(claims, ('iss', 'aud', 'exp'))


@pytest.mark.asyncio
async def test_invalid_claims_fail_first(mocker):
"""Check if claims are invalid, exception is raised and no network call is needed."""
client_id = 'test_client_id'
audience = 'api://default'
headers = {'alg': 'RS256', 'kid': 'test_kid'}
iss_time = time.time()
claims = {'ver': 1,
'jti': 'test_jti_str',
'iss': 'https://test_issuer.com',
'aud': audience,
'iat': iss_time,
'exp': iss_time+300,
'cid': client_id,
'uid': 'test_uid',
'scp': ['openid'],
'sub': '[email protected]'}
signing_input = 'test_signing_input'
signature = 'test_signature'
mock_parse_token = lambda token: (headers, claims, signing_input, signature)
mocker.patch('okta_jwt_verifier.jwt_utils.JWTUtils.parse_token', mock_parse_token)

token = 'test_token'
issuer = 'https://invalid_issuer.com'
jwt_verifier = AccessTokenVerifier(issuer)
with pytest.raises(JWTValidationException) as err:
await jwt_verifier.verify(token)
assert str(err.value) == 'Invalid issuer'


def test_verify_claims_missing_claim():
"""Check if method verify_claims raises an exception if required claim is missing."""
client_id = 'test_client_id'
Expand Down

0 comments on commit b1c2642

Please sign in to comment.