Skip to content

Commit 27ae6f2

Browse files
authored
Merge pull request #1320 from tfranzel/oauth2_scopes
fix unused OAuth2 scopes override #1319
2 parents 0dea78c + 8db4917 commit 27ae6f2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

drf_spectacular/contrib/django_oauth_toolkit.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ def get_security_definition(self, auto_schema):
3737
flows[flow_type]['tokenUrl'] = spectacular_settings.OAUTH2_TOKEN_URL
3838
if spectacular_settings.OAUTH2_REFRESH_URL:
3939
flows[flow_type]['refreshUrl'] = spectacular_settings.OAUTH2_REFRESH_URL
40-
scope_backend = get_scopes_backend()
41-
flows[flow_type]['scopes'] = scope_backend.get_all_scopes()
40+
if spectacular_settings.OAUTH2_SCOPES:
41+
flows[flow_type]['scopes'] = spectacular_settings.OAUTH2_SCOPES
42+
else:
43+
scope_backend = get_scopes_backend()
44+
flows[flow_type]['scopes'] = scope_backend.get_all_scopes()
4245

4346
return {
4447
'type': 'oauth2',

tests/contrib/test_oauth_toolkit.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,43 @@ def test_oauth2_toolkit_scopes_backend(no_warnings):
133133
assert 'implicit' in oauth2['flows']
134134
flow = oauth2['flows']['implicit']
135135
assert 'test_backend_scope' in flow['scopes']
136+
137+
138+
@mock.patch(
139+
'drf_spectacular.settings.spectacular_settings.OAUTH2_SCOPES',
140+
{"read": "Read scope", "burn": "Burn scope"},
141+
)
142+
@mock.patch(
143+
'drf_spectacular.settings.spectacular_settings.OAUTH2_FLOWS',
144+
['implicit']
145+
)
146+
@mock.patch(
147+
'drf_spectacular.settings.spectacular_settings.OAUTH2_REFRESH_URL',
148+
'http://127.0.0.1:8000/o/refresh'
149+
)
150+
@mock.patch(
151+
'drf_spectacular.settings.spectacular_settings.OAUTH2_AUTHORIZATION_URL',
152+
'http://127.0.0.1:8000/o/authorize'
153+
)
154+
@mock.patch(
155+
'oauth2_provider.settings.oauth2_settings.SCOPES',
156+
{"read": "Reading scope", "write": "Writing scope", "extra_scope": "Extra Scope"},
157+
)
158+
@mock.patch(
159+
'oauth2_provider.settings.oauth2_settings.DEFAULT_SCOPES',
160+
["read", "write"]
161+
)
162+
@pytest.mark.contrib('oauth2_provider')
163+
def test_oauth2_toolkit_custom_scopes(no_warnings):
164+
router = routers.SimpleRouter()
165+
router.register('TokenHasReadWriteScope', TokenHasReadWriteScopeViewset, basename="x1")
166+
167+
urlpatterns = [
168+
*router.urls,
169+
path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
170+
]
171+
schema = generate_schema(None, patterns=urlpatterns)
172+
173+
assert schema['components']['securitySchemes']['oauth2']['flows']['implicit']['scopes'] == {
174+
'burn': 'Burn scope', 'read': 'Read scope'
175+
}

0 commit comments

Comments
 (0)