11from collections .abc import Callable
22from functools import wraps
3+ from typing import Iterable , Optional , Sequence
34
45from django .http .response import HttpResponse
56
67from 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+ )
0 commit comments