notionhive-ai commited on
Commit
8adbc1e
·
verified ·
1 Parent(s): 30a502f

Update auth.py

Browse files
Files changed (1) hide show
  1. auth.py +41 -24
auth.py CHANGED
@@ -9,51 +9,68 @@ router = APIRouter()
9
 
10
  CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
11
  CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
12
- REDIRECT_URI = os.getenv("REDIRECT_URI")
13
 
14
- SCOPES = ['https://www.googleapis.com/auth/calendar']
15
 
16
- @router.get("/auth/login")
17
- def login():
18
- flow = Flow.from_client_config(
 
 
 
 
 
 
 
 
 
 
 
 
19
  {
20
  "web": {
21
  "client_id": CLIENT_ID,
22
  "client_secret": CLIENT_SECRET,
23
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
24
  "token_uri": "https://oauth2.googleapis.com/token",
25
- "redirect_uris": [REDIRECT_URI],
26
  }
27
  },
28
- scopes=SCOPES
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  )
30
- flow.redirect_uri = REDIRECT_URI
31
- auth_url, _ = flow.authorization_url(prompt='consent', include_granted_scopes='true', access_type='offline')
32
  return RedirectResponse(auth_url)
33
 
34
  @router.get("/auth/callback")
35
  async def auth_callback(request: Request):
36
  code = request.query_params.get("code")
37
- flow = Flow.from_client_config(
38
- {
39
- "web": {
40
- "client_id": CLIENT_ID,
41
- "client_secret": CLIENT_SECRET,
42
- "auth_uri": "https://accounts.google.com/o/oauth2/auth",
43
- "token_uri": "https://oauth2.googleapis.com/token",
44
- "redirect_uris": [REDIRECT_URI],
45
- }
46
- },
47
- scopes=SCOPES
48
- )
49
- flow.redirect_uri = REDIRECT_URI
50
  try:
51
  flow.fetch_token(code=code)
52
  credentials = flow.credentials
53
  return {
54
  "access_token": credentials.token,
55
  "refresh_token": credentials.refresh_token,
56
- "expiry": credentials.expiry.isoformat()
57
  }
58
  except Exception as e:
59
- raise HTTPException(status_code=500, detail=f"Failed to fetch token: {e}")
 
9
 
10
  CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
11
  CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
12
+ ENV_REDIRECT_URI = os.getenv("REDIRECT_URI") # optional override
13
 
14
+ SCOPES = ["https://www.googleapis.com/auth/calendar"]
15
 
16
+ def build_redirect_uri(request: Request) -> str:
17
+ """
18
+ Prefer env REDIRECT_URI if set.
19
+ Otherwise, derive from request with proxy headers so it works on HF + local.
20
+ """
21
+ if ENV_REDIRECT_URI:
22
+ return ENV_REDIRECT_URI.strip().rstrip("/")
23
+
24
+ # Respect reverse proxy headers (HF Spaces)
25
+ proto = request.headers.get("x-forwarded-proto") or request.url.scheme
26
+ host = request.headers.get("x-forwarded-host") or request.headers.get("host") or request.url.hostname
27
+ return f"{proto}://{host}/auth/callback".rstrip("/")
28
+
29
+ def build_flow(redirect_uri: str) -> Flow:
30
+ return Flow.from_client_config(
31
  {
32
  "web": {
33
  "client_id": CLIENT_ID,
34
  "client_secret": CLIENT_SECRET,
35
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
36
  "token_uri": "https://oauth2.googleapis.com/token",
37
+ "redirect_uris": [redirect_uri],
38
  }
39
  },
40
+ scopes=SCOPES,
41
+ )
42
+
43
+ @router.get("/auth/login")
44
+ def login(request: Request):
45
+ redirect_uri = build_redirect_uri(request)
46
+ flow = build_flow(redirect_uri)
47
+ flow.redirect_uri = redirect_uri
48
+
49
+ # IMPORTANT: offline access so we get refresh_token
50
+ auth_url, _ = flow.authorization_url(
51
+ prompt="consent",
52
+ include_granted_scopes="true",
53
+ access_type="offline",
54
  )
 
 
55
  return RedirectResponse(auth_url)
56
 
57
  @router.get("/auth/callback")
58
  async def auth_callback(request: Request):
59
  code = request.query_params.get("code")
60
+ if not code:
61
+ raise HTTPException(status_code=400, detail="Missing code")
62
+
63
+ redirect_uri = build_redirect_uri(request)
64
+ flow = build_flow(redirect_uri)
65
+ flow.redirect_uri = redirect_uri
66
+
 
 
 
 
 
 
67
  try:
68
  flow.fetch_token(code=code)
69
  credentials = flow.credentials
70
  return {
71
  "access_token": credentials.token,
72
  "refresh_token": credentials.refresh_token,
73
+ "expiry": credentials.expiry.isoformat() if credentials.expiry else None,
74
  }
75
  except Exception as e:
76
+ raise HTTPException(status_code=500, detail=f"Failed to fetch token: {e}")