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

Disable 255 chars length limit for redirect uri (#902) #903

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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Jens Timmerman
Jerome Leclanche
Jim Graham
Paul Oswald
Pavel Tvrdík
pySilver
Rodney Richardson
Silvano Cerza
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]

* #898 Added the ability to customize classes for django admin
* #903 Disable `redirect_uri` field length limit for `AbstractGrant`

### Added
* #884 Added support for Python 3.9
Expand Down
18 changes: 18 additions & 0 deletions oauth2_provider/migrations/0003_auto_20201211_1314.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.4 on 2020-12-11 13:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('oauth2_provider', '0002_auto_20190406_1805'),
]

operations = [
migrations.AlterField(
model_name='grant',
MattBlack85 marked this conversation as resolved.
Show resolved Hide resolved
name='redirect_uri',
field=models.TextField(),
),
]
2 changes: 1 addition & 1 deletion oauth2_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class AbstractGrant(models.Model):
code = models.CharField(max_length=255, unique=True) # code comes from oauthlib
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)
expires = models.DateTimeField()
redirect_uri = models.CharField(max_length=255)
redirect_uri = models.TextField()
scope = models.TextField(blank=True)

created = models.DateTimeField(auto_now_add=True)
Expand Down
55 changes: 43 additions & 12 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
UserModel = get_user_model()


class TestModels(TestCase):
class BaseTestModels(TestCase):
def setUp(self):
self.user = UserModel.objects.create_user("test_user", "test@example.com", "123456")

def tearDown(self):
self.user.delete()


class TestModels(BaseTestModels):
def test_allow_scopes(self):
self.client.login(username="test_user", password="123456")
app = Application.objects.create(
Expand Down Expand Up @@ -103,10 +108,7 @@ def test_scopes_property(self):
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL="tests.SampleRefreshToken",
OAUTH2_PROVIDER_GRANT_MODEL="tests.SampleGrant",
)
class TestCustomModels(TestCase):
def setUp(self):
self.user = UserModel.objects.create_user("test_user", "test@example.com", "123456")

class TestCustomModels(BaseTestModels):
def test_custom_application_model(self):
"""
If a custom application model is installed, it should be present in
Expand Down Expand Up @@ -237,7 +239,21 @@ def test_custom_grant_model_not_installed(self):
oauth2_settings.GRANT_MODEL = "oauth2_provider.Grant"


class TestGrantModel(TestCase):
class TestGrantModel(BaseTestModels):
def setUp(self):
super().setUp()
self.application = Application.objects.create(
name="Test Application",
redirect_uris="",
user=self.user,
client_type=Application.CLIENT_CONFIDENTIAL,
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
)

def tearDown(self):
self.application.delete()
super().tearDown()

def test_str(self):
grant = Grant(code="test_code")
self.assertEqual("%s" % grant, grant.code)
Expand All @@ -247,11 +263,26 @@ def test_expires_can_be_none(self):
self.assertIsNone(grant.expires)
self.assertTrue(grant.is_expired())

def test_redirect_uri_can_be_longer_than_255_chars(self):
long_redirect_uri = "http://example.com/{}".format("authorized/" * 25)
self.assertTrue(len(long_redirect_uri) > 255)
grant = Grant.objects.create(
user=self.user,
code="test_code",
application=self.application,
expires=timezone.now(),
redirect_uri=long_redirect_uri,
scope="",
)
grant.refresh_from_db()

# It would be necessary to run test using another DB engine than sqlite
# that transform varchar(255) into text data type.
# https://sqlite.org/datatype3.html#affinity_name_examples
self.assertEqual(grant.redirect_uri, long_redirect_uri)

class TestAccessTokenModel(TestCase):
def setUp(self):
self.user = UserModel.objects.create_user("test_user", "test@example.com", "123456")

class TestAccessTokenModel(BaseTestModels):
def test_str(self):
access_token = AccessToken(token="test_token")
self.assertEqual("%s" % access_token, access_token.token)
Expand All @@ -273,15 +304,15 @@ def test_expires_can_be_none(self):
self.assertTrue(access_token.is_expired())


class TestRefreshTokenModel(TestCase):
class TestRefreshTokenModel(BaseTestModels):
def test_str(self):
refresh_token = RefreshToken(token="test_token")
self.assertEqual("%s" % refresh_token, refresh_token.token)


class TestClearExpired(TestCase):
class TestClearExpired(BaseTestModels):
def setUp(self):
self.user = UserModel.objects.create_user("test_user", "test@example.com", "123456")
super().setUp()
# Insert two tokens on database.
app = Application.objects.create(
name="test_app",
Expand Down