Claude-skill-registry fastapi-jwt-auth
This skill should be used when implementing secure, reusable JWT verification dependency for FastAPI routes. It ensures strict user isolation and identity verification using Better Auth secrets.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/fastapi-jwt-auth" ~/.claude/skills/majiayu000-claude-skill-registry-fastapi-jwt-auth && rm -rf "$T"
manifest:
skills/data/fastapi-jwt-auth/SKILL.mdsource content
FastAPI JWT Auth Middleware
This skill provides a secure, reusable JWT verification dependency for FastAPI routes.
Purpose
Implementing a secure, reusable JWT verification dependency for FastAPI routes to ensure strict user isolation and identity verification.
Capabilities
- Extracting
from request headers.Authorization: Bearer <token> - Verifying token signature using the
environment variable.BETTER_AUTH_SECRET - Decoding JWT payloads to extract authenticated
anduser_id
.email - Performing path-level validation to ensure the authenticated
matches theuser_id
variable in the route path.{user_id} - Standardized error handling with
:HTTPException
: Token missing, invalid signature, or expired.401 Unauthorized
: Authenticated user ID does not match the requested path resource.403 Forbidden
- Providing a
object injectable directly into route functions.current_user
Implementation Details
Security Pattern
Using
python-jose[cryptography] or PyJWT to handle verification.
from fastapi import Depends, HTTPException, status, Request from jose import jwt async def get_current_user(user_id: str, request: Request): auth_header = request.headers.get("Authorization") if not auth_header or not auth_header.startswith("Bearer "): raise HTTPException(status_code=401, detail="Invalid auth header") token = auth_header.split(" ")[1] payload = jwt.decode(token, BETTER_AUTH_SECRET, algorithms=["HS256"]) token_user_id = payload.get("user_id") if token_user_id != user_id: raise HTTPException(status_code=403, detail="Not authorized for this resource") return payload
Best Practices
- Loading
only once at startup.BETTER_AUTH_SECRET - Always validating the
against the path to prevent ID enumeration/access bypass.user_id - Using dependency injection to keep route logic clean and testable.