11import pytest
2- from django .contrib .auth import get_user
2+ from django .contrib .auth import get_user , get_user_model
33from django .contrib .auth .models import AnonymousUser
44from django .test import RequestFactory
55from django .urls import reverse
1212 InvalidOIDCClientError ,
1313 InvalidOIDCRedirectURIError ,
1414)
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+ )
1621from oauth2_provider .oauth2_validators import OAuth2Validator
1722from oauth2_provider .settings import oauth2_settings
1823from 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):
132137 ],
133138 "subject_types_supported" : ["public" ],
134139 "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+ ],
136144 "code_challenge_methods_supported" : ["plain" , "S256" ],
137145 "claims_supported" : ["sub" ],
138146 }
@@ -206,6 +214,42 @@ def test_get_jwks_info_multiple_rsa_keys(self):
206214 assert response .json () == expected_response
207215
208216
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+
209253def mock_request ():
210254 """
211255 Dummy request with an AnonymousUser attached.
@@ -335,7 +379,8 @@ def test_rp_initiated_logout_get(logged_in_client, rp_settings):
335379@pytest .mark .django_db (databases = retrieve_current_databases ())
336380def test_rp_initiated_logout_get_id_token (logged_in_client , oidc_tokens , rp_settings ):
337381 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 },
339384 )
340385 assert rsp .status_code == 302
341386 assert rsp ["Location" ] == "http://testserver/"
@@ -467,10 +512,7 @@ def test_rp_initiated_logout_expired_tokens_accept(logged_in_client, application
467512 # Accepting expired (but otherwise valid and signed by us) tokens is enabled. Logout should go through.
468513 rsp = logged_in_client .get (
469514 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 },
474516 )
475517 assert rsp .status_code == 302
476518 assert not is_logged_in (logged_in_client )
@@ -482,10 +524,7 @@ def test_rp_initiated_logout_expired_tokens_deny(logged_in_client, application,
482524 # Expired tokens should not be accepted by default.
483525 rsp = logged_in_client .get (
484526 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 },
489528 )
490529 assert rsp .status_code == 400
491530 assert is_logged_in (logged_in_client )
0 commit comments