-
Notifications
You must be signed in to change notification settings - Fork 814
Allow custom uri schemes #155
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
Merged
synasius
merged 2 commits into
django-oauth:master
from
RodneyRichardson:allow-custom-uri-schemes
Jan 15, 2015
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 hidden or 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ Stéphane Raimbault | |
Emanuele Palazzetti | ||
David Fischer | ||
Ash Christopher | ||
Rodney Richardson |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '0.7.2' | ||
__version__ = '0.7.3' | ||
|
||
__author__ = "Massimiliano Pippi & Federico Frenguelli" | ||
|
||
|
This file contains hidden or 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,9 @@ | ||
from django.http import HttpResponseRedirect | ||
|
||
from .settings import oauth2_settings | ||
|
||
|
||
class HttpResponseUriRedirect(HttpResponseRedirect): | ||
def __init__(self, redirect_to, *args, **kwargs): | ||
self.allowed_schemes = oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES | ||
super(HttpResponseUriRedirect, self).__init__(redirect_to, *args, **kwargs) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -32,9 +32,11 @@ def setUp(self): | |
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456") | ||
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456") | ||
|
||
oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES = ['http', 'custom-scheme'] | ||
|
||
self.application = Application( | ||
name="Test Application", | ||
redirect_uris="http://localhost http://example.com http://example.it", | ||
redirect_uris="http://localhost http://example.com http://example.it custom-scheme://example.com", | ||
user=self.dev_user, | ||
client_type=Application.CLIENT_CONFIDENTIAL, | ||
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE, | ||
|
@@ -92,6 +94,34 @@ def test_pre_auth_valid_client(self): | |
self.assertEqual(form['scope'].value(), "read write") | ||
self.assertEqual(form['client_id'].value(), self.application.client_id) | ||
|
||
def test_pre_auth_valid_client_custom_redirect_uri_scheme(self): | ||
""" | ||
Test response for a valid client_id with response_type: code | ||
using a non-standard, but allowed, redirect_uri scheme. | ||
""" | ||
self.client.login(username="test_user", password="123456") | ||
|
||
query_string = urlencode({ | ||
'client_id': self.application.client_id, | ||
'response_type': 'code', | ||
'state': 'random_state_string', | ||
'scope': 'read write', | ||
'redirect_uri': 'custom-scheme://example.com', | ||
}) | ||
url = "{url}?{qs}".format(url=reverse('oauth2_provider:authorize'), qs=query_string) | ||
|
||
response = self.client.get(url) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
# check form is in context and form params are valid | ||
self.assertIn("form", response.context) | ||
|
||
form = response.context["form"] | ||
self.assertEqual(form['redirect_uri'].value(), "custom-scheme://example.com") | ||
self.assertEqual(form['state'].value(), "random_state_string") | ||
self.assertEqual(form['scope'].value(), "read write") | ||
self.assertEqual(form['client_id'].value(), self.application.client_id) | ||
|
||
def test_pre_auth_approval_prompt(self): | ||
""" | ||
|
||
|
@@ -307,6 +337,49 @@ def test_code_post_auth_malicious_redirect_uri(self): | |
response = self.client.post(reverse('oauth2_provider:authorize'), data=form_data) | ||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_code_post_auth_allow_custom_redirect_uri_scheme(self): | ||
""" | ||
Test authorization code is given for an allowed request with response_type: code | ||
using a non-standard, but allowed, redirect_uri scheme. | ||
""" | ||
self.client.login(username="test_user", password="123456") | ||
|
||
form_data = { | ||
'client_id': self.application.client_id, | ||
'state': 'random_state_string', | ||
'scope': 'read write', | ||
'redirect_uri': 'custom-scheme://example.com', | ||
'response_type': 'code', | ||
'allow': True, | ||
} | ||
|
||
response = self.client.post(reverse('oauth2_provider:authorize'), data=form_data) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertIn('custom-scheme://example.com?', response['Location']) | ||
self.assertIn('state=random_state_string', response['Location']) | ||
self.assertIn('code=', response['Location']) | ||
|
||
def test_code_post_auth_deny_custom_redirect_uri_scheme(self): | ||
""" | ||
Test error when resource owner deny access | ||
using a non-standard, but allowed, redirect_uri scheme. | ||
""" | ||
self.client.login(username="test_user", password="123456") | ||
|
||
form_data = { | ||
'client_id': self.application.client_id, | ||
'state': 'random_state_string', | ||
'scope': 'read write', | ||
'redirect_uri': 'custom-scheme://example.com', | ||
'response_type': 'code', | ||
'allow': False, | ||
} | ||
|
||
response = self.client.post(reverse('oauth2_provider:authorize'), data=form_data) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertIn('custom-scheme://example.com?', response['Location']) | ||
self.assertIn("error=access_denied", response['Location']) | ||
|
||
|
||
class TestAuthorizationCodeTokenView(BaseTest): | ||
def get_auth(self): | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
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.
I'm not intimately familiar with this function, but is there a reason for using a custom regex instead of a custom validator based on
urlparse.urlparse
?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.
That's not my code - that's already in the master (I just fixed the scheme to match the RFC standard).
The call seems to handle the case where the network location has been internationalized (although there are no tests for that), so that may have something to do with it.
We DO need a RedirectURIValidator (which inherits from this), because the standard says that Redirect URIs cannot contain fragments.
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.
Looking at urlparse documentation, I can't see why this (or the safe equivalent for Python 2 and 3 compatibility) couldn't be used instead of a RegexValidator. Perhaps you might add this as a separate issue?
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.
Yeah,
urlparse
is what I used in my implementation (https://github.com/Locu/djoauth2/blob/9df7c3661e0a4c3585d3a333a63d5ed74472083c/djoauth2/authorization.py#L185):I'll take a look at this later today / tomorrow when I have the time.