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

Commit 326c8ed

Browse files
committed
Did make blacken, isort and bandit and this was the result.
1 parent 7df46c4 commit 326c8ed

57 files changed

Lines changed: 4145 additions & 3641 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def run_tests(self):
6060
"Programming Language :: Python :: 3.5",
6161
"Programming Language :: Python :: 3.6",
6262
"Topic :: Software Development :: Libraries :: Python Modules"],
63+
extras_require={
64+
'docs': ['Sphinx', 'sphinx-autobuild', 'alabaster'],
65+
'quality': ['pylama', 'isort', 'eradicate', 'mypy', 'black', 'bandit'],
66+
},
6367
install_requires=[
6468
"oidcmsg>=0.6.3",
6569
"oidcservice>=0.6.3",

src/oidcendpoint/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import string
2+
23
# Since SystemRandom is not available on all systems
34
try:
45
import random.SystemRandom as rnd
56
except ImportError:
67
import random as rnd
78

8-
__version__ = '0.9.0'
9+
__version__ = "0.9.0"
910

1011

11-
DEF_SIGN_ALG = {"id_token": "RS256",
12-
"userinfo": "RS256",
13-
"request_object": "RS256",
14-
"client_secret_jwt": "HS256",
15-
"private_key_jwt": "RS256"}
12+
DEF_SIGN_ALG = {
13+
"id_token": "RS256",
14+
"userinfo": "RS256",
15+
"request_object": "RS256",
16+
"client_secret_jwt": "HS256",
17+
"private_key_jwt": "RS256",
18+
}
1619

1720
HTTP_ARGS = ["headers", "redirections", "connection_type"]
1821

1922
JWT_BEARER = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
2023

21-
URL_ENCODED = 'application/x-www-form-urlencoded'
24+
URL_ENCODED = "application/x-www-form-urlencoded"
2225
JSON_ENCODED = "application/json"
2326
JOSE_ENCODED = "application/jose"
2427

src/oidcendpoint/authn_event.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
class AuthnEvent(Message):
88
c_param = {
9-
'uid': SINGLE_REQUIRED_STRING,
10-
'salt': SINGLE_REQUIRED_STRING,
11-
'authn_info': SINGLE_REQUIRED_STRING,
12-
'authn_time': SINGLE_OPTIONAL_INT,
13-
'valid_until': SINGLE_OPTIONAL_INT
9+
"uid": SINGLE_REQUIRED_STRING,
10+
"salt": SINGLE_REQUIRED_STRING,
11+
"authn_info": SINGLE_REQUIRED_STRING,
12+
"authn_time": SINGLE_OPTIONAL_INT,
13+
"valid_until": SINGLE_OPTIONAL_INT,
1414
}
1515

1616
def valid(self, now=0):
1717
if now:
18-
return self['valid_until'] > now
18+
return self["valid_until"] > now
1919
else:
20-
return self['valid_until'] > time_sans_frac()
20+
return self["valid_until"] > time_sans_frac()
2121

2222
def expires_in(self):
23-
return self['valid_until'] - time_sans_frac()
23+
return self["valid_until"] - time_sans_frac()
2424

2525

2626
def create_authn_event(uid, salt, authn_info=None, **kwargs):
@@ -33,22 +33,22 @@ def create_authn_event(uid, salt, authn_info=None, **kwargs):
3333
:return:
3434
"""
3535

36-
args = {'uid': uid, 'salt': salt, 'authn_info': authn_info}
36+
args = {"uid": uid, "salt": salt, "authn_info": authn_info}
3737

3838
try:
39-
args['authn_time'] = int(kwargs['authn_time'])
39+
args["authn_time"] = int(kwargs["authn_time"])
4040
except KeyError:
4141
try:
42-
args['authn_time'] = int(kwargs['timestamp'])
42+
args["authn_time"] = int(kwargs["timestamp"])
4343
except KeyError:
44-
args['authn_time'] = time_sans_frac()
44+
args["authn_time"] = time_sans_frac()
4545

4646
try:
47-
args['valid_until'] = kwargs['valid_until']
47+
args["valid_until"] = kwargs["valid_until"]
4848
except KeyError:
4949
try:
50-
args['valid_until'] = args['authn_time'] + kwargs['expires_in']
50+
args["valid_until"] = args["authn_time"] + kwargs["expires_in"]
5151
except KeyError:
52-
args['valid_until'] = args['authn_time'] + 3600
52+
args["valid_until"] = args["authn_time"] + 3600
5353

5454
return AuthnEvent(**args)

src/oidcendpoint/authz/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def set(self, uid, client_id, permission):
2424
try:
2525
self.permdb[uid][client_id] = permission
2626
except KeyError:
27-
self.permdb[uid] = {client_id:permission}
27+
self.permdb[uid] = {client_id: permission}
2828

2929
def permissions(self, cookie=None, **kwargs):
3030
if cookie is None:
@@ -39,7 +39,7 @@ def permissions(self, cookie=None, **kwargs):
3939
b64, _ts, typ = val
4040

4141
info = cookie_value(b64)
42-
return self.get(info['sub'], info['client_id'])
42+
return self.get(info["sub"], info["client_id"])
4343

4444
def get(self, uid, client_id):
4545
try:

src/oidcendpoint/client_authn.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
logger = logging.getLogger(__name__)
1919

20-
__author__ = 'roland hedberg'
20+
__author__ = "roland hedberg"
2121

2222

2323
class AuthnFailure(Exception):
@@ -62,9 +62,9 @@ def basic_authn(authn):
6262
_tok = base64.b64decode(_tok)
6363
part = as_unicode(_tok).split(":")
6464
if len(part) == 2:
65-
return dict(zip(['id', 'secret'], part))
65+
return dict(zip(["id", "secret"], part))
6666
else:
67-
raise ValueError('Illegal token')
67+
raise ValueError("Illegal token")
6868

6969

7070
class ClientSecretBasic(ClientAuthnMethod):
@@ -77,9 +77,11 @@ class ClientSecretBasic(ClientAuthnMethod):
7777
def verify(self, request, authorization_info, **kwargs):
7878
client_info = basic_authn(authorization_info)
7979

80-
if self.endpoint_context.cdb[
81-
client_info['id']]["client_secret"] == client_info['secret']:
82-
return {'client_id': client_info['id']}
80+
if (
81+
self.endpoint_context.cdb[client_info["id"]]["client_secret"]
82+
== client_info["secret"]
83+
):
84+
return {"client_id": client_info["id"]}
8385
else:
8486
raise AuthnFailure()
8587

@@ -93,10 +95,11 @@ class ClientSecretPost(ClientSecretBasic):
9395
"""
9496

9597
def verify(self, request, **kwargs):
96-
if self.endpoint_context.cdb[
97-
request[
98-
'client_id']]["client_secret"] == request['client_secret']:
99-
return {'client_id': request['client_id']}
98+
if (
99+
self.endpoint_context.cdb[request["client_id"]]["client_secret"]
100+
== request["client_secret"]
101+
):
102+
return {"client_id": request["client_id"]}
100103
else:
101104
raise AuthnFailure("secrets doesn't match")
102105

@@ -109,7 +112,7 @@ def verify(self, request, authorization_info, **kwargs):
109112
if not authorization_info.startswith("Bearer "):
110113
raise AuthnFailure("Wrong type of authorization token")
111114

112-
return {'token': authorization_info.split(' ', 1)[1]}
115+
return {"token": authorization_info.split(" ", 1)[1]}
113116

114117

115118
class BearerBody(ClientSecretPost):
@@ -119,13 +122,12 @@ class BearerBody(ClientSecretPost):
119122

120123
def verify(self, request, **kwargs):
121124
try:
122-
return {'token': request['access_token']}
125+
return {"token": request["access_token"]}
123126
except KeyError:
124-
raise AuthnFailure('No access token')
127+
raise AuthnFailure("No access token")
125128

126129

127130
class JWSAuthnMethod(ClientAuthnMethod):
128-
129131
def verify(self, request, **kwargs):
130132
_jwt = JWT(self.endpoint_context.keyjar)
131133
try:
@@ -139,7 +141,7 @@ def verify(self, request, **kwargs):
139141
except AttributeError:
140142
logger.debug("authntoken: %s" % sanitize(ca_jwt))
141143

142-
request[verified_claim_name('client_assertion')] = ca_jwt
144+
request[verified_claim_name("client_assertion")] = ca_jwt
143145

144146
try:
145147
client_id = kwargs["client_id"]
@@ -150,12 +152,12 @@ def verify(self, request, **kwargs):
150152
# could be either my issuer id or the token endpoint
151153
if self.endpoint_context.issuer in ca_jwt["aud"]:
152154
pass
153-
elif self.endpoint_context.endpoint['token'].full_path in ca_jwt['aud']:
155+
elif self.endpoint_context.endpoint["token"].full_path in ca_jwt["aud"]:
154156
pass
155157
else:
156158
raise NotForMe("Not for me!")
157159

158-
return {'client_id': client_id, 'jwt': ca_jwt}
160+
return {"client_id": client_id, "jwt": ca_jwt}
159161

160162

161163
class ClientSecretJWT(JWSAuthnMethod):
@@ -180,21 +182,22 @@ class PrivateKeyJWT(JWSAuthnMethod):
180182
"bearer_body": BearerBody,
181183
"client_secret_jwt": ClientSecretJWT,
182184
"private_key_jwt": PrivateKeyJWT,
183-
"none": None
185+
"none": None,
184186
}
185187

186188
TYPE_METHOD = [(JWT_BEARER, JWSAuthnMethod)]
187189

188190

189191
def valid_client_info(cinfo):
190-
eta = cinfo.get('client_secret_expires_at', 0)
192+
eta = cinfo.get("client_secret_expires_at", 0)
191193
if eta != 0 and eta < utc_time_sans_frac():
192194
return False
193195
return True
194196

195197

196-
def verify_client(endpoint_context, request, authorization_info=None,
197-
get_client_id_from_token=None):
198+
def verify_client(
199+
endpoint_context, request, authorization_info=None, get_client_id_from_token=None
200+
):
198201
"""
199202
Initiated Guessing !
200203
@@ -209,78 +212,81 @@ def verify_client(endpoint_context, request, authorization_info=None,
209212
# fixes request = {} instead of str
210213
# "AttributeError: 'dict' object has no attribute 'startswith'" in oidcendpoint/endpoint.py(158)client_authentication()
211214
if isinstance(authorization_info, dict):
212-
strings_parade = ('{} {}'.format(k,v) for k,v in authorization_info.items())
213-
authorization_info = ' '.join(strings_parade)
215+
strings_parade = ("{} {}".format(k, v) for k, v in authorization_info.items())
216+
authorization_info = " ".join(strings_parade)
214217

215218
if authorization_info is None:
216-
if 'client_id' in request and 'client_secret' in request:
219+
if "client_id" in request and "client_secret" in request:
217220
auth_info = ClientSecretPost(endpoint_context).verify(request)
218-
auth_info['method'] = 'client_secret_post'
219-
elif 'client_assertion' in request:
221+
auth_info["method"] = "client_secret_post"
222+
elif "client_assertion" in request:
220223
auth_info = JWSAuthnMethod(endpoint_context).verify(request)
221224
# If symmetric key was used
222225
# auth_method = 'client_secret_jwt'
223226
# If asymmetric key was used
224-
auth_info['method'] = 'private_key_jwt'
225-
elif 'access_token' in request:
227+
auth_info["method"] = "private_key_jwt"
228+
elif "access_token" in request:
226229
auth_info = BearerBody(endpoint_context).verify(request)
227-
auth_info['method'] = 'bearer_body'
230+
auth_info["method"] = "bearer_body"
228231
else:
229232
raise UnknownOrNoAuthnMethod()
230233
else:
231-
if authorization_info.startswith('Basic '):
234+
if authorization_info.startswith("Basic "):
232235
auth_info = ClientSecretBasic(endpoint_context).verify(
233-
request, authorization_info)
234-
auth_info['method'] = 'client_secret_basic'
235-
elif authorization_info.startswith('Bearer '):
236+
request, authorization_info
237+
)
238+
auth_info["method"] = "client_secret_basic"
239+
elif authorization_info.startswith("Bearer "):
236240
auth_info = BearerHeader(endpoint_context).verify(
237-
request, authorization_info)
238-
auth_info['method'] = 'bearer_header'
241+
request, authorization_info
242+
)
243+
auth_info["method"] = "bearer_header"
239244
else:
240245
raise UnknownOrNoAuthnMethod(authorization_info)
241246

242247
try:
243-
client_id = auth_info['client_id']
248+
client_id = auth_info["client_id"]
244249
except KeyError:
245250
try:
246-
_token = auth_info['token']
251+
_token = auth_info["token"]
247252
except KeyError:
248-
logger.warning('No token')
253+
logger.warning("No token")
249254
else:
250255
if get_client_id_from_token:
251256
try:
252257
_id = get_client_id_from_token(endpoint_context, _token, request)
253258
except KeyError:
254-
raise ValueError('Unknown token')
259+
raise ValueError("Unknown token")
255260

256261
if _id:
257-
auth_info['client_id'] = _id
262+
auth_info["client_id"] = _id
258263
else:
259264
try:
260265
_cinfo = endpoint_context.cdb[client_id]
261266
except KeyError:
262-
raise ValueError('Unknown Client ID')
267+
raise ValueError("Unknown Client ID")
263268
else:
264269
if isinstance(_cinfo, str):
265270
try:
266271
_cinfo = endpoint_context.cdb[_cinfo]
267272
except KeyError:
268-
raise ValueError('Unknown Client ID')
273+
raise ValueError("Unknown Client ID")
269274

270275
try:
271276
valid_client_info(_cinfo)
272277
except KeyError:
273-
logger.warning('Client registration has timed out')
274-
raise ValueError('Not valid client')
278+
logger.warning("Client registration has timed out")
279+
raise ValueError("Not valid client")
275280
else:
276281
# store what authn method was used
277282
try:
278-
endpoint_context.cdb[client_id]['auth_method'][
279-
request.__class__.__name__] = auth_info['method']
283+
endpoint_context.cdb[client_id]["auth_method"][
284+
request.__class__.__name__
285+
] = auth_info["method"]
280286
except KeyError:
281287
try:
282-
endpoint_context.cdb[client_id]['auth_method'] = {
283-
request.__class__.__name__: auth_info['method']
288+
endpoint_context.cdb[client_id]["auth_method"] = {
289+
request.__class__.__name__: auth_info["method"]
284290
}
285291
except KeyError:
286292
pass

0 commit comments

Comments
 (0)