11import time
22from datetime import datetime , timedelta
33from operator import add , sub
4- from typing import Any , Callable , Dict , Optional , TYPE_CHECKING , TypeVar , \
5- Union
4+ from typing import TYPE_CHECKING , Any , Callable , Dict , Optional , TypeVar , Union
65
76from jwt import PyJWT
8-
97from jwt_rsa .rsa import RSAPrivateKey , RSAPublicKey
108
9+
1110if TYPE_CHECKING :
1211 # pylama:ignore=E0602
1312 DateType = Union [timedelta , datetime , float , int , ellipsis ]
1413else :
1514 DateType = Union [timedelta , datetime , float , int , type (Ellipsis )]
1615
17- R = TypeVar ('R' )
16+ R = TypeVar ("R" )
1817
1918
2019class JWT :
21- __slots__ = ('__private_key' , '__public_key' , '__jwt' ,
22- '__expires' , '__nbf_delta' , '__algorithm' )
20+ __slots__ = (
21+ "__private_key" , "__public_key" , "__jwt" ,
22+ "__expires" , "__nbf_delta" , "__algorithm" ,
23+ )
2324
2425 DEFAULT_EXPIRATION = 86400 * 30 # one month
2526 NBF_DELTA = 20
2627 ALGORITHMS = tuple ({
27- ' RS256' , ' RS384' , ' RS512' , ' ES256' , ' ES384' ,
28- ' ES521' , ' ES512' , ' PS256' , ' PS384' , ' PS512'
28+ " RS256" , " RS384" , " RS512" , " ES256" , " ES384" ,
29+ " ES521" , " ES512" , " PS256" , " PS384" , " PS512" ,
2930 })
3031
3132 def __init__ (
@@ -34,7 +35,7 @@ def __init__(
3435 public_key : Optional [RSAPublicKey ] = None ,
3536 expires : Optional [int ] = None ,
3637 nbf_delta : Optional [int ] = None ,
37- algorithm : str = "RS512"
38+ algorithm : str = "RS512" ,
3839 ):
3940
4041 self .__private_key = private_key
@@ -48,7 +49,7 @@ def _date_to_timestamp(
4849 self ,
4950 value : DateType ,
5051 default : Callable [[], R ],
51- timedelta_func : Callable [[float , float ], int ] = add
52+ timedelta_func : Callable [[float , float ], int ] = add ,
5253 ) -> Union [int , float , R ]:
5354 if isinstance (value , timedelta ):
5455 return timedelta_func (time .time (), value .total_seconds ())
@@ -65,7 +66,7 @@ def encode(
6566 self ,
6667 expired : DateType = ...,
6768 nbf : DateType = ...,
68- ** claims : int
69+ ** claims : int ,
6970 ) -> str :
7071 if not self .__private_key :
7172 raise RuntimeError ("Can't encode without private key" )
@@ -75,17 +76,17 @@ def encode(
7576 exp = int (
7677 self ._date_to_timestamp (
7778 expired ,
78- lambda : time .time () + self .__expires
79- )
79+ lambda : time .time () + self .__expires ,
80+ ),
8081 ),
8182 nbf = int (
8283 self ._date_to_timestamp (
8384 nbf ,
8485 lambda : time .time () - self .__nbf_delta ,
85- timedelta_func = sub
86- )
86+ timedelta_func = sub ,
87+ ),
8788 ),
88- )
89+ ),
8990 )
9091
9192 return self .__jwt .encode (
@@ -95,7 +96,7 @@ def encode(
9596 ).decode ()
9697
9798 def decode (
98- self , token : str , verify : bool = True , ** kwargs : Any
99+ self , token : str , verify : bool = True , ** kwargs : Any ,
99100 ) -> Dict [str , Any ]:
100101 if not self .__public_key :
101102 raise RuntimeError ("Can't decode without public key" )
@@ -105,11 +106,11 @@ def decode(
105106 key = self .__public_key ,
106107 verify = verify ,
107108 algorithms = self .ALGORITHMS ,
108- ** kwargs
109+ ** kwargs ,
109110 )
110111
111112
112- if __name__ == ' __main__' :
113+ if __name__ == " __main__" :
113114 from jwt_rsa .rsa import generate_rsa
114115
115116 key , public = generate_rsa (2048 )
@@ -118,5 +119,5 @@ def decode(
118119
119120 token = jwt .encode ()
120121
121- print (' Token' , token )
122- print (' Content' , jwt .decode (token ))
122+ print (" Token" , token )
123+ print (" Content" , jwt .decode (token ))
0 commit comments