Spaces:
Sleeping
Sleeping
Download server/src/middleware/requireAuth.ts from recursiverall/api-gateway: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/recursiverall/api-gateway/resolve/main/server/src/middleware/requireAuth.ts
- Command line
-
hf download hf://spaces/recursiverall/api-gateway/server/src/middleware/requireAuth.ts
-
curl -L -o requireAuth.ts https://huggingface.co/spaces/recursiverall/api-gateway/resolve/main/server/src/middleware/requireAuth.ts
1.48 kB
| import type { Request, Response, NextFunction } from 'express'; | |
| import { validateSession } from '../services/auth.js'; | |
| import { isTrustedRequest } from '../lib/ip-trust.js'; | |
| // Gate the /api/* admin surface behind a dashboard session (#35, item #2). | |
| // The token is the opaque session token issued by /api/auth/login|setup, sent | |
| // as `Authorization: Bearer <token>`. The /v1 proxy is NOT gated by this — it | |
| // keeps its own unified-API-key auth for app clients. | |
| // | |
| // Single-user convenience: a caller whose source IP is on the local machine or | |
| // the local network (loopback, RFC1918, link-local, IPv6 ULA / link-local) is | |
| // treated as already authenticated. The dashboard is intended for one operator | |
| // on a trusted network, so the login form is suppressed for those callers. | |
| // Remote callers still need a valid session token. See server/src/lib/ip-trust.ts | |
| // for the full policy and its limitations. | |
| export function requireAuth(req: Request, res: Response, next: NextFunction): void { | |
| if (isTrustedRequest(req)) { | |
| next(); | |
| return; | |
| } | |
| const token = req.headers.authorization?.replace(/^Bearer\s+/i, '') | |
| ?? (req.headers['x-dashboard-token'] as string | undefined); | |
| const session = validateSession(token); | |
| if (!session) { | |
| res.status(401).json({ error: { message: 'Authentication required', type: 'authentication_error' } }); | |
| return; | |
| } | |
| (req as Request & { user?: typeof session }).user = session; | |
| next(); | |
| } | |