feat: add in-depth OAuth logging across auth_service, auth, providers, gmail_service
Agent-Logs-Url: https://github.com/christianlouis/InboxConverge/sessions/91c7dd2b-0d88-46a9-a711-3a4c69d99520 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
3470213fb3
commit
075ba92f09
@@ -49,6 +49,11 @@ class OAuthService:
|
||||
"""
|
||||
try:
|
||||
# Exchange code for token
|
||||
logger.debug(
|
||||
"OAuth [Google sign-in]: exchanging authorization code for tokens "
|
||||
"(redirect_uri=%s)",
|
||||
redirect_uri,
|
||||
)
|
||||
async with httpx.AsyncClient() as client:
|
||||
token_response = await client.post(
|
||||
"https://oauth2.googleapis.com/token",
|
||||
@@ -62,7 +67,12 @@ class OAuthService:
|
||||
)
|
||||
|
||||
if token_response.status_code != 200:
|
||||
logger.error(f"Google token exchange failed: {token_response.text}")
|
||||
logger.error(
|
||||
"OAuth [Google sign-in]: token exchange failed "
|
||||
"(status=%s, body=%s)",
|
||||
token_response.status_code,
|
||||
token_response.text,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Failed to exchange authorization code",
|
||||
@@ -72,12 +82,26 @@ class OAuthService:
|
||||
access_token = token_data.get("access_token")
|
||||
|
||||
if not access_token:
|
||||
logger.error(
|
||||
"OAuth [Google sign-in]: token exchange response contained "
|
||||
"no access_token (keys_present=%s)",
|
||||
list(token_data.keys()),
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="No access token received",
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
"OAuth [Google sign-in]: token exchange succeeded — "
|
||||
"scopes=%s, has_refresh_token=%s, expires_in=%s",
|
||||
token_data.get("scope", ""),
|
||||
bool(token_data.get("refresh_token")),
|
||||
token_data.get("expires_in"),
|
||||
)
|
||||
|
||||
# Get user info
|
||||
logger.debug("OAuth [Google sign-in]: fetching Google user profile")
|
||||
user_info_response = await client.get(
|
||||
"https://www.googleapis.com/oauth2/v2/userinfo",
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
@@ -85,7 +109,10 @@ class OAuthService:
|
||||
|
||||
if user_info_response.status_code != 200:
|
||||
logger.error(
|
||||
f"Google user info fetch failed: {user_info_response.text}"
|
||||
"OAuth [Google sign-in]: user-info fetch failed "
|
||||
"(status=%s, body=%s)",
|
||||
user_info_response.status_code,
|
||||
user_info_response.text,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
@@ -93,6 +120,12 @@ class OAuthService:
|
||||
)
|
||||
|
||||
user_info = user_info_response.json()
|
||||
logger.debug(
|
||||
"OAuth [Google sign-in]: user profile retrieved — "
|
||||
"email=%s, verified=%s",
|
||||
user_info.get("email"),
|
||||
user_info.get("verified_email"),
|
||||
)
|
||||
|
||||
return {
|
||||
"email": user_info.get("email"),
|
||||
@@ -109,7 +142,7 @@ class OAuthService:
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"OAuth error: {e}")
|
||||
logger.error("OAuth [Google sign-in]: unexpected error: %s", e)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="OAuth authentication failed",
|
||||
@@ -126,6 +159,7 @@ class OAuthService:
|
||||
Returns:
|
||||
Dict with access_token, refresh_token, and token_type
|
||||
"""
|
||||
logger.debug("OAuth: issuing application JWT tokens for user_id=%s", user.id)
|
||||
access_token = create_access_token(data={"sub": str(user.id)})
|
||||
refresh_token = create_refresh_token(data={"sub": str(user.id)})
|
||||
|
||||
|
||||
@@ -87,6 +87,13 @@ class GmailService:
|
||||
scopes=GMAIL_SCOPES,
|
||||
)
|
||||
self._service = None
|
||||
logger.debug(
|
||||
"OAuth [GmailService]: initialized — has_refresh_token=%s, "
|
||||
"has_client_id=%s, has_client_secret=%s",
|
||||
bool(refresh_token),
|
||||
bool(client_id),
|
||||
bool(client_secret),
|
||||
)
|
||||
|
||||
@property
|
||||
def service(self):
|
||||
@@ -244,7 +251,21 @@ class GmailService:
|
||||
operation="get_profile", status="success"
|
||||
).inc()
|
||||
GMAIL_API_DURATION_SECONDS.labels(operation="get_profile").observe(_dur)
|
||||
return result.get("emailAddress")
|
||||
email = result.get("emailAddress")
|
||||
logger.debug("OAuth [GmailService]: fetched email address=%s", email)
|
||||
return email
|
||||
except google.auth.exceptions.RefreshError as e:
|
||||
_dur = time.perf_counter() - _start
|
||||
GMAIL_API_REQUESTS_TOTAL.labels(
|
||||
operation="get_profile", status="error"
|
||||
).inc()
|
||||
GMAIL_API_DURATION_SECONDS.labels(operation="get_profile").observe(_dur)
|
||||
logger.error(
|
||||
"OAuth [GmailService]: token refresh failed while fetching email "
|
||||
"address — refresh token may be revoked. Detail: %s",
|
||||
e,
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
_dur = time.perf_counter() - _start
|
||||
GMAIL_API_REQUESTS_TOTAL.labels(
|
||||
@@ -501,6 +522,11 @@ class GmailService:
|
||||
current_token = self.credentials.token
|
||||
if current_token and current_token != self._initial_access_token:
|
||||
GMAIL_TOKEN_REFRESHES_TOTAL.inc()
|
||||
logger.debug(
|
||||
"OAuth [GmailService]: access token was auto-refreshed during API "
|
||||
"call; new expiry=%s",
|
||||
self.credentials.expiry,
|
||||
)
|
||||
return {
|
||||
"access_token": current_token,
|
||||
"expiry": self.credentials.expiry,
|
||||
|
||||
Reference in New Issue
Block a user