Skip to content

Commit e63999d

Browse files
Jaap Roespre-commit-ci[bot]
andauthored
Work around double parsing of ui_locales (#1469)
* Work around double parsing of ui_locales * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 34912ff commit e63999d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232

3333
### Fixed
3434
* #1443 Query strings with invalid hex values now raise a SuspiciousOperation exception (in DRF extension) instead of raising a 500 ValueError: Invalid hex encoding in query string.
35+
* #1468 `ui_locales` request parameter triggers `AttributeError` under certain circumstances
3536
### Security
3637

3738
## [2.4.0] - 2024-05-13

oauth2_provider/views/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ def get(self, request, *args, **kwargs):
186186
# a successful response depending on "approval_prompt" url parameter
187187
require_approval = request.GET.get("approval_prompt", oauth2_settings.REQUEST_APPROVAL_PROMPT)
188188

189+
if "ui_locales" in credentials and isinstance(credentials["ui_locales"], list):
190+
# Make sure ui_locales a space separated string for oauthlib to handle it correctly.
191+
credentials["ui_locales"] = " ".join(credentials["ui_locales"])
192+
189193
try:
190194
# If skip_authorization field is True, skip the authorization screen even
191195
# if this is the first use of the application and there was no previous authorization.

tests/test_ui_locales.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from django.contrib.auth import get_user_model
2+
from django.test import TestCase, override_settings
3+
from django.urls import reverse
4+
5+
from oauth2_provider.models import get_application_model
6+
7+
8+
UserModel = get_user_model()
9+
Application = get_application_model()
10+
11+
12+
@override_settings(
13+
OAUTH2_PROVIDER={
14+
"OIDC_ENABLED": True,
15+
"PKCE_REQUIRED": False,
16+
"SCOPES": {
17+
"openid": "OpenID connect",
18+
},
19+
}
20+
)
21+
class TestUILocalesParam(TestCase):
22+
@classmethod
23+
def setUpTestData(cls):
24+
cls.application = Application.objects.create(
25+
name="Test Application",
26+
client_id="test",
27+
redirect_uris="https://www.example.com/",
28+
client_type=Application.CLIENT_PUBLIC,
29+
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
30+
)
31+
cls.trusted_application = Application.objects.create(
32+
name="Trusted Application",
33+
client_id="trusted",
34+
redirect_uris="https://www.example.com/",
35+
client_type=Application.CLIENT_PUBLIC,
36+
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
37+
skip_authorization=True,
38+
)
39+
cls.user = UserModel.objects.create_user("test_user")
40+
cls.url = reverse("oauth2_provider:authorize")
41+
42+
def setUp(self):
43+
self.client.force_login(self.user)
44+
45+
def test_application_ui_locales_param(self):
46+
response = self.client.get(
47+
f"{self.url}?response_type=code&client_id=test&scope=openid&ui_locales=de",
48+
)
49+
self.assertEqual(response.status_code, 200)
50+
self.assertTemplateUsed(response, "oauth2_provider/authorize.html")
51+
52+
def test_trusted_application_ui_locales_param(self):
53+
response = self.client.get(
54+
f"{self.url}?response_type=code&client_id=trusted&scope=openid&ui_locales=de",
55+
)
56+
self.assertEqual(response.status_code, 302)
57+
self.assertRegex(response.url, r"https://www\.example\.com/\?code=[a-zA-Z0-9]+")

0 commit comments

Comments
 (0)