Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ David Smith
Diego Garcia
Dulmandakh Sukhbaatar
Dylan Giesler
Dylan Tack
Emanuele Palazzetti
Federico Dolce
Frederico Vieira
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [unreleased]
* Remove support for Django 3.0
* Add support for Django 3.2
* #981 redirect_uri is now required in authorization requests when multiple URIs are registered.

### Added
* #712, #636, #808. Calls to `django.contrib.auth.authenticate()` now pass a `request`
Expand Down
9 changes: 6 additions & 3 deletions oauth2_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.utils.translation import gettext_lazy as _
from jwcrypto import jwk
from jwcrypto.common import base64url_encode
from oauthlib.oauth2.rfc6749 import errors

from .generators import generate_client_id, generate_client_secret
from .scopes import get_scopes_backend
Expand Down Expand Up @@ -107,11 +108,13 @@ def __str__(self):
@property
def default_redirect_uri(self):
"""
Returns the default redirect_uri extracting the first item from
the :attr:`redirect_uris` string
Returns the default redirect_uri, *if* only one is registered.
"""
if self.redirect_uris:
return self.redirect_uris.split().pop(0)
uris = self.redirect_uris.split()
if len(uris) == 1:
return self.redirect_uris.split().pop(0)
raise errors.MissingRedirectURIError()

assert False, (
"If you are using implicit, authorization_code"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_authorization_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ def test_pre_auth_default_redirect(self):
Test for default redirect uri if omitted from query string with response_type: code
"""
self.client.login(username="test_user", password="123456")
self.application.redirect_uris = "http://localhost"
self.application.save()

query_data = {
"client_id": self.application.client_id,
Expand All @@ -269,6 +271,21 @@ def test_pre_auth_default_redirect(self):
form = response.context["form"]
self.assertEqual(form["redirect_uri"].value(), "http://localhost")

def test_pre_auth_missing_redirect(self):
"""
Test response if redirect_uri is missing and multiple URIs are registered.
@see https://datatracker.ietf.org/doc/html/rfc6749#section-3.1.2.3
"""
self.client.login(username="test_user", password="123456")

query_data = {
"client_id": self.application.client_id,
"response_type": "code",
}

response = self.client.get(reverse("oauth2_provider:authorize"), data=query_data)
self.assertEqual(response.status_code, 400)

def test_pre_auth_forbibben_redirect(self):
"""
Test error when passing a forbidden redirect_uri in query string with response_type: code
Expand All @@ -293,6 +310,7 @@ def test_pre_auth_wrong_response_type(self):
query_data = {
"client_id": self.application.client_id,
"response_type": "WRONG",
"redirect_uri": "http://example.org",
}

response = self.client.get(reverse("oauth2_provider:authorize"), data=query_data)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def test_pre_auth_default_redirect(self):
Test for default redirect uri if omitted from query string with response_type: code
"""
self.client.login(username="hy_test_user", password="123456")
self.application.redirect_uris = "http://localhost"
self.application.save()

query_string = urlencode(
{
Expand Down Expand Up @@ -413,6 +415,7 @@ def test_pre_auth_wrong_response_type(self):
{
"client_id": self.application.client_id,
"response_type": "WRONG",
"redirect_uri": "http://example.org",
}
)
url = "{url}?{qs}".format(url=reverse("oauth2_provider:authorize"), qs=query_string)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_implicit.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def test_pre_auth_default_redirect(self):
Test for default redirect uri if omitted from query string with response_type: token
"""
self.client.login(username="test_user", password="123456")
self.application.redirect_uris = "http://localhost"
self.application.save()

query_data = {
"client_id": self.application.client_id,
Expand Down