From a3daae1c40f2cb59e3866ea21ac5caaad4100ac2 Mon Sep 17 00:00:00 2001 From: Leo Borcherding Date: Tue, 24 Feb 2026 14:37:00 -0600 Subject: [PATCH] fix: replace datetime.UTC with timezone.utc for Python 3.9+ compatibility - Replace datetime.UTC with datetime.timezone.utc in authentication.py and storage.py - Fixes ImportError on Python versions < 3.11 - timezone.utc works on Python 3.9+ Resolves #237 --- studio/backend/auth/authentication.py | 6 +++--- studio/backend/auth/storage.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/studio/backend/auth/authentication.py b/studio/backend/auth/authentication.py index 6ea668db3e..e725834630 100644 --- a/studio/backend/auth/authentication.py +++ b/studio/backend/auth/authentication.py @@ -1,5 +1,5 @@ import secrets -from datetime import UTC, datetime, timedelta +from datetime import datetime, timedelta, timezone from typing import Optional from fastapi import Depends, HTTPException, status @@ -34,7 +34,7 @@ def create_access_token( Tokens are valid across restarts because SECRET_KEY is stored in SQLite. """ to_encode = {"sub": subject} - expire = datetime.now(UTC) + ( + expire = datetime.now(timezone.utc) + ( expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) ) to_encode.update({"exp": expire}) @@ -48,7 +48,7 @@ def create_refresh_token(subject: str) -> str: Refresh tokens are opaque (not JWTs) and expire after REFRESH_TOKEN_EXPIRE_DAYS. """ token = secrets.token_urlsafe(48) - expires_at = datetime.now(UTC) + timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS) + expires_at = datetime.now(timezone.utc) + timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS) save_refresh_token(token, subject, expires_at.isoformat()) return token diff --git a/studio/backend/auth/storage.py b/studio/backend/auth/storage.py index faea6266e3..e5a486bca2 100644 --- a/studio/backend/auth/storage.py +++ b/studio/backend/auth/storage.py @@ -3,7 +3,7 @@ SQLite storage for authentication data (user credentials + JWT secret). """ import hashlib import sqlite3 -from datetime import UTC, datetime +from datetime import datetime, timezone from pathlib import Path from typing import Optional, Tuple @@ -218,7 +218,7 @@ def verify_refresh_token(token: str) -> Optional[str]: # Clean up any expired tokens while we're here conn.execute( "DELETE FROM refresh_tokens WHERE expires_at < ?", - (datetime.now(UTC).isoformat(),), + (datetime.now(timezone.utc).isoformat(),), ) conn.commit() @@ -235,7 +235,7 @@ def verify_refresh_token(token: str) -> Optional[str]: # Check expiry expires_at = datetime.fromisoformat(row["expires_at"]) - if datetime.now(UTC) > expires_at: + if datetime.now(timezone.utc) > expires_at: conn.execute("DELETE FROM refresh_tokens WHERE id = ?", (row["id"],)) conn.commit() return None