haonguyen1915/flex-kit/fastapi-auth
Claude Code and agent skill by haonguyen1915/flex-kit
- community
Checking that the SKILL.md file is still in the repository.
Authentication in FastAPI - choosing and implementing OAuth2 password flow with JWT bearer tokens, API keys, HTTP Basic, and scope-based access (RBAC), plus the shared rules (401 vs 403, WWW-Authenticate, hash-not-plaintext, constant-time compare, expiring tokens). Use when adding auth to FastAPI routes, picking an auth scheme, or reviewing FastAPI security code. Not for general REST shape (see fastapi-rest).
Install
npx vskill@latest install haonguyen1915/flex-kit/fastapi-authSKILL.md
The same SKILL.md was published earlier as haonguyen1915/flex-kit/fastapi-auth.
FastAPI Auth
Authentication in FastAPI is a dependency: a security scheme pulls a token/key off the request, a function validates it and returns the user, and routes depend on that function. Pick the scheme by the caller; obey the cross-cutting rules below whichever you pick.
Choose the scheme
| Caller | Scheme | Extractor | |
|---|---|---|---|
| End users / SPA / mobile | OAuth2 password + JWT bearer | OAuth2PasswordBearer | |
| Service-to-service / internal | API key | APIKeyHeader / APIKeyQuery | |
| Tooling / simple internal | HTTP Basic | HTTPBasic | |
| Per-route permissions | OAuth2 scopes (RBAC) | SecurityScopes + Security(...) |
Default to OAuth2 + JWT for user-facing APIs. Full code per scheme in references/.
The dependency shape (every scheme shares it)
Protect a whole router with dependencies=[Depends(get_current_user)]; take the user as a parameter on a route only when you need its value.
Cross-cutting rules (all schemes)
- Never store a plaintext password. Hash with a modern KDF (
pwdlib-> argon2/bcrypt),
store only the hash, verify on login.
- 401 vs 403.
401 Unauthorized= missing/invalid identity (send aWWW-Authenticate
header); 403 Forbidden = valid identity but not allowed (wrong scope/role). Don't mix them.
- Constant-time compare. Check API keys / basic passwords with
compare_digest, never
== - == leaks length and content through response timing.
- Expiring tokens. Give JWTs a near-future
exp; verify signature andexpon every
request; use refresh tokens for longevity.
- The signing key (`SECRET_KEY`) and API keys load from config/environment, never literals
in source; keep them out of the repo and logs, and rotate on leak.
- Authn once, authz per route. Authenticate with
get_current_user; gate permissions
per route with scopes (Security(get_current_user, scopes=[...])).
Review checklist
- [ ] auth is a
Depends/Securitydependency; routes never parse tokens inline - [ ] passwords hashed with a real KDF; plaintext never stored or logged
- [ ]
401(+WWW-Authenticate) for bad identity,403for insufficient scope - [ ] key/password checks use
compare_digest, not== - [ ] JWTs are signed, expiring, and verified (signature +
exp) each request - [ ] signing key + API keys come from config, not literals in source
- [ ] per-route permission enforced via
SecurityScopes/Security
Red Flags
- a signing key or API key hard-coded as a literal in source
- a plaintext password stored, compared, or written to a log
==used to check an API key or password (timing leak - usecompare_digest)- a JWT with no
exp, or one whose signature/expiry is never verified 403returned for "not logged in", or401for "logged in but not permitted"- token parsing copy-pasted into each route instead of one shared dependency
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingflex_kit/packs/frameworks/fastapi/skills/auth/fastapi-auth/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/haonguyen1915/flex-kit/fastapi-auth)