Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add issuer monitoring #325

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Added
~~~~~

* Added ``jwt_auth_check_symmetric_key``, ``jwt_auth_asymmetric_verified``, ``jwt_auth_symmetric_verified``, and ``jwt_auth_verification_failed`` custom attributes to aid in deprecation and removal of symmetric keys.
* Added ``jwt_auth_issuer`` and ``jwt_auth_issuer_verification`` custom attributes.

Changed
~~~~~~~
Expand Down
13 changes: 12 additions & 1 deletion edx_rest_framework_extensions/auth/jwt/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,19 @@ def _decode_and_verify_token(token, jwt_issuer):

# TODO (ARCH-204): verify issuer manually until it is properly configured.
token_issuer = decoded_token.get('iss')
# .. custom_attribute_name: jwt_auth_issuer
# .. custom_attribute_description: Value set to the JWT auth issuer.
set_custom_attribute('jwt_auth_issuer', token_issuer)
robrap marked this conversation as resolved.
Show resolved Hide resolved
issuer_matched = any(issuer['ISSUER'] == token_issuer for issuer in get_jwt_issuers())
if not issuer_matched:
if token_issuer == jwt_issuer['ISSUER']:
# .. custom_attribute_name: jwt_auth_issuer_verification
# .. custom_attribute_description: Depending on issuer verification, the value will
# be one of: matches-first-issuer, matches-later-issuer, or no-match.
set_custom_attribute('jwt_auth_issuer_verification', 'matches-first-issuer')
elif issuer_matched:
set_custom_attribute('jwt_auth_issuer_verification', 'matches-later-issuer')
else:
set_custom_attribute('jwt_auth_issuer_verification', 'no-match')
logger.info('Token decode failed due to mismatched issuer [%s]', token_issuer)
raise jwt.InvalidTokenError('%s is not a valid issuer.' % token_issuer)

Expand Down
6 changes: 6 additions & 0 deletions edx_rest_framework_extensions/auth/jwt/tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,21 @@ def test_keyset_size_and_other_monitoring(self, mock_set_custom_attribute):
mock.call('jwt_auth_check_symmetric_key', True),
mock.call('jwt_auth_verify_asymmetric_keys_count', 1),
mock.call('jwt_auth_asymmetric_verified', True),
mock.call('jwt_auth_issuer', 'test-issuer-1'),
mock.call('jwt_auth_issuer_verification', 'matches-first-issuer'),

mock.call('jwt_auth_check_symmetric_key', False),
mock.call('jwt_auth_verify_asymmetric_keys_count', 1),
mock.call('jwt_auth_asymmetric_verified', True),
mock.call('jwt_auth_issuer', 'test-issuer-1'),
mock.call('jwt_auth_issuer_verification', 'matches-first-issuer'),

mock.call('jwt_auth_check_symmetric_key', True),
mock.call('jwt_auth_verify_asymmetric_keys_count', 1),
mock.call('jwt_auth_verify_all_keys_count', 2),
mock.call('jwt_auth_symmetric_verified', True),
mock.call('jwt_auth_issuer', 'test-issuer-1'),
mock.call('jwt_auth_issuer_verification', 'matches-first-issuer'),
]


Expand Down