Skip to content
This repository was archived by the owner on Jun 12, 2021. It is now read-only.

Commit 68363ea

Browse files
committed
More pylinting.
1 parent fab55a8 commit 68363ea

15 files changed

+110
-92
lines changed

exampel/conversation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@
278278
_authz_rep = AuthorizationResponse(**op_authz_resp)
279279
print(_authz_rep.to_urlencoded())
280280
_resp = service['authorization'].parse_response(_authz_rep.to_urlencoded())
281-
service['authorization'].update_service_context(_resp, state=STATE)
281+
service['authorization'].update_service_context(_resp, key=STATE)
282282
print()
283283
print('--- Authorization registration, response ----')
284284
print(_resp)
@@ -316,7 +316,7 @@
316316

317317
service_context.issuer = OP_BASEURL
318318
_resp = service['accesstoken'].parse_response(json.dumps(_resp), state=STATE)
319-
service['accesstoken'].update_service_context(_resp, state=STATE)
319+
service['accesstoken'].update_service_context(_resp, key=STATE)
320320
print()
321321
print('--- Access token, response ----')
322322
print(_resp)
@@ -334,7 +334,7 @@
334334
op_resp = {"sub": "1b2fc9341a16ae4e30082965d537"}
335335

336336
_resp = service['userinfo'].parse_response(json.dumps(op_resp), state=STATE)
337-
service['userinfo'].update_service_context(_resp, state=STATE)
337+
service['userinfo'].update_service_context(_resp, key=STATE)
338338
print()
339339
print('--- User info, response ----')
340340
print(_resp)

src/oidcservice/client_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def find_token(request, token_type, service, **kwargs):
267267
# I should pick the latest acquired token, this should be the right
268268
# order for that.
269269
_arg = service.multiple_extend_request_args(
270-
{}, kwargs['state'], ['access_token'],
270+
{}, kwargs['key'], ['access_token'],
271271
['auth_response', 'token_response', 'refresh_token_response'])
272272
return _arg['access_token']
273273

src/oidcservice/oauth2/access_token.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Implements the service that talks to the Access Token endpoint."""
12
import logging
23

34
from oidcmsg import oauth2
@@ -8,10 +9,11 @@
89
from oidcservice.service import Service
910

1011

11-
logger = logging.getLogger(__name__)
12+
LOGGER = logging.getLogger(__name__)
1213

1314

1415
class AccessToken(Service):
16+
"""The access token service."""
1517
msg_type = oauth2.AccessTokenRequest
1618
response_cls = oauth2.AccessTokenResponse
1719
error_msg = ResponseMessage
@@ -60,4 +62,3 @@ def oauth_pre_construct(self, request_args=None, **kwargs):
6062
request_args = _args
6163

6264
return request_args, {}
63-

src/oidcservice/oauth2/authorization.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""The service that talks to the OAuth2 Authorization endpoint."""
12
import logging
23

34
from oidcmsg import oauth2
@@ -11,10 +12,11 @@
1112
from oidcservice.service import Service
1213

1314

14-
logger = logging.getLogger(__name__)
15+
LOGGER = logging.getLogger(__name__)
1516

1617

1718
class Authorization(Service):
19+
"""The service that talks to the OAuth2 Authorization endpoint."""
1820
msg_type = oauth2.AuthorizationRequest
1921
response_cls = oauth2.AuthorizationResponse
2022
error_msg = ResponseMessage
@@ -30,12 +32,13 @@ def __init__(self, service_context, state_db,
3032
self.pre_construct.extend([pick_redirect_uris, set_state_parameter])
3133
self.post_construct.append(self.store_auth_request)
3234

33-
def update_service_context(self, resp, state='', **kwargs):
35+
def update_service_context(self, resp, key='', **kwargs):
3436
if 'expires_in' in resp:
3537
resp['__expires_at'] = time_sans_frac() + int(resp['expires_in'])
36-
self.store_item(resp, 'auth_response', state)
38+
self.store_item(resp, 'auth_response', key)
3739

3840
def store_auth_request(self, request_args=None, **kwargs):
41+
"""Store the authorization request in the state DB."""
3942
_key = get_state_parameter(request_args, kwargs)
4043
self.store_item(request_args, 'auth_request', _key)
4144
return request_args
@@ -75,4 +78,3 @@ def post_parse_response(self, response, **kwargs):
7578
except KeyError:
7679
pass
7780
return response
78-
Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""The service that talks to the OAuth2 provider info discovery endpoint."""
12
import logging
23

34
from cryptojwt.key_jar import KeyJar
@@ -9,10 +10,11 @@
910
from oidcservice.exception import OidcServiceError
1011
from oidcservice.service import Service
1112

12-
logger = logging.getLogger(__name__)
13+
LOGGER = logging.getLogger(__name__)
1314

1415

1516
class ProviderInfoDiscovery(Service):
17+
"""The service that talks to the OAuth2 provider info discovery endpoint."""
1618
msg_type = oauth2.Message
1719
response_cls = oauth2.ASConfigurationResponse
1820
error_msg = ResponseMessage
@@ -38,8 +40,8 @@ def get_endpoint(self):
3840

3941
if _iss.endswith('/'):
4042
return OIDCONF_PATTERN.format(_iss[:-1])
41-
else:
42-
return OIDCONF_PATTERN.format(_iss)
43+
44+
return OIDCONF_PATTERN.format(_iss)
4345

4446
def get_request_parameters(self, method="GET", **kwargs):
4547
"""
@@ -51,82 +53,92 @@ def get_request_parameters(self, method="GET", **kwargs):
5153
"""
5254
return {'url': self.get_endpoint(), 'method': method}
5355

54-
def _update_service_context(self, resp, **kwargs):
56+
def _verify_issuer(self, resp, issuer):
57+
_pcr_issuer = resp["issuer"]
58+
if resp["issuer"].endswith("/"):
59+
if issuer.endswith("/"):
60+
_issuer = issuer
61+
else:
62+
_issuer = issuer + "/"
63+
else:
64+
if issuer.endswith("/"):
65+
_issuer = issuer[:-1]
66+
else:
67+
_issuer = issuer
68+
69+
# In some cases we can live with the two URLs not being
70+
# the same. But this is an excepted that has to be explicit
71+
try:
72+
self.service_context.allow['issuer_mismatch']
73+
except KeyError:
74+
if _issuer != _pcr_issuer:
75+
raise OidcServiceError(
76+
"provider info issuer mismatch '%s' != '%s'" % (
77+
_issuer, _pcr_issuer))
78+
return _issuer
79+
80+
@staticmethod
81+
def _store_endpoint(srvs, key, val):
82+
for _srv in srvs.values():
83+
# Every service has an endpoint_name assigned
84+
# when initiated. This name *MUST* match the
85+
# endpoint names used in the provider info
86+
if _srv.endpoint_name == key:
87+
_srv.endpoint = val
88+
89+
def _set_endpoints(self, resp):
90+
"""
91+
If there are services defined set the service endpoint to be
92+
the URLs specified in the provider information."""
93+
try:
94+
_srvs = self.service_context.service
95+
except AttributeError:
96+
pass
97+
else:
98+
if _srvs:
99+
for key, val in resp.items():
100+
# All service endpoint parameters in the provider info has
101+
# a name ending in '_endpoint' so I can look specifically
102+
# for those
103+
if key.endswith("_endpoint"):
104+
self._store_endpoint(_srvs, key, val)
105+
106+
def _update_service_context(self, resp):
55107
"""
56108
Deal with Provider Config Response. Based on the provider info
57109
response a set of parameters in different places needs to be set.
58110
59111
:param resp: The provider info response
60112
:param service_context: Information collected/used by services
61113
"""
62-
issuer = self.service_context.issuer
63114

64115
# Verify that the issuer value received is the same as the
65116
# url that was used as service endpoint (without the .well-known part)
66117
if "issuer" in resp:
67-
_pcr_issuer = resp["issuer"]
68-
if resp["issuer"].endswith("/"):
69-
if issuer.endswith("/"):
70-
_issuer = issuer
71-
else:
72-
_issuer = issuer + "/"
73-
else:
74-
if issuer.endswith("/"):
75-
_issuer = issuer[:-1]
76-
else:
77-
_issuer = issuer
78-
79-
# In some cases we can live with the two URLs not being
80-
# the same. But this is an excepted that has to be explicit
81-
try:
82-
self.service_context.allow['issuer_mismatch']
83-
except KeyError:
84-
if _issuer != _pcr_issuer:
85-
raise OidcServiceError(
86-
"provider info issuer mismatch '%s' != '%s'" % (
87-
_issuer, _pcr_issuer))
88-
118+
_pcr_issuer = self._verify_issuer(resp, self.service_context.issuer)
89119
else: # No prior knowledge
90-
_pcr_issuer = issuer
120+
_pcr_issuer = self.service_context.issuer
91121

92122
self.service_context.issuer = _pcr_issuer
93123
self.service_context.provider_info = resp
94124

95-
# If there are services defined set the service endpoint to be
96-
# the URLs specified in the provider information.
97-
try:
98-
_srvs = self.service_context.service
99-
except AttributeError:
100-
pass
101-
else:
102-
if self.service_context.service:
103-
for key, val in resp.items():
104-
# All service endpoint parameters in the provider info has
105-
# a name ending in '_endpoint' so I can look specifically
106-
# for those
107-
if key.endswith("_endpoint"):
108-
for _srv in self.service_context.service.values():
109-
# Every service has an endpoint_name assigned
110-
# when initiated. This name *MUST* match the
111-
# endpoint names used in the provider info
112-
if _srv.endpoint_name == key:
113-
_srv.endpoint = val
125+
self._set_endpoints(resp)
114126

115127
# If I already have a Key Jar then I'll add then provider keys to
116128
# that. Otherwise a new Key Jar is minted
117129
try:
118-
kj = self.service_context.keyjar
130+
_keyjar = self.service_context.keyjar
119131
except KeyError:
120-
kj = KeyJar()
132+
_keyjar = KeyJar()
121133

122134
# Load the keys. Note that this only means that the key specification
123135
# is loaded not necessarily that any keys are fetched.
124136
if 'jwks_uri' in resp:
125-
kj.load_keys(_pcr_issuer, jwks_uri=resp['jwks_uri'])
137+
_keyjar.load_keys(_pcr_issuer, jwks_uri=resp['jwks_uri'])
126138
elif 'jwks' in resp:
127-
kj.load_keys(_pcr_issuer, jwks=resp['jwks'])
139+
_keyjar.load_keys(_pcr_issuer, jwks=resp['jwks'])
128140

129-
self.service_context.keyjar = kj
141+
self.service_context.keyjar = _keyjar
130142

131143
def update_service_context(self, resp, **kwargs):
132-
return self._update_service_context(resp, **kwargs)
144+
return self._update_service_context(resp)

src/oidcservice/oauth2/refresh_access_token.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""The service that talks to the OAuth2 refresh access token endpoint."""
12
import logging
23

34
from oidcmsg import oauth2
@@ -8,10 +9,11 @@
89
from oidcservice.service import Service
910

1011

11-
logger = logging.getLogger(__name__)
12+
LOGGER = logging.getLogger(__name__)
1213

1314

1415
class RefreshAccessToken(Service):
16+
"""The service that talks to the OAuth2 refresh access token endpoint."""
1517
msg_type = oauth2.RefreshAccessTokenRequest
1618
response_cls = oauth2.AccessTokenResponse
1719
error_msg = ResponseMessage
@@ -33,6 +35,7 @@ def update_service_context(self, resp, key='', **kwargs):
3335
self.store_item(resp, 'token_response', key)
3436

3537
def oauth_pre_construct(self, request_args=None, **kwargs):
38+
"""Preconstructor of request arguments"""
3639
_state = get_state_parameter(request_args, kwargs)
3740
parameters = list(self.msg_type.c_param.keys())
3841

src/oidcservice/oauth2/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
def get_state_parameter(request_args, kwargs):
5+
"""Find a state value from a set of possible places."""
56
try:
67
_state = kwargs['state']
78
except KeyError:
@@ -14,6 +15,7 @@ def get_state_parameter(request_args, kwargs):
1415

1516

1617
def pick_redirect_uris(request_args=None, service=None, **kwargs):
18+
"""Pick one redirect_uri base on response_mode out of a list of such."""
1719
_context = service.service_context
1820
if 'redirect_uri' in request_args:
1921
pass
@@ -43,6 +45,6 @@ def pick_redirect_uris(request_args=None, service=None, **kwargs):
4345

4446

4547
def set_state_parameter(request_args=None, **kwargs):
48+
"""Assigned a state value."""
4649
request_args['state'] = get_state_parameter(request_args, kwargs)
4750
return request_args, {'state': request_args['state']}
48-

src/oidcservice/oidc/access_token.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,25 @@ def gather_verify_arguments(self):
5151

5252
return kwargs
5353

54-
def update_service_context(self, resp, state='', **kwargs):
54+
def update_service_context(self, resp, key='', **kwargs):
5555
try:
5656
_idt = resp[verified_claim_name('id_token')]
5757
except KeyError:
5858
pass
5959
else:
6060
try:
61-
if self.get_state_by_nonce(_idt['nonce']) != state:
61+
if self.get_state_by_nonce(_idt['nonce']) != key:
6262
raise ParameterError('Someone has messed with "nonce"')
6363
except KeyError:
6464
raise ValueError('Invalid nonce value')
6565

66-
self.store_sub2state(_idt['sub'], state)
66+
self.store_sub2state(_idt['sub'], key)
6767

6868
if 'expires_in' in resp:
6969
resp['__expires_at'] = time_sans_frac() + int(
7070
resp['expires_in'])
7171

72-
self.store_item(resp, 'token_response', state)
72+
self.store_item(resp, 'token_response', key)
7373

7474
def get_authn_method(self):
7575
try:

src/oidcservice/oidc/authorization.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def set_state(self, request_args, **kwargs):
4545
_state)
4646
return request_args, {}
4747

48-
def update_service_context(self, resp, state='', **kwargs):
48+
def update_service_context(self, resp, key='', **kwargs):
4949
try:
5050
_idt = resp[verified_claim_name('id_token')]
5151
except KeyError:
@@ -54,16 +54,16 @@ def update_service_context(self, resp, state='', **kwargs):
5454
# If there is a verified ID Token then we have to do nonce
5555
# verification
5656
try:
57-
if self.get_state_by_nonce(_idt['nonce']) != state:
57+
if self.get_state_by_nonce(_idt['nonce']) != key:
5858
raise ParameterError('Someone has messed with "nonce"')
5959
except KeyError:
6060
raise ValueError('Missing nonce value')
6161

62-
self.store_sub2state(_idt['sub'], state)
62+
self.store_sub2state(_idt['sub'], key)
6363

6464
if 'expires_in' in resp:
6565
resp['__expires_at'] = time_sans_frac() + int(resp['expires_in'])
66-
self.store_item(resp.to_json(), 'auth_response', state)
66+
self.store_item(resp.to_json(), 'auth_response', key)
6767

6868
def oidc_pre_construct(self, request_args=None, **kwargs):
6969
if request_args is None:

src/oidcservice/oidc/registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def oidc_post_construct(self, request_args=None, **kwargs):
144144

145145
return request_args
146146

147-
def update_service_context(self, resp, state='', **kwargs):
147+
def update_service_context(self, resp, key='', **kwargs):
148148
self.service_context.registration_response = resp
149149
if "token_endpoint_auth_method" not in \
150150
self.service_context.registration_response:

0 commit comments

Comments
 (0)