Commit 8813ab8
Handle GitHub OAuth failures in /auth/callback without a 500 (#54)
## What
A user hit **Internal Server Error** on
`https://serge.huggingface.tech/login` today. The OAuth roundtrip got
through the token exchange but GitHub rejected the freshly-issued token
on `GET /user`:
```
POST github.com/login/oauth/access_token → 200 OK
GET api.github.com/user → 401 Unauthorized ("Bad credentials")
GET /auth/callback?... → 500 Internal Server Error
```
`auth_callback` called `user_resp.raise_for_status()`, so the 401
bubbled up as an unhandled `httpx.HTTPStatusError` → bare **500** with
no logging of *why*.
## Change
Each GitHub step in the callback (token exchange, `/user`, and the
org-membership-derived allow-list check) now:
- logs GitHub's own status + response body (so we can diagnose the
*next* failure), and
- redirects to `/login?error=<code>` instead of raising.
`login.html` maps the code to a readable message:
- `github_auth_failed` — "GitHub couldn't verify your sign-in. This is
usually temporary — please try again."
- `invalid_oauth_state` — expired/reused sign-in link
- `not_allowed` — account not on the allow-list
## Testing
Drove the real 401 path with a FastAPI `TestClient` + mocked GitHub
responses: the `/user` 401 now yields `302 →
/login?error=github_auth_failed` (no 500), and the log line reads `oauth
/user lookup failed: github returned 401: {"message":"Bad
credentials"}`. `ruff format`/`check` clean; no tests referenced the old
error strings.
Note: the underlying 401 on a just-minted token is most likely transient
(or an OAuth App `client_secret` drift) — this PR is the UX/robustness
fix so it surfaces cleanly instead of a 500.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 95deede commit 8813ab8
2 files changed
Lines changed: 68 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
9 | 19 | | |
10 | 20 | | |
11 | 21 | | |
| |||
17 | 27 | | |
18 | 28 | | |
19 | 29 | | |
| 30 | + | |
20 | 31 | | |
21 | 32 | | |
22 | 33 | | |
23 | 34 | | |
24 | 35 | | |
25 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
26 | 54 | | |
27 | 55 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2154 | 2154 | | |
2155 | 2155 | | |
2156 | 2156 | | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
2157 | 2164 | | |
2158 | 2165 | | |
2159 | 2166 | | |
| |||
2199 | 2206 | | |
2200 | 2207 | | |
2201 | 2208 | | |
2202 | | - | |
| 2209 | + | |
2203 | 2210 | | |
2204 | 2211 | | |
2205 | 2212 | | |
| |||
2211 | 2218 | | |
2212 | 2219 | | |
2213 | 2220 | | |
2214 | | - | |
2215 | | - | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
2216 | 2230 | | |
2217 | | - | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
| 2239 | + | |
2218 | 2240 | | |
2219 | 2241 | | |
2220 | 2242 | | |
| |||
2223 | 2245 | | |
2224 | 2246 | | |
2225 | 2247 | | |
2226 | | - | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
| 2252 | + | |
| 2253 | + | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
2227 | 2259 | | |
2228 | 2260 | | |
2229 | | - | |
| 2261 | + | |
| 2262 | + | |
2230 | 2263 | | |
2231 | 2264 | | |
2232 | 2265 | | |
| |||
2263 | 2296 | | |
2264 | 2297 | | |
2265 | 2298 | | |
2266 | | - | |
| 2299 | + | |
2267 | 2300 | | |
2268 | 2301 | | |
2269 | 2302 | | |
| |||
0 commit comments