11"""OAuth authentication provider for Basic Memory MCP server."""
22
33import secrets
4- from datetime import datetime , timedelta
4+ from datetime import datetime , timedelta , timezone
55from typing import Dict , Optional
66
77import jwt
@@ -92,7 +92,7 @@ async def authorize(
9292 self .authorization_codes [auth_code ] = BasicMemoryAuthorizationCode (
9393 code = auth_code ,
9494 scopes = params .scopes or [],
95- expires_at = (datetime .utcnow ( ) + timedelta (minutes = 10 )).timestamp (),
95+ expires_at = (datetime .now ( timezone . utc ) + timedelta (minutes = 10 )).timestamp (),
9696 client_id = client .client_id ,
9797 code_challenge = params .code_challenge ,
9898 redirect_uri = params .redirect_uri ,
@@ -119,7 +119,7 @@ async def load_authorization_code(
119119
120120 if code and code .client_id == client .client_id :
121121 # Check if expired
122- if datetime .utcnow ( ).timestamp () > code .expires_at :
122+ if datetime .now ( timezone . utc ).timestamp () > code .expires_at :
123123 del self .authorization_codes [authorization_code ]
124124 return None
125125 return code
@@ -135,7 +135,7 @@ async def exchange_authorization_code(
135135 refresh_token = secrets .token_urlsafe (32 )
136136
137137 # Store tokens
138- expires_at = (datetime .utcnow ( ) + timedelta (hours = 1 )).timestamp ()
138+ expires_at = (datetime .now ( timezone . utc ) + timedelta (hours = 1 )).timestamp ()
139139
140140 self .access_tokens [access_token ] = BasicMemoryAccessToken (
141141 token = access_token ,
@@ -187,7 +187,7 @@ async def exchange_refresh_token(
187187 new_refresh_token = secrets .token_urlsafe (32 )
188188
189189 # Store new tokens
190- expires_at = (datetime .utcnow ( ) + timedelta (hours = 1 )).timestamp ()
190+ expires_at = (datetime .now ( timezone . utc ) + timedelta (hours = 1 )).timestamp ()
191191
192192 self .access_tokens [new_access_token ] = BasicMemoryAccessToken (
193193 token = new_access_token ,
@@ -220,7 +220,7 @@ async def load_access_token(self, token: str) -> Optional[BasicMemoryAccessToken
220220
221221 if access_token :
222222 # Check if expired
223- if access_token .expires_at and datetime .utcnow ( ).timestamp () > access_token .expires_at :
223+ if access_token .expires_at and datetime .now ( timezone . utc ).timestamp () > access_token .expires_at :
224224 logger .debug ("Token found in memory but expired, removing" )
225225 del self .access_tokens [token ]
226226 return None
@@ -262,8 +262,8 @@ def _generate_access_token(self, client_id: str, scopes: list[str]) -> str:
262262 "iss" : self .issuer_url ,
263263 "sub" : client_id ,
264264 "aud" : "basic-memory" ,
265- "exp" : datetime .utcnow ( ) + timedelta (hours = 1 ),
266- "iat" : datetime .utcnow ( ),
265+ "exp" : datetime .now ( timezone . utc ) + timedelta (hours = 1 ),
266+ "iat" : datetime .now ( timezone . utc ),
267267 "scopes" : scopes ,
268268 }
269269
0 commit comments