1
1
from datetime import datetime
2
2
from datetime import timedelta
3
+ from datetime import timezone
3
4
from typing import Any
4
5
from typing import Awaitable
5
6
from typing import Callable
27
28
from .claims import Claims
28
29
from .config import OAuth2Config
29
30
from .core import OAuth2Core
31
+ from .exceptions import OAuth2AuthenticationError
30
32
31
33
32
34
class Auth (AuthCredentials ):
@@ -51,7 +53,7 @@ def jwt_decode(cls, token: str) -> dict:
51
53
52
54
@classmethod
53
55
def jwt_create (cls , token_data : dict ) -> str :
54
- expire = datetime .utcnow ( ) + timedelta (seconds = cls .expires )
56
+ expire = datetime .now ( timezone . utc ) + timedelta (seconds = cls .expires )
55
57
return cls .jwt_encode ({** token_data , "exp" : expire })
56
58
57
59
@@ -106,7 +108,11 @@ async def authenticate(self, request: Request) -> Optional[Tuple[Auth, User]]:
106
108
if not scheme or not param :
107
109
return Auth (), User ()
108
110
109
- user = User (Auth .jwt_decode (param ))
111
+ token_data = Auth .jwt_decode (param )
112
+ if token_data ["exp" ] and token_data ["exp" ] < int (datetime .now (timezone .utc ).timestamp ()):
113
+ raise OAuth2AuthenticationError (401 , "Token expired" )
114
+
115
+ user = User (token_data )
110
116
auth = Auth (user .pop ("scope" , []))
111
117
auth .provider = auth .clients .get (user .get ("provider" ))
112
118
claims = auth .provider .claims if auth .provider else {}
0 commit comments