Skip to content

Commit 95945db

Browse files
committed
WIP: add typing
Signed-off-by: David Black <[email protected]>
1 parent c4b6283 commit 95945db

Some content is hidden

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

42 files changed

+325
-266
lines changed

atlassian_jwt_auth/__init__.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
from atlassian_jwt_auth.algorithms import get_permitted_algorithm_names # noqa
2-
2+
from atlassian_jwt_auth.key import (HTTPSPublicKeyRetriever, # noqa
3+
KeyIdentifier)
34
from atlassian_jwt_auth.signer import ( # noqa
4-
create_signer,
5-
create_signer_from_file_private_key_repository,
6-
)
7-
8-
from atlassian_jwt_auth.key import ( # noqa
9-
KeyIdentifier,
10-
HTTPSPublicKeyRetriever,
11-
)
12-
13-
from atlassian_jwt_auth.verifier import ( # noqa
14-
JWTAuthVerifier,
15-
)
5+
create_signer, create_signer_from_file_private_key_repository)
6+
from atlassian_jwt_auth.verifier import JWTAuthVerifier # noqa

atlassian_jwt_auth/auth.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import absolute_import
22

3-
from typing import Any
3+
from typing import Any, Union
44

55
import atlassian_jwt_auth
66
from atlassian_jwt_auth import KeyIdentifier
@@ -10,7 +10,8 @@
1010
class BaseJWTAuth(object):
1111
"""Adds a JWT bearer token to the request per the ASAP specification"""
1212

13-
def __init__(self, signer: JWTAuthSigner, audience: str, *args: Any, **kwargs: Any) -> None:
13+
def __init__(self, signer: JWTAuthSigner, audience: str,
14+
*args: Any, **kwargs: Any) -> None:
1415
self._audience = audience
1516
self._signer = signer
1617
self._additional_claims = kwargs.get('additional_claims', {})

atlassian_jwt_auth/contrib/aiohttp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
if sys.version_info >= (3, 5):
55
try:
66
import aiohttp # noqa
7+
78
from .auth import JWTAuth # noqa
89
from .key import HTTPSPublicKeyRetriever # noqa
910
from .verifier import JWTAuthVerifier # noqa

atlassian_jwt_auth/contrib/aiohttp/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union, Any, Dict
1+
from typing import Any, Dict, Union
22

33
from aiohttp import BasicAuth
44

@@ -14,7 +14,7 @@ class JWTAuth(BaseJWTAuth, BasicAuth):
1414
def __new__(cls, *args, **kwargs):
1515
return super().__new__(cls, '')
1616

17-
def encode(self) -> str :
17+
def encode(self) -> str:
1818
return self._get_header_value().decode(self.encoding)
1919

2020

atlassian_jwt_auth/contrib/aiohttp/key.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import asyncio
22
import urllib.parse
33
from asyncio import AbstractEventLoop
4-
from typing import Dict, Any, Optional
4+
from typing import Any, Dict, Optional
55

66
import aiohttp
77

88
from atlassian_jwt_auth.exceptions import PublicKeyRetrieverException
9-
from atlassian_jwt_auth.key import (
10-
PEM_FILE_TYPE,
9+
from atlassian_jwt_auth.key import PEM_FILE_TYPE
10+
from atlassian_jwt_auth.key import \
1111
HTTPSPublicKeyRetriever as _HTTPSPublicKeyRetriever
12-
)
1312

1413

1514
class HTTPSPublicKeyRetriever(_HTTPSPublicKeyRetriever):
1615
"""A class for retrieving JWT public keys with aiohttp"""
1716
_class_session = None
1817

19-
def __init__(self, base_url:str, *, loop: Optional[AbstractEventLoop]=None) -> None:
18+
def __init__(self, base_url: str, *,
19+
loop: Optional[AbstractEventLoop] = None) -> None:
2020
if loop is None:
2121
loop = asyncio.get_event_loop()
2222
self.loop = loop
@@ -28,7 +28,8 @@ def _get_session(self) -> aiohttp.ClientSession:
2828
loop=self.loop)
2929
return HTTPSPublicKeyRetriever._class_session
3030

31-
def _convert_proxies_to_proxy_arg(self, url: str, requests_kwargs: Dict[Any, Any]) -> Dict[str, Any]:
31+
def _convert_proxies_to_proxy_arg(
32+
self, url: str, requests_kwargs: Dict[Any, Any]) -> Dict[str, Any]:
3233
""" returns a modified requests_kwargs dict that contains proxy
3334
information in a form that aiohttp accepts
3435
(it wants proxy information instead of a dict of proxies).
@@ -41,7 +42,8 @@ def _convert_proxies_to_proxy_arg(self, url: str, requests_kwargs: Dict[Any, Any
4142
requests_kwargs['proxy'] = proxy
4243
return requests_kwargs
4344

44-
async def _retrieve(self, url:str , requests_kwargs: Dict[Any, Any]) -> str:
45+
async def _retrieve(
46+
self, url: str, requests_kwargs: Dict[Any, Any]) -> str:
4547
requests_kwargs = self._convert_proxies_to_proxy_arg(
4648
url, requests_kwargs)
4749
try:

atlassian_jwt_auth/contrib/aiohttp/verifier.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Dict, Any
2+
from typing import Any, Dict
33

44
import jwt
55

@@ -8,7 +8,8 @@
88

99

1010
class JWTAuthVerifier(_JWTAuthVerifier):
11-
async def verify_jwt(self, a_jwt: str, audience: str, leeway: int=0, **requests_kwargs: Any) -> Dict[Any, Any]:
11+
async def verify_jwt(self, a_jwt: str, audience: str,
12+
leeway: int = 0, **requests_kwargs: Any) -> Dict[Any, Any]:
1213
"""Verify if the token is correct
1314
1415
Returns:

atlassian_jwt_auth/contrib/django/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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.",
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from collections.abc import Callable
22
from functools import wraps
3+
from typing import Iterable, Optional, Sequence
34

45
from django.http.response import HttpResponse
56

67
from atlassian_jwt_auth.frameworks.django.decorators import with_asap
7-
from typing import Optional, Sequence, Iterable
88

99

10-
def validate_asap(issuers: Optional[Iterable[str]]=None, subjects: Optional[Iterable[str]]=None, required: bool=True) -> Callable[]:
10+
def validate_asap(
11+
issuers: Optional[Iterable[str]] = None,
12+
subjects: Optional[Iterable[str]] = None,
13+
required: bool = True,
14+
) -> Callable:
1115
"""Decorator to allow endpoint-specific ASAP authorization, assuming ASAP
1216
authentication has already occurred.
1317
@@ -18,39 +22,47 @@ def validate_asap(issuers: Optional[Iterable[str]]=None, subjects: Optional[Iter
1822
:param boolean required: Whether or not to require ASAP on this endpoint.
1923
Note that requirements will be still be verified if claims are present.
2024
"""
25+
2126
def validate_asap_decorator(func):
2227
@wraps(func)
2328
def validate_asap_wrapper(request, *args, **kwargs):
24-
asap_claims = getattr(request, 'asap_claims', None)
29+
asap_claims = getattr(request, "asap_claims", None)
2530
if required and not asap_claims:
26-
message = 'Unauthorized: Invalid or missing token'
31+
message = "Unauthorized: Invalid or missing token"
2732
response = HttpResponse(message, status=401)
28-
response['WWW-Authenticate'] = 'Bearer'
33+
response["WWW-Authenticate"] = "Bearer"
2934
return response
3035

3136
if asap_claims:
32-
iss = asap_claims['iss']
37+
iss = asap_claims["iss"]
3338
if issuers and iss not in issuers:
34-
message = 'Forbidden: Invalid token issuer'
39+
message = "Forbidden: Invalid token issuer"
3540
return HttpResponse(message, status=403)
3641

37-
sub = asap_claims.get('sub')
42+
sub = asap_claims.get("sub")
3843
if subjects and sub not in subjects:
39-
message = 'Forbidden: Invalid token subject'
44+
message = "Forbidden: Invalid token subject"
4045
return HttpResponse(message, status=403)
4146

4247
return func(request, *args, **kwargs)
4348

4449
return validate_asap_wrapper
50+
4551
return validate_asap_decorator
4652

4753

48-
def requires_asap(issuers: Optional[Iterable[str]]=None, subject_should_match_issuer: Optional[bool]=None, func: Optional[Callable]=None) ->:
54+
def requires_asap(
55+
issuers: Optional[Iterable[str]] = None,
56+
subject_should_match_issuer: Optional[bool] = None,
57+
func: Optional[Callable] = None,
58+
) -> Callable:
4959
"""Decorator for Django endpoints to require ASAP
5060
5161
:param list issuers: *required The 'iss' claims that this endpoint is from.
5262
"""
53-
return with_asap(func=func,
54-
required=True,
55-
issuers=issuers,
56-
subject_should_match_issuer=subject_should_match_issuer)
63+
return with_asap(
64+
func=func,
65+
required=True,
66+
issuers=issuers,
67+
subject_should_match_issuer=subject_should_match_issuer,
68+
)

atlassian_jwt_auth/contrib/django/middleware.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from typing import Optional, Any, Callable
1+
from typing import Any, Callable, Optional
22

33
from django.conf import settings
44
from django.utils.deprecation import MiddlewareMixin
55

6-
from atlassian_jwt_auth.frameworks.django.middleware import (
6+
from atlassian_jwt_auth.frameworks.django.middleware import \
77
OldStyleASAPMiddleware
8-
)
98

109

1110
class ProxiedAsapMiddleware(OldStyleASAPMiddleware, MiddlewareMixin):
@@ -14,7 +13,7 @@ class ProxiedAsapMiddleware(OldStyleASAPMiddleware, MiddlewareMixin):
1413
1514
This must come before any authentication middleware."""
1615

17-
def __init__(self, get_response: Optional[Any]=None) -> None:
16+
def __init__(self, get_response: Optional[Any] = None) -> None:
1817
super(ProxiedAsapMiddleware, self).__init__()
1918
self.get_response = get_response
2019

@@ -51,7 +50,8 @@ def process_request(self, request) -> Optional[str]:
5150
if asap_auth is not None:
5251
request.META[self.xauth] = asap_auth
5352

54-
def process_view(self, request: Any, view_func: Callable, view_args: Any, view_kwargs: Any) -> None:
53+
def process_view(self, request: Any, view_func: Callable,
54+
view_args: Any, view_kwargs: Any) -> None:
5555
if not hasattr(request, 'asap_forwarded'):
5656
return None
5757

atlassian_jwt_auth/contrib/flask_app/__init__.py

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

33
from .decorators import requires_asap # noqa
44

5-
65
warnings.warn(
76
"The atlassian_jwt_auth.contrib.flask_app package is deprecated in 4.0.0 "
87
"in favour of atlassian_jwt_auth.frameworks.flask.",

0 commit comments

Comments
 (0)