-
-
Notifications
You must be signed in to change notification settings - Fork 800
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
fix: prompt=none shows a login screen #1361
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.conf import settings | ||
from django.contrib.auth.middleware import AuthenticationMiddleware | ||
from django.contrib.sessions.middleware import SessionMiddleware | ||
|
||
from oauth2_provider.oauth2_validators import OAuth2Validator | ||
|
||
|
||
# get_response is required for middlware, it doesn't need to do anything | ||
# the way we're using it, so we just use a lambda that returns None | ||
def get_response(): | ||
None | ||
|
||
|
||
class CustomOAuth2Validator(OAuth2Validator): | ||
def validate_silent_login(self, request) -> None: | ||
# request is an OAuthLib.common.Request and doesn't have the session | ||
# or user of the django request. We will emulate the session and auth | ||
# middleware here, since that is what the idp is using for auth. You | ||
# may need to modify this if you are using a different session | ||
# middleware or auth backend. | ||
|
||
session_cookie_name = settings.SESSION_COOKIE_NAME | ||
HTTP_COOKIE = request.headers.get("HTTP_COOKIE") | ||
COOKIES = HTTP_COOKIE.split("; ") | ||
for cookie in COOKIES: | ||
cookie_name, cookie_value = cookie.split("=") | ||
if cookie.startswith(session_cookie_name): | ||
break | ||
session_middleware = SessionMiddleware(get_response) | ||
session = session_middleware.SessionStore(cookie_value) | ||
# add session to request for compatibility with django.contrib.auth | ||
request.session = session | ||
|
||
# call the auth middleware to set request.user | ||
auth_middleware = AuthenticationMiddleware(get_response) | ||
auth_middleware.process_request(request) | ||
return request.user.is_authenticated | ||
|
||
def validate_silent_authorization(self, request) -> None: | ||
return True |
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 |
---|---|---|
|
@@ -545,6 +545,33 @@ def test_code_post_auth_fails_when_redirect_uri_path_is_invalid(self): | |
|
||
@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW) | ||
class TestOIDCAuthorizationCodeView(BaseTest): | ||
def test_login(self): | ||
""" | ||
Test login page is rendered if user is not authenticated | ||
""" | ||
self.oauth2_settings.PKCE_REQUIRED = False | ||
|
||
query_data = { | ||
"client_id": self.application.client_id, | ||
"response_type": "code", | ||
"state": "random_state_string", | ||
"scope": "openid", | ||
"redirect_uri": "http://example.org", | ||
} | ||
path = reverse("oauth2_provider:authorize") | ||
response = self.client.get(path, data=query_data) | ||
# The authorization view redirects to the login page with the | ||
self.assertEqual(response.status_code, 302) | ||
scheme, netloc, path, params, query, fragment = urlparse(response["Location"]) | ||
self.assertEqual(path, settings.LOGIN_URL) | ||
parsed_query = parse_qs(query) | ||
next = parsed_query["next"][0] | ||
self.assertIn(f"client_id={self.application.client_id}", next) | ||
self.assertIn("response_type=code", next) | ||
self.assertIn("state=random_state_string", next) | ||
self.assertIn("scope=openid", next) | ||
self.assertIn("redirect_uri=http%3A%2F%2Fexample.org", next) | ||
|
||
def test_id_token_skip_authorization_completely(self): | ||
""" | ||
If application.skip_authorization = True, should skip the authorization page. | ||
|
@@ -645,6 +672,33 @@ def test_prompt_login(self): | |
|
||
self.assertNotIn("prompt=login", next) | ||
|
||
def test_prompt_none_unauthorized(self): | ||
""" | ||
Test response for redirect when supplied with prompt: none | ||
|
||
Should redirect to redirect_uri with an error of login_required | ||
""" | ||
self.oauth2_settings.PKCE_REQUIRED = False | ||
|
||
query_data = { | ||
"client_id": self.application.client_id, | ||
"response_type": "code", | ||
"state": "random_state_string", | ||
"scope": "read write", | ||
"redirect_uri": "http://example.org", | ||
"prompt": "none", | ||
} | ||
|
||
response = self.client.get(reverse("oauth2_provider:authorize"), data=query_data) | ||
|
||
self.assertEqual(response.status_code, 302) | ||
|
||
scheme, netloc, path, params, query, fragment = urlparse(response["Location"]) | ||
parsed_query = parse_qs(query) | ||
|
||
self.assertIn("login_required", parsed_query["error"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use |
||
self.assertIn("random_state_string", parsed_query["state"]) | ||
|
||
|
||
class BaseAuthorizationCodeTokenView(BaseTest): | ||
def get_auth(self, scope="read write"): | ||
|
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.
Maybe add a query parameter, or add another test with a query parameter in the redirect_url to test the
seperator
computed line 268 inhandle_no_permission()