Skip to content
20 changes: 17 additions & 3 deletions hermes_cli/dashboard_auth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
from __future__ import annotations

import asyncio
import logging
from typing import Awaitable, Callable

Expand Down Expand Up @@ -297,6 +298,10 @@ def _verify_bearer(request: Request, *, access_token: str):
401, so the desktop retries instead of dropping the user to full re-login.
Unlike the cookie path there is no server-side refresh — the desktop owns
its refresh token and rotates via ``/auth/native/refresh``.

Blocking: performs provider IDP I/O (``verify_session`` may re-fetch the
IDP's JWKS). Callers on the event loop must invoke it through
``asyncio.to_thread``.
"""
unreachable_provider: str | None = None
for provider in list_session_providers():
Expand Down Expand Up @@ -356,7 +361,9 @@ async def gated_auth_middleware(
bearer = _extract_bearer(request)
if bearer:
try:
bearer_session = _verify_bearer(request, access_token=bearer)
bearer_session = await asyncio.to_thread(
_verify_bearer, request, access_token=bearer
)
except ProviderError as e:
# At least one provider's IDP/JWKS was unreachable and none
# verified the token — transient outage, not bad credentials.
Expand Down Expand Up @@ -421,7 +428,9 @@ async def gated_auth_middleware(
unreachable_provider: str | None = None
for provider in _ordered_session_providers(provider_hint):
try:
session = provider.verify_session(access_token=at)
session = await asyncio.to_thread(
provider.verify_session, access_token=at
)
except ProviderError as e:
_log.warning(
"dashboard-auth: provider %r unreachable during verify: %s",
Expand Down Expand Up @@ -454,7 +463,8 @@ async def gated_auth_middleware(
# serve the request transparently; only after every provider rejects
# the RT do we fall through to clear-and-relogin.
try:
refreshed = _attempt_refresh(
refreshed = await asyncio.to_thread(
_attempt_refresh,
request,
refresh_token=_rt,
provider_hint=provider_hint,
Expand Down Expand Up @@ -555,6 +565,10 @@ def _attempt_refresh(request: Request, *, refresh_token, provider_hint: str | No
no RT or every reachable provider rejects it. If no provider succeeds and
at least one raised ``ProviderError``, re-raises with that provider's name
so the caller can return 503 without clearing potentially valid cookies.

Blocking: performs provider IDP I/O (``refresh_session`` is a token-endpoint
round trip). Callers on the event loop must invoke it through
``asyncio.to_thread``.
"""
if not refresh_token:
return None
Expand Down
24 changes: 17 additions & 7 deletions hermes_cli/dashboard_auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
from __future__ import annotations

import asyncio
import logging
import threading
import time
Expand Down Expand Up @@ -202,7 +203,9 @@ async def auth_login(request: Request, provider: str, next: str = ""):
return RedirectResponse(url=login_url, status_code=302)

try:
ls = p.start_login(redirect_uri=_redirect_uri(request))
ls = await asyncio.to_thread(
p.start_login, redirect_uri=_redirect_uri(request)
)
except ProviderError as e:
audit_log(
AuditEvent.LOGIN_FAILURE,
Expand Down Expand Up @@ -367,7 +370,9 @@ async def auth_native_authorize(
raise HTTPException(status_code=503, detail=str(e))

try:
ls = p.start_login(redirect_uri=_redirect_uri(request))
ls = await asyncio.to_thread(
p.start_login, redirect_uri=_redirect_uri(request)
)
except ProviderError as e:
raise HTTPException(status_code=503, detail=f"Provider unreachable: {e}")

Expand Down Expand Up @@ -467,7 +472,8 @@ async def auth_callback(
)

try:
session = p.complete_login(
session = await asyncio.to_thread(
p.complete_login,
code=code,
state=state,
code_verifier=verifier,
Expand Down Expand Up @@ -707,8 +713,10 @@ async def auth_password_login(request: Request, body: _PasswordLoginBody):
raise HTTPException(status_code=404, detail="Unknown provider")

try:
session = p.complete_password_login(
username=body.username, password=body.password
session = await asyncio.to_thread(
p.complete_password_login,
username=body.username,
password=body.password,
)
except InvalidCredentialsError:
audit_log(
Expand Down Expand Up @@ -765,7 +773,7 @@ async def auth_logout(request: Request):
# logged but never raised.
for provider in list_providers():
try:
provider.revoke_session(refresh_token=rt)
await asyncio.to_thread(provider.revoke_session, refresh_token=rt)
except Exception as e: # noqa: BLE001 — best-effort
_log.warning(
"dashboard-auth: revoke on %r failed: %s",
Expand Down Expand Up @@ -936,7 +944,9 @@ async def auth_native_refresh(request: Request, body: _NativeRefreshBody):
unreachable: str | None = None
for provider in providers:
try:
session = provider.refresh_session(refresh_token=body.refresh_token)
session = await asyncio.to_thread(
provider.refresh_session, refresh_token=body.refresh_token
)
except RefreshExpiredError:
continue
except ProviderError as e:
Expand Down
Loading
Loading