Skip to content

Commit 88542af

Browse files
committed
refac
1 parent fab19d6 commit 88542af

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

auth/auth_info_middleware.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from fastmcp.server.dependencies import get_access_token
99
from fastmcp.server.dependencies import get_http_headers
1010

11+
from auth.external_oauth_provider import SESSION_TIME
1112
from auth.oauth21_session_store import ensure_session_from_access_token
1213
from auth.oauth_types import WorkspaceAccessToken
1314

@@ -117,6 +118,9 @@ async def _process_request_for_auth(self, context: MiddlewareContext):
117118
else:
118119
# Standard GoogleProvider returns a base AccessToken;
119120
# wrap it in WorkspaceAccessToken for identical downstream handling
121+
verified_expires = getattr(
122+
verified_auth, "expires_at", None
123+
)
120124
access_token = WorkspaceAccessToken(
121125
token=token_str,
122126
client_id=getattr(
@@ -128,10 +132,9 @@ async def _process_request_for_auth(self, context: MiddlewareContext):
128132
)
129133
or [],
130134
session_id=f"google_oauth_{token_str[:8]}",
131-
expires_at=getattr(
132-
verified_auth, "expires_at", None
133-
)
134-
or int(time.time()) + 3600,
135+
expires_at=verified_expires
136+
if verified_expires is not None
137+
else int(time.time()) + SESSION_TIME,
135138
claims=getattr(
136139
verified_auth, "claims", {}
137140
)

auth/external_oauth_provider.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,25 @@
2727
GOOGLE_ISSUER_URL = "https://accounts.google.com"
2828

2929
# Configurable session time in seconds (default: 1 hour)
30-
SESSION_TIME = int(os.getenv("SESSION_TIME", "3600"))
30+
_DEFAULT_SESSION_TIME = 3600
31+
32+
33+
def _get_session_time() -> int:
34+
"""Parse SESSION_TIME from environment with fallback and minimum clamp."""
35+
raw = os.getenv("SESSION_TIME", "")
36+
if not raw:
37+
return _DEFAULT_SESSION_TIME
38+
try:
39+
value = int(raw)
40+
except ValueError:
41+
logger.warning(
42+
"Invalid SESSION_TIME=%r, falling back to %d", raw, _DEFAULT_SESSION_TIME
43+
)
44+
return _DEFAULT_SESSION_TIME
45+
return max(value, 1)
46+
47+
48+
SESSION_TIME = _get_session_time()
3149

3250

3351
class ExternalOAuthProvider(GoogleProvider):

0 commit comments

Comments
 (0)