|
1 | 1 | import os |
2 | 2 | import httpx |
3 | | -from fastapi import Request |
| 3 | +import logging |
| 4 | +from fastapi import Request, HTTPException |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
4 | 7 |
|
5 | 8 | GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID") |
6 | 9 | GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET") |
7 | 10 |
|
8 | 11 | if not GITHUB_CLIENT_ID or not GITHUB_CLIENT_SECRET: |
| 12 | + logger.error("GitHub OAuth credentials missing!") |
| 13 | + logger.error(f"GITHUB_CLIENT_ID: {'present' if GITHUB_CLIENT_ID else 'missing'}") |
| 14 | + logger.error(f"GITHUB_CLIENT_SECRET: {'present' if GITHUB_CLIENT_SECRET else 'missing'}") |
9 | 15 | raise ValueError("❌ GitHub OAuth credentials not found") |
10 | 16 |
|
11 | 17 | async def exchange_code_for_token(code: str): |
12 | | - async with httpx.AsyncClient() as client: |
13 | | - response = await client.post( |
14 | | - "https://github.com/login/oauth/access_token", |
15 | | - headers={"Accept": "application/json"}, |
16 | | - data={ |
17 | | - "client_id": GITHUB_CLIENT_ID, |
18 | | - "client_secret": GITHUB_CLIENT_SECRET, |
19 | | - "code": code |
20 | | - } |
21 | | - ) |
22 | | - return response.json() |
| 18 | + if not code: |
| 19 | + raise HTTPException(status_code=400, detail="GitHub code is required") |
| 20 | + |
| 21 | + try: |
| 22 | + async with httpx.AsyncClient() as client: |
| 23 | + logger.info(f"Exchanging code for token with GitHub") |
| 24 | + response = await client.post( |
| 25 | + "https://github.com/login/oauth/access_token", |
| 26 | + headers={"Accept": "application/json"}, |
| 27 | + data={ |
| 28 | + "client_id": GITHUB_CLIENT_ID, |
| 29 | + "client_secret": GITHUB_CLIENT_SECRET, |
| 30 | + "code": code |
| 31 | + } |
| 32 | + ) |
| 33 | + response.raise_for_status() |
| 34 | + data = response.json() |
| 35 | + logger.info("Successfully exchanged code for token") |
| 36 | + return data |
| 37 | + except httpx.HTTPError as e: |
| 38 | + logger.error(f"HTTP error during token exchange: {str(e)}") |
| 39 | + logger.error(f"Response status: {e.response.status_code if hasattr(e, 'response') else 'unknown'}") |
| 40 | + logger.error(f"Response body: {e.response.text if hasattr(e, 'response') else 'unknown'}") |
| 41 | + raise HTTPException(status_code=500, detail=f"GitHub API error: {str(e)}") |
| 42 | + except Exception as e: |
| 43 | + logger.exception("Error exchanging code for token:") |
| 44 | + raise HTTPException(status_code=500, detail=str(e)) |
23 | 45 |
|
24 | 46 | async def get_user_info(access_token: str): |
25 | | - async with httpx.AsyncClient() as client: |
26 | | - response = await client.get( |
27 | | - "https://api.github.com/user", |
28 | | - headers={"Authorization": f"Bearer {access_token}"} |
29 | | - ) |
30 | | - return response.json() |
| 47 | + if not access_token: |
| 48 | + raise HTTPException(status_code=400, detail="Access token is required") |
| 49 | + |
| 50 | + try: |
| 51 | + async with httpx.AsyncClient() as client: |
| 52 | + logger.info("Fetching user info from GitHub") |
| 53 | + response = await client.get( |
| 54 | + "https://api.github.com/user", |
| 55 | + headers={"Authorization": f"Bearer {access_token}"} |
| 56 | + ) |
| 57 | + response.raise_for_status() |
| 58 | + data = response.json() |
| 59 | + logger.info("Successfully fetched user info") |
| 60 | + return data |
| 61 | + except httpx.HTTPError as e: |
| 62 | + logger.error(f"HTTP error fetching user info: {str(e)}") |
| 63 | + logger.error(f"Response status: {e.response.status_code if hasattr(e, 'response') else 'unknown'}") |
| 64 | + logger.error(f"Response body: {e.response.text if hasattr(e, 'response') else 'unknown'}") |
| 65 | + raise HTTPException(status_code=500, detail=f"GitHub API error: {str(e)}") |
| 66 | + except Exception as e: |
| 67 | + logger.exception("Error fetching user info:") |
| 68 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments