File size: 1,543 Bytes
37f768f
 
 
8c63527
37f768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d26cf8
 
37f768f
93f1280
37f768f
 
 
 
bac337d
37f768f
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Handles the google OAuth 2.0 redirect callback"""

from google_auth_flow import exchange_code_for_token
from token_store import save_token

def handle_oauth_callback(code: str, state: str) -> dict:
    """
    Exchanges the OAuth authorization code for tokens and persists them.
 
    Args:
        code:  The `code` query parameter from the callback URL.
        state: The `state` parameter — we use it to carry the user's email.
 
    Returns:
        A dict with keys:
          - success (bool)
          - user_email (str)
          - message (str)
    """
    user_email = state  # we set state=user_email when building the auth URL
 
    if not code:
        return {"success": False, "user_email": user_email, "message": "No authorization code received."}
    if not user_email:
        return {"success": False, "user_email": "", "message": "No user email in OAuth state parameter."}
 
    # The instance where this would fail is when a user has mutiple auth flows created or
    # if there are multiple workers involved
    try:
        token_dict = exchange_code_for_token(code, state)
        save_token(user_email, token_dict)
        return {
            "success": True,
            "user_email": user_email,
            "message": f"✅ Google access granted and token saved for {user_email}. You can retry your request.",
        }
    except Exception as e:
        return {
            "success": False,
            "user_email": user_email,
            "message": f"OAuth token exchange failed: {str(e)}",
        }