Skip to content

Commit 8a88c02

Browse files
authored
Merge pull request #11 from its-dirg/issue-6
Fix #6: Handle pre-existing subject identifier in the userinfo db.
2 parents 29732c2 + d6b3c41 commit 8a88c02

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/pyop/provider.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ def authorize(self, authentication_request, # type: oic.oic.message.Authorizati
133133
Creates an Authentication Response for the specified authentication request and local identifier of the
134134
authenticated user.
135135
"""
136-
sub = self._create_subject_identifier(user_id, authentication_request['client_id'],
137-
authentication_request['redirect_uri'])
136+
custom_sub = self.userinfo[user_id].get('sub')
137+
if custom_sub:
138+
self.authz_state.subject_identifiers[user_id] = {'public': custom_sub}
139+
sub = custom_sub
140+
else:
141+
sub = self._create_subject_identifier(user_id, authentication_request['client_id'],
142+
authentication_request['redirect_uri'])
143+
138144
self._check_subject_identifier_matches_requested(authentication_request, sub)
139145
response = AuthorizationResponse()
140146

@@ -425,7 +431,8 @@ def handle_userinfo_request(self, request=None, http_headers=None):
425431
requested_claims.update(self._get_requested_claims_in(authentication_request, 'userinfo'))
426432
user_claims = self.userinfo.get_claims_for(user_id, requested_claims)
427433

428-
response = OpenIDSchema(sub=introspection['sub'], **user_claims)
434+
user_claims.setdefault('sub', introspection['sub'])
435+
response = OpenIDSchema(**user_claims)
429436
logger.debug('userinfo=%s from requested_claims=%s userinfo=%s',
430437
response, requested_claims, user_claims)
431438
return response

tests/pyop/test_provider.py

+18
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)