-
Notifications
You must be signed in to change notification settings - Fork 11
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
✨(backend) add option to configure list of required OIDC claims #525
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,3 +365,87 @@ def get_userinfo_mocked(*args): | |
klass.get_or_create_user(access_token="test-token", id_token=None, payload=None) | ||
|
||
assert models.User.objects.count() == 1 | ||
|
||
|
||
# Required claims | ||
|
||
|
||
@override_settings( | ||
OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo", | ||
USER_OIDC_REQUIRED_CLAIMS=["email", "sub", "address"], | ||
) | ||
@responses.activate | ||
def test_authentication_get_userinfo_required_claims_missing(): | ||
"""Ensure SuspiciousOperation is raised if required claims are missing.""" | ||
|
||
responses.add( | ||
responses.GET, | ||
re.compile(r".*/userinfo"), | ||
json={ | ||
"last_name": "Doe", | ||
"email": "[email protected]", | ||
}, | ||
status=200, | ||
) | ||
|
||
oidc_backend = OIDCAuthenticationBackend() | ||
|
||
with pytest.raises( | ||
SuspiciousOperation, match="Missing required claims in user info: sub, address" | ||
): | ||
oidc_backend.get_userinfo("fake_access_token", None, None) | ||
|
||
|
||
@override_settings( | ||
OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo", | ||
USER_OIDC_REQUIRED_CLAIMS=["email", "Sub"], | ||
) | ||
@responses.activate | ||
def test_authentication_get_userinfo_required_claims_case_sensitivity(): | ||
"""Ensure the system respects case sensitivity for required claims.""" | ||
|
||
responses.add( | ||
responses.GET, | ||
re.compile(r".*/userinfo"), | ||
json={ | ||
"sub": "123", | ||
"last_name": "Doe", | ||
"email": "[email protected]", | ||
}, | ||
status=200, | ||
) | ||
|
||
oidc_backend = OIDCAuthenticationBackend() | ||
|
||
with pytest.raises( | ||
SuspiciousOperation, match="Missing required claims in user info: Sub" | ||
): | ||
oidc_backend.get_userinfo("fake_access_token", None, None) | ||
|
||
|
||
@override_settings( | ||
OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo", | ||
USER_OIDC_REQUIRED_CLAIMS=["email", "sub"], | ||
) | ||
@responses.activate | ||
def test_authentication_get_userinfo_required_claims_success(): | ||
"""Ensure user is authenticated when required claims are present.""" | ||
|
||
responses.add( | ||
responses.GET, | ||
re.compile(r".*/userinfo"), | ||
json={ | ||
"sub": "123", | ||
"last_name": "Doe", | ||
"email": "[email protected]", | ||
}, | ||
status=200, | ||
) | ||
|
||
oidc_backend = OIDCAuthenticationBackend() | ||
result = oidc_backend.get_userinfo("fake_access_token", None, None) | ||
|
||
assert result["sub"] == "123" | ||
assert result.get("first_name") is None | ||
assert result["last_name"] == "Doe" | ||
assert result["email"] == "[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
according to the spec, these claims should be referred as
essential claims
. Please take a look here https://openid.net/specs/openid-connect-core-1_0.htmlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might prefer to override the base method, and call it in
get_or_create_user
, as proposed in the initial implementation.get_userinfo
should have a single responsibility, getting the data.https://github.com/mozilla/mozilla-django-oidc/blob/2c2334fdc9b2fc72a492b5f0e990b4c30de68363/mozilla_django_oidc/auth.py#L84
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! 🙏
Note that checking the presence of the "sub" claim was already wrong then 🤔
Let's fix that before it is released!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lebaudantoine Here you go: #531
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of, checking the presence of "sub" was naive and simple.
As soon as we introduce more complexity (a list check, a new setting, …) I would stick with the Mozilla django oidc philosophy