Skip to content

Commit 66251cc

Browse files
committed
Minor simplifications
Signed-off-by: Ivan Kanakarakis <ivan.kanak@gmail.com>
1 parent 2b4bd40 commit 66251cc

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

src/pyop/authz_state.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,27 @@ def __init__(self, subject_identifier_factory, authorization_code_db=None, acces
8080
"""
8181
Mapping of user id's to subject identifiers.
8282
"""
83-
if isinstance(self.authorization_codes, StatelessWrapper) or \
84-
isinstance(self.access_tokens, StatelessWrapper) or isinstance(
85-
self.refresh_tokens, StatelessWrapper):
86-
self.stateless = True
87-
self.subject_identifiers = {}
88-
else:
89-
self.stateless = False
90-
self.subject_identifiers = subject_identifier_db if subject_identifier_db is not None else {}
91-
92-
def create_authorization_code(self, authorization_request, subject_identifier, scope=None, user_info=None,
93-
extra_id_token_claims=None):
83+
self.stateless = (
84+
isinstance(self.authorization_codes, StatelessWrapper)
85+
or isinstance(self.access_tokens, StatelessWrapper)
86+
or isinstance(self.refresh_tokens, StatelessWrapper)
87+
)
88+
self.subject_identifiers = (
89+
{}
90+
if self.stateless
91+
else subject_identifier_db
92+
if subject_identifier_db is not None
93+
else {}
94+
)
95+
96+
def create_authorization_code(
97+
self,
98+
authorization_request,
99+
subject_identifier,
100+
scope=None,
101+
user_info=None,
102+
extra_id_token_claims=None,
103+
):
94104
# type: (AuthorizationRequest, str, Optional[List[str]], Optional[dict], Optional[Mappings[str, Union[str, List[str]]]]) -> str
95105
"""
96106
Creates an authorization code bound to the authorization request and the authenticated user identified
@@ -111,7 +121,7 @@ def create_authorization_code(self, authorization_request, subject_identifier, s
111121
self.KEY_AUTHORIZATION_REQUEST: authorization_request.to_dict()
112122
}
113123

114-
if isinstance(self.authorization_codes, StatelessWrapper):
124+
if self.stateless:
115125
if user_info:
116126
authz_info[self.KEY_USER_INFO] = user_info
117127
authz_info[self.KEY_EXTRA_ID_TOKEN_CLAIMS] = extra_id_token_claims or {}
@@ -159,7 +169,7 @@ def _create_access_token(self, subject_identifier, auth_req, granted_scope, curr
159169
self.KEY_AUTHORIZATION_REQUEST: auth_req
160170
}
161171

162-
if isinstance(self.access_tokens, StatelessWrapper):
172+
if self.stateless:
163173
if user_info:
164174
authz_info[self.KEY_USER_INFO] = user_info
165175
access_token_val = self.access_tokens.pack(authz_info)
@@ -229,7 +239,7 @@ def create_refresh_token(self, access_token_value):
229239

230240
authz_info = {'access_token': access_token_value, 'exp': int(time.time()) + self.refresh_token_lifetime}
231241

232-
if isinstance(self.refresh_tokens, StatelessWrapper):
242+
if self.stateless:
233243
refresh_token = self.refresh_tokens.pack(authz_info)
234244
else:
235245
refresh_token = rand_str()
@@ -326,7 +336,7 @@ def get_subject_identifier(self, subject_type, user_id, sector_identifier=None):
326336
raise ValueError('Unknown subject_type={}'.format(subject_type))
327337

328338
def _is_valid_subject_identifier(self, sub):
329-
# type: (str) -> str
339+
# type: (str) -> bool
330340
"""
331341
Determines whether the subject identifier is known.
332342
"""
@@ -340,8 +350,7 @@ def _is_valid_subject_identifier(self, sub):
340350
def get_user_id_for_subject_identifier(self, subject_identifier):
341351
for user_id, subject_identifiers in self.subject_identifiers.items():
342352
is_public_sub = 'public' in subject_identifiers and subject_identifier == subject_identifiers['public']
343-
is_pairwise_sub = 'pairwise' in subject_identifiers and subject_identifier in subject_identifiers[
344-
'pairwise']
353+
is_pairwise_sub = 'pairwise' in subject_identifiers and subject_identifier in subject_identifiers['pairwise']
345354
if is_public_sub or is_pairwise_sub:
346355
return user_id
347356

@@ -377,7 +386,7 @@ def get_authorization_request_for_code(self, authorization_code):
377386
self.authorization_codes[authorization_code][self.KEY_AUTHORIZATION_REQUEST])
378387

379388
def get_authorization_request_for_access_token(self, access_token_value):
380-
# type: (str) ->
389+
# type: (str) ->
381390
if access_token_value not in self.access_tokens:
382391
raise InvalidAccessToken('{} unknown'.format(access_token_value))
383392

src/pyop/provider.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ def __init__(self, signing_key, configuration_information, authz_state, clients,
7979
self.configuration_information.verify()
8080

8181
self.authz_state = authz_state
82-
83-
if self.authz_state and self.authz_state.stateless:
84-
self.stateless = True
85-
else:
86-
self.stateless = False
82+
self.stateless = self.authz_state and self.authz_state.stateless
8783

8884
self.clients = clients
8985
self.userinfo = userinfo
@@ -611,7 +607,7 @@ def handle_client_registration_request(self, request, http_headers=None):
611607
def logout_user(self, subject_identifier=None, end_session_request=None):
612608
# type: (Optional[str], Optional[oic.oic.message.EndSessionRequest]) -> None
613609
if self.stateless:
614-
raise OAuthError("logout user isn't supported with stateless provider", "invalid_request")
610+
raise OAuthError("Logout is not supported with stateless storage provider", "invalid_request")
615611
if not end_session_request:
616612
end_session_request = EndSessionRequest()
617613
if 'id_token_hint' in end_session_request:

0 commit comments

Comments
 (0)