Skip to content

Commit 06600c5

Browse files
committed
Sem-ver: bugfix Apply ruff formatting and use ruff for linting
Signed-off-by: David Black <[email protected]>
1 parent 20eb8d9 commit 06600c5

Some content is hidden

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

49 files changed

+1367
-1260
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ jobs:
1919
- name: Install dependencies
2020
run: |
2121
python -m pip install --upgrade pip wheel setuptools
22-
pip install -q pycodestyle==2.9.1 flake8==5.0.4
22+
pip install -q ruff
2323
- name: Lint
2424
run: |
25-
pycodestyle .
26-
flake8 .
25+
ruff check .
26+
ruff format --check .
2727
- name: Test
2828
run: |
2929
pip install wheel

atlassian_jwt_auth/algorithms.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
def get_permitted_algorithm_names():
2-
""" returns permitted algorithm names. """
2+
"""returns permitted algorithm names."""
33
return [
4-
'RS256',
5-
'RS384',
6-
'RS512',
7-
'ES256',
8-
'ES384',
9-
'ES512',
10-
'PS256',
11-
'PS384',
12-
'PS512'
4+
"RS256",
5+
"RS384",
6+
"RS512",
7+
"ES256",
8+
"ES384",
9+
"ES512",
10+
"PS256",
11+
"PS384",
12+
"PS512",
1313
]

atlassian_jwt_auth/auth.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ class BaseJWTAuth(object):
99
def __init__(self, signer, audience, *args, **kwargs):
1010
self._audience = audience
1111
self._signer = signer
12-
self._additional_claims = kwargs.get('additional_claims', {})
12+
self._additional_claims = kwargs.get("additional_claims", {})
1313

1414
@classmethod
15-
def create(cls, issuer, key_identifier, private_key_pem, audience,
16-
**kwargs):
15+
def create(cls, issuer, key_identifier, private_key_pem, audience, **kwargs):
1716
"""Instantiate a JWTAuth while creating the signer inline"""
18-
signer = atlassian_jwt_auth.create_signer(issuer, key_identifier,
19-
private_key_pem, **kwargs)
17+
signer = atlassian_jwt_auth.create_signer(
18+
issuer, key_identifier, private_key_pem, **kwargs
19+
)
2020
return cls(signer, audience)
2121

2222
def _get_header_value(self):
23-
return b'Bearer ' + self._signer.generate_jwt(
24-
self._audience, additional_claims=self._additional_claims)
23+
return b"Bearer " + self._signer.generate_jwt(
24+
self._audience, additional_claims=self._additional_claims
25+
)

atlassian_jwt_auth/contrib/aiohttp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Provide asyncio support"""
2+
23
import sys
34

45
if sys.version_info >= (3, 5):
@@ -9,6 +10,7 @@
910
from .verifier import JWTAuthVerifier # noqa
1011
except ImportError as e:
1112
import warnings
13+
1214
warnings.warn(str(e))
1315

1416

atlassian_jwt_auth/contrib/aiohttp/auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ class JWTAuth(BaseJWTAuth, BasicAuth):
88
99
It should be aiohttp.BasicAuth subclass, so redefine its `__new__` method.
1010
"""
11+
1112
def __new__(cls, *args, **kwargs):
12-
return super().__new__(cls, '')
13+
return super().__new__(cls, "")
1314

1415
def encode(self):
1516
return self._get_header_value().decode(self.encoding)
1617

1718

18-
def create_jwt_auth(
19-
issuer, key_identifier, private_key_pem, audience, **kwargs):
19+
def create_jwt_auth(issuer, key_identifier, private_key_pem, audience, **kwargs):
2020
"""Instantiate a JWTAuth while creating the signer inline"""
21-
return JWTAuth.create(
22-
issuer, key_identifier, private_key_pem, audience, **kwargs)
21+
return JWTAuth.create(issuer, key_identifier, private_key_pem, audience, **kwargs)

atlassian_jwt_auth/contrib/aiohttp/key.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
from atlassian_jwt_auth.exceptions import PublicKeyRetrieverException
77
from atlassian_jwt_auth.key import (
88
PEM_FILE_TYPE,
9-
HTTPSPublicKeyRetriever as _HTTPSPublicKeyRetriever
9+
)
10+
from atlassian_jwt_auth.key import (
11+
HTTPSPublicKeyRetriever as _HTTPSPublicKeyRetriever,
1012
)
1113

1214

1315
class HTTPSPublicKeyRetriever(_HTTPSPublicKeyRetriever):
1416
"""A class for retrieving JWT public keys with aiohttp"""
17+
1518
_class_session = None
1619

1720
def __init__(self, base_url, *, loop=None):
@@ -23,32 +26,32 @@ def __init__(self, base_url, *, loop=None):
2326
def _get_session(self):
2427
if HTTPSPublicKeyRetriever._class_session is None:
2528
HTTPSPublicKeyRetriever._class_session = aiohttp.ClientSession(
26-
loop=self.loop)
29+
loop=self.loop
30+
)
2731
return HTTPSPublicKeyRetriever._class_session
2832

2933
def _convert_proxies_to_proxy_arg(self, url, requests_kwargs):
30-
""" returns a modified requests_kwargs dict that contains proxy
31-
information in a form that aiohttp accepts
32-
(it wants proxy information instead of a dict of proxies).
34+
"""returns a modified requests_kwargs dict that contains proxy
35+
information in a form that aiohttp accepts
36+
(it wants proxy information instead of a dict of proxies).
3337
"""
3438
proxy = None
35-
if 'proxies' in requests_kwargs:
39+
if "proxies" in requests_kwargs:
3640
scheme = urllib.parse.urlparse(url).scheme
37-
proxy = requests_kwargs['proxies'].get(scheme, None)
38-
del requests_kwargs['proxies']
39-
requests_kwargs['proxy'] = proxy
41+
proxy = requests_kwargs["proxies"].get(scheme, None)
42+
del requests_kwargs["proxies"]
43+
requests_kwargs["proxy"] = proxy
4044
return requests_kwargs
4145

4246
async def _retrieve(self, url, requests_kwargs):
43-
requests_kwargs = self._convert_proxies_to_proxy_arg(
44-
url, requests_kwargs)
47+
requests_kwargs = self._convert_proxies_to_proxy_arg(url, requests_kwargs)
4548
try:
46-
resp = await self._session.get(url, headers={'accept':
47-
PEM_FILE_TYPE},
48-
**requests_kwargs)
49+
resp = await self._session.get(
50+
url, headers={"accept": PEM_FILE_TYPE}, **requests_kwargs
51+
)
4952
resp.raise_for_status()
50-
self._check_content_type(url, resp.headers['content-type'])
53+
self._check_content_type(url, resp.headers["content-type"])
5154
return await resp.text()
5255
except aiohttp.ClientError as e:
53-
status_code = getattr(e, 'code', None)
56+
status_code = getattr(e, "code", None)
5457
raise PublicKeyRetrieverException(e, status_code=status_code)

atlassian_jwt_auth/contrib/aiohttp/verifier.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ async def verify_jwt(self, a_jwt, audience, leeway=0, **requests_kwargs):
2222
if asyncio.iscoroutine(public_key):
2323
public_key = await public_key
2424

25-
alg = jwt.get_unverified_header(a_jwt).get('alg', None)
25+
alg = jwt.get_unverified_header(a_jwt).get("alg", None)
2626
public_key_obj = self._load_public_key(public_key, alg)
2727
return self._decode_jwt(
28-
a_jwt, key_identifier, public_key_obj,
29-
audience=audience, leeway=leeway)
28+
a_jwt, key_identifier, public_key_obj, audience=audience, leeway=leeway
29+
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import warnings
22

3-
43
warnings.warn(
54
"The atlassian_jwt_auth.contrib.django package is deprecated in 4.0.0 "
65
"in favour of atlassian_jwt_auth.frameworks.django.",
7-
DeprecationWarning, stacklevel=2
6+
DeprecationWarning,
7+
stacklevel=2,
88
)

atlassian_jwt_auth/contrib/django/decorators.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@ def validate_asap(issuers=None, subjects=None, required=True):
1616
:param boolean required: Whether or not to require ASAP on this endpoint.
1717
Note that requirements will be still be verified if claims are present.
1818
"""
19+
1920
def validate_asap_decorator(func):
2021
@wraps(func)
2122
def validate_asap_wrapper(request, *args, **kwargs):
22-
asap_claims = getattr(request, 'asap_claims', None)
23+
asap_claims = getattr(request, "asap_claims", None)
2324
if required and not asap_claims:
24-
message = 'Unauthorized: Invalid or missing token'
25+
message = "Unauthorized: Invalid or missing token"
2526
response = HttpResponse(message, status=401)
26-
response['WWW-Authenticate'] = 'Bearer'
27+
response["WWW-Authenticate"] = "Bearer"
2728
return response
2829

2930
if asap_claims:
30-
iss = asap_claims['iss']
31+
iss = asap_claims["iss"]
3132
if issuers and iss not in issuers:
32-
message = 'Forbidden: Invalid token issuer'
33+
message = "Forbidden: Invalid token issuer"
3334
return HttpResponse(message, status=403)
3435

35-
sub = asap_claims.get('sub')
36+
sub = asap_claims.get("sub")
3637
if subjects and sub not in subjects:
37-
message = 'Forbidden: Invalid token subject'
38+
message = "Forbidden: Invalid token subject"
3839
return HttpResponse(message, status=403)
3940

4041
return func(request, *args, **kwargs)
4142

4243
return validate_asap_wrapper
44+
4345
return validate_asap_decorator
4446

4547

@@ -48,7 +50,9 @@ def requires_asap(issuers=None, subject_should_match_issuer=None, func=None):
4850
4951
:param list issuers: *required The 'iss' claims that this endpoint is from.
5052
"""
51-
return with_asap(func=func,
52-
required=True,
53-
issuers=issuers,
54-
subject_should_match_issuer=subject_should_match_issuer)
53+
return with_asap(
54+
func=func,
55+
required=True,
56+
issuers=issuers,
57+
subject_should_match_issuer=subject_should_match_issuer,
58+
)
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from django.conf import settings
22
from django.utils.deprecation import MiddlewareMixin
33

4-
from atlassian_jwt_auth.frameworks.django.middleware import (
5-
OldStyleASAPMiddleware
6-
)
4+
from atlassian_jwt_auth.frameworks.django.middleware import OldStyleASAPMiddleware
75

86

97
class ProxiedAsapMiddleware(OldStyleASAPMiddleware, MiddlewareMixin):
@@ -18,17 +16,17 @@ def __init__(self, get_response=None):
1816

1917
# Rely on this header to tell us if a request has been forwarded
2018
# from an ASAP-enabled service; will overwrite X-Forwarded-For
21-
self.xfwd = getattr(settings, 'ASAP_PROXIED_FORWARDED_FOR_HEADER',
22-
'HTTP_X_ASAP_FORWARDED_FOR')
19+
self.xfwd = getattr(
20+
settings, "ASAP_PROXIED_FORWARDED_FOR_HEADER", "HTTP_X_ASAP_FORWARDED_FOR"
21+
)
2322

2423
# This header won't always be set, i.e. some users will be anonymous
25-
self.xauth = getattr(settings, 'ASAP_PROXIED_AUTHORIZATION_HEADER',
26-
'HTTP_X_ASAP_AUTHORIZATION')
24+
self.xauth = getattr(
25+
settings, "ASAP_PROXIED_AUTHORIZATION_HEADER", "HTTP_X_ASAP_AUTHORIZATION"
26+
)
2727

2828
def process_request(self, request):
29-
error_response = super(ProxiedAsapMiddleware, self).process_request(
30-
request
31-
)
29+
error_response = super(ProxiedAsapMiddleware, self).process_request(request)
3230

3331
if error_response:
3432
return error_response
@@ -38,26 +36,26 @@ def process_request(self, request):
3836
return
3937

4038
request.asap_forwarded = True
41-
request.META['HTTP_X_FORWARDED_FOR'] = forwarded_for
39+
request.META["HTTP_X_FORWARDED_FOR"] = forwarded_for
4240

43-
asap_auth = request.META.pop('HTTP_AUTHORIZATION', None)
41+
asap_auth = request.META.pop("HTTP_AUTHORIZATION", None)
4442
orig_auth = request.META.pop(self.xauth, None)
4543

4644
# Swap original client header in to allow regular auth middleware
4745
if orig_auth is not None:
48-
request.META['HTTP_AUTHORIZATION'] = orig_auth
46+
request.META["HTTP_AUTHORIZATION"] = orig_auth
4947
if asap_auth is not None:
5048
request.META[self.xauth] = asap_auth
5149

5250
def process_view(self, request, view_func, view_args, view_kwargs):
53-
if not hasattr(request, 'asap_forwarded'):
51+
if not hasattr(request, "asap_forwarded"):
5452
return
5553

5654
# swap headers back into place
5755
asap_auth = request.META.pop(self.xauth, None)
58-
orig_auth = request.META.pop('HTTP_AUTHORIZATION', None)
56+
orig_auth = request.META.pop("HTTP_AUTHORIZATION", None)
5957

6058
if asap_auth is not None:
61-
request.META['HTTP_AUTHORIZATION'] = asap_auth
59+
request.META["HTTP_AUTHORIZATION"] = asap_auth
6260
if orig_auth is not None:
6361
request.META[self.xauth] = orig_auth

0 commit comments

Comments
 (0)