@@ -184,6 +184,15 @@ def test_authorize(self):
184184        assert  resp ['code' ] in  self .provider .authz_state .authorization_codes 
185185        assert  resp ['state' ] ==  self .authn_request_args ['state' ]
186186
187+     def  test_authorize_with_custom_sub (self , monkeypatch ):
188+         sub  =  'test_sub1' 
189+         monkeypatch .setitem (self .provider .userinfo ._db [TEST_USER_ID ], 'sub' , sub )
190+         auth_req  =  AuthorizationRequest ().from_dict (self .authn_request_args )
191+         resp  =  self .provider .authorize (auth_req , TEST_USER_ID )
192+         assert  resp ['code' ] in  self .provider .authz_state .authorization_codes 
193+         assert  resp ['state' ] ==  self .authn_request_args ['state' ]
194+         assert  self .provider .authz_state .authorization_codes [resp ['code' ]]['sub' ] ==  sub 
195+ 
187196    @patch ('time.time' , MOCK_TIME ) 
188197    @pytest .mark .parametrize ('extra_claims' , [ 
189198        {'foo' : 'bar' }, 
@@ -427,6 +436,15 @@ def test_handle_userinfo(self):
427436        assert  response .to_dict () ==  self .provider .userinfo [TEST_USER_ID ]
428437        assert  self .provider .authz_state .get_user_id_for_subject_identifier (response_sub ) ==  TEST_USER_ID 
429438
439+     def  test_handle_userinfo_with_custom_sub (self , monkeypatch ):
440+         sub  =  'test_sub1' 
441+         monkeypatch .setitem (self .provider .userinfo ._db [TEST_USER_ID ], 'sub' , sub )
442+         claims_request  =  ClaimsRequest (userinfo = Claims (email = None ))
443+         access_token  =  self .create_access_token ({'scope' : 'openid profile' , 'claims' : claims_request })
444+         response  =  self .provider .handle_userinfo_request (urlencode ({'access_token' : access_token }))
445+ 
446+         assert  response ['sub' ] ==  sub 
447+ 
430448    def  test_handle_userinfo_rejects_request_missing_access_token (self ):
431449        with  pytest .raises (BearerTokenError ) as  exc :
432450            self .provider .handle_userinfo_request ()
0 commit comments