1
1
import pytest
2
- from django .contrib .auth import get_user
2
+ from django .contrib .auth import get_user , get_user_model
3
3
from django .contrib .auth .models import AnonymousUser
4
4
from django .test import RequestFactory
5
5
from django .urls import reverse
12
12
InvalidOIDCClientError ,
13
13
InvalidOIDCRedirectURIError ,
14
14
)
15
- from oauth2_provider .models import get_access_token_model , get_id_token_model , get_refresh_token_model
15
+ from oauth2_provider .models import (
16
+ get_access_token_model ,
17
+ get_application_model ,
18
+ get_id_token_model ,
19
+ get_refresh_token_model ,
20
+ )
16
21
from oauth2_provider .oauth2_validators import OAuth2Validator
17
22
from oauth2_provider .settings import oauth2_settings
18
23
from oauth2_provider .views .oidc import RPInitiatedLogoutView , _load_id_token , _validate_claims
@@ -132,7 +137,10 @@ def test_get_connect_discovery_info_without_issuer_url(self):
132
137
],
133
138
"subject_types_supported" : ["public" ],
134
139
"id_token_signing_alg_values_supported" : ["RS256" , "HS256" ],
135
- "token_endpoint_auth_methods_supported" : ["client_secret_post" , "client_secret_basic" ],
140
+ "token_endpoint_auth_methods_supported" : [
141
+ "client_secret_post" ,
142
+ "client_secret_basic" ,
143
+ ],
136
144
"code_challenge_methods_supported" : ["plain" , "S256" ],
137
145
"claims_supported" : ["sub" ],
138
146
}
@@ -206,6 +214,42 @@ def test_get_jwks_info_multiple_rsa_keys(self):
206
214
assert response .json () == expected_response
207
215
208
216
217
+ @pytest .mark .usefixtures ("oauth2_settings" )
218
+ @pytest .mark .oauth2_settings (presets .OIDC_SETTINGS_SESSION_MANAGEMENT )
219
+ class TestAuthorizationView (TestCase ):
220
+ def test_session_state_is_present_in_url (self ):
221
+ User = get_user_model ()
222
+ Application = get_application_model ()
223
+
224
+ User .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
225
+ dev_user = User .
objects .
create_user (
"dev_user" ,
"[email protected] " ,
"123456" )
226
+
227
+ application = Application .objects .create (
228
+ name = "Test Application" ,
229
+ redirect_uris = (
230
+ "http://localhost http://example.com http://example.org custom-scheme://example.com"
231
+ ),
232
+ user = dev_user ,
233
+ client_type = Application .CLIENT_CONFIDENTIAL ,
234
+ authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
235
+ client_secret = "1234567890qwertyuiop" ,
236
+ )
237
+ self .client .login (username = "test_user" , password = "123456" )
238
+ response = self .client .post (
239
+ reverse ("oauth2_provider:authorize" ),
240
+ {
241
+ "client_id" : application .client_id ,
242
+ "response_type" : "code" ,
243
+ "state" : "random_state_string" ,
244
+ "scope" : "read write" ,
245
+ "redirect_uri" : "http://example.org" ,
246
+ "allow" : True ,
247
+ },
248
+ )
249
+ self .assertEqual (response .status_code , 302 )
250
+ self .assertTrue ("session_state" in response ["Location" ])
251
+
252
+
209
253
def mock_request ():
210
254
"""
211
255
Dummy request with an AnonymousUser attached.
@@ -335,7 +379,8 @@ def test_rp_initiated_logout_get(logged_in_client, rp_settings):
335
379
@pytest .mark .django_db (databases = retrieve_current_databases ())
336
380
def test_rp_initiated_logout_get_id_token (logged_in_client , oidc_tokens , rp_settings ):
337
381
rsp = logged_in_client .get (
338
- reverse ("oauth2_provider:rp-initiated-logout" ), data = {"id_token_hint" : oidc_tokens .id_token }
382
+ reverse ("oauth2_provider:rp-initiated-logout" ),
383
+ data = {"id_token_hint" : oidc_tokens .id_token },
339
384
)
340
385
assert rsp .status_code == 302
341
386
assert rsp ["Location" ] == "http://testserver/"
@@ -467,10 +512,7 @@ def test_rp_initiated_logout_expired_tokens_accept(logged_in_client, application
467
512
# Accepting expired (but otherwise valid and signed by us) tokens is enabled. Logout should go through.
468
513
rsp = logged_in_client .get (
469
514
reverse ("oauth2_provider:rp-initiated-logout" ),
470
- data = {
471
- "id_token_hint" : expired_id_token ,
472
- "client_id" : application .client_id ,
473
- },
515
+ data = {"id_token_hint" : expired_id_token , "client_id" : application .client_id },
474
516
)
475
517
assert rsp .status_code == 302
476
518
assert not is_logged_in (logged_in_client )
@@ -482,10 +524,7 @@ def test_rp_initiated_logout_expired_tokens_deny(logged_in_client, application,
482
524
# Expired tokens should not be accepted by default.
483
525
rsp = logged_in_client .get (
484
526
reverse ("oauth2_provider:rp-initiated-logout" ),
485
- data = {
486
- "id_token_hint" : expired_id_token ,
487
- "client_id" : application .client_id ,
488
- },
527
+ data = {"id_token_hint" : expired_id_token , "client_id" : application .client_id },
489
528
)
490
529
assert rsp .status_code == 400
491
530
assert is_logged_in (logged_in_client )
0 commit comments