Skip to content

Commit 7ef3cbd

Browse files
authored
Improve JWT expiration time (#1449)
1 parent e55aa6b commit 7ef3cbd

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

agixt/MagicalAuth.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from typing import List, Optional
2626
from fastapi import Header, HTTPException
2727
from Globals import getenv, get_default_agent
28+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
2829
from datetime import datetime, timedelta
2930
from fastapi import HTTPException
3031
from agixtsdk import AGiXTSDK
@@ -527,9 +528,50 @@ def send_magic_link(
527528
raise HTTPException(
528529
status_code=401, detail="Invalid MFA token. Please try again."
529530
)
530-
expiration = datetime.now().replace(
531-
hour=0, minute=0, second=0, microsecond=0
532-
) + timedelta(days=1)
531+
532+
# --- Start Calculation ---
533+
534+
# 1. Get the timezone name from the environment variable
535+
tz_name = getenv("TZ")
536+
if not tz_name:
537+
print("Warning: TZ environment variable not set. Defaulting to UTC.")
538+
tz_name = "UTC" # Default to UTC if not set
539+
540+
# 2. Get the timezone object
541+
try:
542+
server_tz = ZoneInfo(tz_name)
543+
except ZoneInfoNotFoundError:
544+
print(f"Warning: Timezone '{tz_name}' not found. Defaulting to UTC.")
545+
server_tz = ZoneInfo("UTC") # Default to UTC if invalid
546+
547+
# 3. Get the current time *in the server's timezone*
548+
now = datetime.now(server_tz)
549+
550+
# 4. Calculate the next month and year based on the current aware time
551+
current_year = now.year
552+
current_month = now.month
553+
554+
next_month = current_month + 1
555+
next_year = current_year
556+
if next_month > 12:
557+
next_month = 1
558+
next_year += 1
559+
560+
# 5. Create the expiration datetime for the first day of the next month
561+
# at midnight *in the server's timezone*.
562+
# Crucially, associate the timezone directly during creation.
563+
expiration = datetime(
564+
year=next_year,
565+
month=next_month,
566+
day=1,
567+
hour=0,
568+
minute=0,
569+
second=0,
570+
microsecond=0,
571+
tzinfo=server_tz, # Associate the server's timezone
572+
)
573+
# --- End Calculation ---
574+
533575
self.token = jwt.encode(
534576
{
535577
"sub": str(user.id),

0 commit comments

Comments
 (0)