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
@@ -206,6 +211,42 @@ def test_get_jwks_info_multiple_rsa_keys(self):
206211 assert response .json () == expected_response
207212
208213
214+ @pytest .mark .usefixtures ("oauth2_settings" )
215+ @pytest .mark .oauth2_settings (presets .OIDC_SETTINGS_SESSION_MANAGEMENT )
216+ class TestAuthorizationView (TestCase ):
217+ def test_session_state_is_present_in_url (self ):
218+ User = get_user_model ()
219+ Application = get_application_model ()
220+
221+ User .
objects .
create_user (
"test_user" ,
"[email protected] " ,
"123456" )
222+ dev_user = User .
objects .
create_user (
"dev_user" ,
"[email protected] " ,
"123456" )
223+
224+ application = Application .objects .create (
225+ name = "Test Application" ,
226+ redirect_uris = (
227+ "http://localhost http://example.com http://example.org custom-scheme://example.com"
228+ ),
229+ user = dev_user ,
230+ client_type = Application .CLIENT_CONFIDENTIAL ,
231+ authorization_grant_type = Application .GRANT_AUTHORIZATION_CODE ,
232+ client_secret = "1234567890qwertyuiop" ,
233+ )
234+ self .client .login (username = "test_user" , password = "123456" )
235+ response = self .client .post (
236+ reverse ("oauth2_provider:authorize" ),
237+ {
238+ "client_id" : application .client_id ,
239+ "response_type" : "code" ,
240+ "state" : "random_state_string" ,
241+ "scope" : "read write" ,
242+ "redirect_uri" : "http://example.org" ,
243+ "allow" : True ,
244+ },
245+ )
246+ self .assertEqual (response .status_code , 302 )
247+ self .assertTrue ("session_state" in response ["Location" ])
248+
249+
209250def mock_request ():
210251 """
211252 Dummy request with an AnonymousUser attached.
@@ -467,10 +508,7 @@ def test_rp_initiated_logout_expired_tokens_accept(logged_in_client, application
467508 # Accepting expired (but otherwise valid and signed by us) tokens is enabled. Logout should go through.
468509 rsp = logged_in_client .get (
469510 reverse ("oauth2_provider:rp-initiated-logout" ),
470- data = {
471- "id_token_hint" : expired_id_token ,
472- "client_id" : application .client_id ,
473- },
511+ data = {"id_token_hint" : expired_id_token , "client_id" : application .client_id },
474512 )
475513 assert rsp .status_code == 302
476514 assert not is_logged_in (logged_in_client )
@@ -482,10 +520,7 @@ def test_rp_initiated_logout_expired_tokens_deny(logged_in_client, application,
482520 # Expired tokens should not be accepted by default.
483521 rsp = logged_in_client .get (
484522 reverse ("oauth2_provider:rp-initiated-logout" ),
485- data = {
486- "id_token_hint" : expired_id_token ,
487- "client_id" : application .client_id ,
488- },
523+ data = {"id_token_hint" : expired_id_token , "client_id" : application .client_id },
489524 )
490525 assert rsp .status_code == 400
491526 assert is_logged_in (logged_in_client )
0 commit comments