Skip to content

Commit bfd3c65

Browse files
committed
Improve code for codeclimate
1 parent 2a31f20 commit bfd3c65

8 files changed

Lines changed: 65 additions & 46 deletions

File tree

.codeclimate.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ checks:
1313
config:
1414
threshold: 35
1515

16+
method-count:
17+
enabled: false
18+
1619
file-lines:
1720
config:
1821
threshold: 460
1922

2023
return-statements:
2124
config:
2225
threshold: 6
26+
27+
similar-code:
28+
enabled: false

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Lovely features that Authlib has built-in:
184184
<summary>🎉 RFC8414: OAuth 2.0 Authorization Server Metadata</summary>
185185

186186
- [x] Authorization Server Metadata Model
187+
- [x] Well Known URI
187188
- [ ] Framework integrations
188189
</details>
189190

@@ -196,8 +197,11 @@ Lovely features that Authlib has built-in:
196197
</details>
197198

198199
<details>
199-
<summary>⏳ OpenID Connect Discovery 1.0</summary>
200-
<p>Developers can create a JSON file themselves.</p>
200+
<summary>🎉 OpenID Connect Discovery 1.0</summary>
201+
202+
- [x] OpenID Provider Metadata Model
203+
- [x] Well Known URI
204+
- [ ] Framework integrations
201205
</details>
202206

203207
And more will be added.

authlib/oauth2/client.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,10 @@ def fetch_token(self, url=None, code=None, authorization_response=None,
151151
InsecureTransportError.check(url)
152152

153153
session_kwargs = self._extract_session_request_params(kwargs)
154-
if code or authorization_response:
155-
body = self._prepare_authorization_code_body(
156-
code, authorization_response, body, **kwargs)
157-
elif username and password:
158-
if 'scope' not in kwargs and self.scope:
159-
kwargs['scope'] = self.scope
160-
grant_type = kwargs.pop('grant_type', 'password')
161-
body = prepare_token_request(
162-
grant_type, body, username=username,
163-
password=password, **kwargs)
164-
else:
165-
grant_type = kwargs.pop('grant_type', 'client_credentials')
166-
if 'scope' not in kwargs and self.scope:
167-
kwargs['scope'] = self.scope
168-
body = prepare_token_request(grant_type, body, **kwargs)
154+
155+
body = self._prepare_token_endpoint_body(
156+
code, authorization_response, body,
157+
username, password, **kwargs)
169158

170159
if auth is None:
171160
auth = self.client_auth
@@ -333,6 +322,25 @@ def _prepare_authorization_code_body(self, code, authorization_response,
333322
'authorization_code', body=body,
334323
code=code, state=state, **kwargs)
335324

325+
def _prepare_token_endpoint_body(self, code, authorization_response,
326+
body, username, password, **kwargs):
327+
if code or authorization_response:
328+
body = self._prepare_authorization_code_body(
329+
code, authorization_response, body, **kwargs)
330+
elif username and password:
331+
if 'scope' not in kwargs and self.scope:
332+
kwargs['scope'] = self.scope
333+
grant_type = kwargs.pop('grant_type', 'password')
334+
body = prepare_token_request(
335+
grant_type, body, username=username,
336+
password=password, **kwargs)
337+
else:
338+
grant_type = kwargs.pop('grant_type', 'client_credentials')
339+
if 'scope' not in kwargs and self.scope:
340+
kwargs['scope'] = self.scope
341+
body = prepare_token_request(grant_type, body, **kwargs)
342+
return body
343+
336344
def _extract_session_request_params(self, kwargs):
337345
"""Extract parameters for session object from the passing ``**kwargs``."""
338346
rv = {}

authlib/oauth2/rfc8414/models.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def validate_scopes_supported(self):
107107
Servers MAY choose not to advertise some supported scope values
108108
even when this parameter is used.
109109
"""
110-
_validate_array_value(self, 'scopes_supported')
110+
validate_array_value(self, 'scopes_supported')
111111

112112
def validate_response_types_supported(self):
113113
"""REQUIRED. JSON array containing a list of the OAuth 2.0
@@ -130,7 +130,7 @@ def validate_response_modes_supported(self):
130130
"fragment"]". The response mode value "form_post" is also defined
131131
in "OAuth 2.0 Form Post Response Mode" [OAuth.Post].
132132
"""
133-
_validate_array_value(self, 'response_modes_supported')
133+
validate_array_value(self, 'response_modes_supported')
134134

135135
def validate_grant_types_supported(self):
136136
"""OPTIONAL. JSON array containing a list of the OAuth 2.0 grant
@@ -140,7 +140,7 @@ def validate_grant_types_supported(self):
140140
Protocol" [RFC7591]. If omitted, the default value is
141141
"["authorization_code", "implicit"]".
142142
"""
143-
_validate_array_value(self, 'grant_types_supported')
143+
validate_array_value(self, 'grant_types_supported')
144144

145145
def validate_token_endpoint_auth_methods_supported(self):
146146
"""OPTIONAL. JSON array containing a list of client authentication
@@ -150,7 +150,7 @@ def validate_token_endpoint_auth_methods_supported(self):
150150
default is "client_secret_basic" -- the HTTP Basic Authentication
151151
Scheme specified in Section 2.3.1 of OAuth 2.0 [RFC6749].
152152
"""
153-
_validate_array_value(self, 'token_endpoint_auth_methods_supported')
153+
validate_array_value(self, 'token_endpoint_auth_methods_supported')
154154

155155
def validate_token_endpoint_auth_signing_alg_values_supported(self):
156156
"""OPTIONAL. JSON array containing a list of the JWS signing
@@ -187,7 +187,7 @@ def validate_ui_locales_supported(self):
187187
[RFC5646]. If omitted, the set of supported languages and scripts
188188
is unspecified.
189189
"""
190-
_validate_array_value(self, 'ui_locales_supported')
190+
validate_array_value(self, 'ui_locales_supported')
191191

192192
def validate_op_policy_uri(self):
193193
"""OPTIONAL. URL that the authorization server provides to the
@@ -234,7 +234,7 @@ def validate_revocation_endpoint_auth_methods_supported(self):
234234
"client_secret_basic" -- the HTTP Basic Authentication Scheme
235235
specified in Section 2.3.1 of OAuth 2.0 [RFC6749].
236236
"""
237-
_validate_array_value(self, 'revocation_endpoint_auth_methods_supported')
237+
validate_array_value(self, 'revocation_endpoint_auth_methods_supported')
238238

239239
def validate_revocation_endpoint_auth_signing_alg_values_supported(self):
240240
"""OPTIONAL. JSON array containing a list of the JWS signing
@@ -273,7 +273,7 @@ def validate_introspection_endpoint_auth_methods_supported(self):
273273
omitted, the set of supported authentication methods MUST be
274274
determined by other means.
275275
"""
276-
_validate_array_value(self, 'introspection_endpoint_auth_methods_supported')
276+
validate_array_value(self, 'introspection_endpoint_auth_methods_supported')
277277

278278
def validate_introspection_endpoint_auth_signing_alg_values_supported(self):
279279
"""OPTIONAL. JSON array containing a list of the JWS signing
@@ -302,7 +302,7 @@ def validate_code_challenge_methods_supported(self):
302302
[IANA.OAuth.Parameters]. If omitted, the authorization server
303303
does not support PKCE.
304304
"""
305-
_validate_array_value(self, 'code_challenge_methods_supported')
305+
validate_array_value(self, 'code_challenge_methods_supported')
306306

307307
@property
308308
def response_modes_supported(self):
@@ -376,7 +376,7 @@ def _validate_alg_values(data, key, auth_methods_supported):
376376
'the value "none" MUST NOT be used in "{}"'.format(key))
377377

378378

379-
def _validate_array_value(metadata, key):
379+
def validate_array_value(metadata, key):
380380
values = metadata.get(key)
381381
if values is not None and not isinstance(values, list):
382382
raise ValueError('"{}" MUST be JSON array'.format(key))

authlib/oidc/core/grants/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def exists_nonce(self, nonce, request): # pragma: no cover
4141
4242
def exists_nonce(self, nonce, request):
4343
exists = AuthorizationCode.query.filter_by(
44-
client_id=req.client_id, nonce=nonce
44+
client_id=request.client_id, nonce=nonce
4545
).first()
4646
return bool(exists)
4747

authlib/oidc/core/grants/implicit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def exists_nonce(self, nonce, request):
2929
3030
def exists_nonce(self, nonce, request):
3131
exists = AuthorizationCode.query.filter_by(
32-
client_id=req.client_id, nonce=nonce
32+
client_id=request.client_id, nonce=nonce
3333
).first()
3434
return bool(exists)
3535

authlib/oidc/discovery/models.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from authlib.oauth2.rfc8414 import AuthorizationServerMetadata
2+
from authlib.oauth2.rfc8414.models import validate_array_value
23

34

45
class OpenIDProviderMetadata(AuthorizationServerMetadata):
@@ -53,7 +54,7 @@ def validate_acr_values_supported(self):
5354
"""OPTIONAL. JSON array containing a list of the Authentication
5455
Context Class References that this OP supports.
5556
"""
56-
_validate_array_value(self, 'acr_values_supported')
57+
validate_array_value(self, 'acr_values_supported')
5758

5859
def validate_subject_types_supported(self):
5960
"""REQUIRED. JSON array containing a list of the Subject Identifier
@@ -102,35 +103,35 @@ def validate_id_token_encryption_alg_values_supported(self):
102103
algorithms (alg values) supported by the OP for the ID Token to
103104
encode the Claims in a JWT.
104105
"""
105-
_validate_array_value(self, 'id_token_encryption_alg_values_supported')
106+
validate_array_value(self, 'id_token_encryption_alg_values_supported')
106107

107108
def validate_id_token_encryption_enc_values_supported(self):
108109
"""OPTIONAL. JSON array containing a list of the JWE encryption
109110
algorithms (enc values) supported by the OP for the ID Token to
110111
encode the Claims in a JWT.
111112
"""
112-
_validate_array_value(self, 'id_token_encryption_enc_values_supported')
113+
validate_array_value(self, 'id_token_encryption_enc_values_supported')
113114

114115
def validate_userinfo_signing_alg_values_supported(self):
115116
"""OPTIONAL. JSON array containing a list of the JWS signing
116117
algorithms (alg values) [JWA] supported by the UserInfo Endpoint
117118
to encode the Claims in a JWT. The value none MAY be included.
118119
"""
119-
_validate_array_value(self, 'userinfo_signing_alg_values_supported')
120+
validate_array_value(self, 'userinfo_signing_alg_values_supported')
120121

121122
def validate_userinfo_encryption_alg_values_supported(self):
122123
"""OPTIONAL. JSON array containing a list of the JWE encryption
123124
algorithms (alg values) [JWA] supported by the UserInfo Endpoint
124125
to encode the Claims in a JWT.
125126
"""
126-
_validate_array_value(self, 'userinfo_encryption_alg_values_supported')
127+
validate_array_value(self, 'userinfo_encryption_alg_values_supported')
127128

128129
def validate_userinfo_encryption_enc_values_supported(self):
129130
"""OPTIONAL. JSON array containing a list of the JWE encryption
130131
algorithms (enc values) [JWA] supported by the UserInfo Endpoint
131132
to encode the Claims in a JWT.
132133
"""
133-
_validate_array_value(self, 'userinfo_encryption_enc_values_supported')
134+
validate_array_value(self, 'userinfo_encryption_enc_values_supported')
134135

135136
def validate_request_object_signing_alg_values_supported(self):
136137
"""OPTIONAL. JSON array containing a list of the JWS signing
@@ -160,15 +161,15 @@ def validate_request_object_encryption_alg_values_supported(self):
160161
These algorithms are used both when the Request Object is passed
161162
by value and when it is passed by reference.
162163
"""
163-
_validate_array_value(self, 'request_object_encryption_alg_values_supported')
164+
validate_array_value(self, 'request_object_encryption_alg_values_supported')
164165

165166
def validate_request_object_encryption_enc_values_supported(self):
166167
"""OPTIONAL. JSON array containing a list of the JWE encryption
167168
algorithms (enc values) supported by the OP for Request Objects.
168169
These algorithms are used both when the Request Object is passed
169170
by value and when it is passed by reference.
170171
"""
171-
_validate_array_value(self, 'request_object_encryption_enc_values_supported')
172+
validate_array_value(self, 'request_object_encryption_enc_values_supported')
172173

173174
def validate_display_values_supported(self):
174175
"""OPTIONAL. JSON array containing a list of the display parameter
@@ -195,8 +196,6 @@ def validate_claim_types_supported(self):
195196
"""
196197
values = self.get('claim_types_supported')
197198
if not values:
198-
# If omitted, the implementation supports only normal Claims
199-
self['claim_types_supported'] = ['normal']
200199
return
201200

202201
if not isinstance(values, list):
@@ -212,15 +211,15 @@ def validate_claims_supported(self):
212211
for. Note that for privacy or other reasons, this might not be an
213212
exhaustive list.
214213
"""
215-
_validate_array_value(self, 'claims_supported')
214+
validate_array_value(self, 'claims_supported')
216215

217216
def validate_claims_locales_supported(self):
218217
"""OPTIONAL. Languages and scripts supported for values in Claims
219218
being returned, represented as a JSON array of BCP47 [RFC5646]
220219
language tag values. Not all languages and scripts are necessarily
221220
supported for all Claim values.
222221
"""
223-
_validate_array_value(self, 'claims_locales_supported')
222+
validate_array_value(self, 'claims_locales_supported')
224223

225224
def validate_claims_parameter_supported(self):
226225
"""OPTIONAL. Boolean value specifying whether the OP supports use of
@@ -251,6 +250,11 @@ def validate_require_request_uri_registration(self):
251250
"""
252251
_validate_boolean_value(self, 'require_request_uri_registration')
253252

253+
@property
254+
def claim_types_supported(self):
255+
# If omitted, the implementation supports only normal Claims
256+
return self.get('claim_types_supported', ['normal'])
257+
254258
@property
255259
def claims_parameter_supported(self):
256260
# If omitted, the default value is false.
@@ -271,14 +275,9 @@ def require_request_uri_registration(self):
271275
# If omitted, the default value is false.
272276
return self.get('require_request_uri_registration', False)
273277

278+
274279
def _validate_boolean_value(metadata, key):
275280
if key not in metadata:
276281
return
277282
if metadata[key] not in (True, False):
278283
raise ValueError('"{}" MUST be boolean'.format(key))
279-
280-
281-
def _validate_array_value(metadata, key):
282-
values = metadata.get(key)
283-
if values is not None and not isinstance(values, list):
284-
raise ValueError('"{}" MUST be JSON array'.format(key))

tests/core/test_oidc/test_discovery.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def test_validate_claim_types_supported(self):
154154
'claim_types_supported',
155155
['invalid']
156156
)
157+
metadata = OpenIDProviderMetadata()
158+
self.assertEqual(metadata.claim_types_supported, ['normal'])
157159

158160
def test_validate_claims_supported(self):
159161
self._call_validate_array(

0 commit comments

Comments
 (0)