一键导入
fastapi-jwt-user-context
provide fastapi jwt dependency, current_user from jwt, protected endpoint auth, fastapi jwt verification with shared secret
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
provide fastapi jwt dependency, current_user from jwt, protected endpoint auth, fastapi jwt verification with shared secret
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Generate Helm 3+ chart structure for todo/task applications. Use for: creating Chart.yaml, values.yaml, and templates/ directory with Deployment and Service manifests. Triggers: "helm chart", "helm template", "helm values", "create helm chart", "helm install", "helm upgrade", "package as helm chart". NOT for: plain Kubernetes YAML (use kubernetes-yaml-best-practices), Kustomize, or kubectl commands. NOT for: CRDs, hooks, subcharts, or advanced Helm features unless explicitly requested.
Apply Kubernetes pod security hardening with securityContext, Pod Security Standards, and container isolation. Use for: adding runAsNonRoot, readOnlyRootFilesystem, dropping capabilities, seccomp profiles, and privilege escalation prevention. Triggers: "secure pod", "pod security", "non-root container", "security context", "k8s security", "harden deployment", "pod security standards", "restricted PSS". NOT for: NetworkPolicies, RBAC, Secrets management, or service mesh security. Use alongside kubernetes-yaml-best-practices for complete manifests.
Generate production-ready Kubernetes YAML manifests following best practices. Use when creating or reviewing: Deployments, Services, ConfigMaps, Secrets, Ingress, PersistentVolumeClaims, or any k8s resource YAML. Triggers: "kubernetes manifest", "deployment yaml", "service yaml", "write k8s resource", "create kubernetes", "k8s yaml", "pod spec", "container spec". NOT for Helm charts, Kustomize overlays, or kubectl commands.
Deploy and test applications on local Minikube Kubernetes cluster. Use for: loading local Docker images to Minikube, exposing services locally via port-forward or minikube service, debugging pods (logs, describe, exec), troubleshooting CrashLoopBackOff/ImagePullBackOff errors. Triggers: "deploy to minikube", "minikube image load", "port-forward", "minikube service", "debug pod", "why is pod crashing", "minikube dashboard", "local kubernetes testing". NOT for: minikube installation, minikube start/stop, cloud Kubernetes, Helm charts, or writing YAML manifests (use kubernetes-yaml-best-practices for YAML).
OCI & OKE specialist for the Todo app hackathon project. Provides infrastructure context, authentication patterns, and deployment guidance for Oracle Kubernetes Engine in me-dubai-1. Use when working with: OKE cluster operations, kubectl commands, oci CLI, Helm deploys to OKE, Dapr/Kafka on OKE, Ingress/LoadBalancer setup, Kubernetes secrets, cloud deployment, OCI networking, node pool management, or troubleshooting OKE auth errors. Triggers: "OKE", "oci", "kubectl", "deploy to cloud", "cluster", "OCI", "oracle kubernetes", "ingress on OKE", "secrets on OKE", "Dapr on OKE", "Kafka on OKE", "node pool", "DOKS alternative".
Dapr service invocation pattern for FastAPI Todo app. Use DaprClient.invoke_method to call other services with user_id propagated via metadata. Use when implementing service-to-service communication in FastAPI applications with user context propagation.
| name | fastapi-jwt-user-context |
| description | provide fastapi jwt dependency, current_user from jwt, protected endpoint auth, fastapi jwt verification with shared secret |
This skill provides FastAPI JWT authentication patterns for extracting current_user context from JWT tokens. It implements secure token verification using shared secrets from BETTER_AUTH_SECRET environment variable, ensuring proper user context isolation and protected endpoint authorization.
Use the FastAPI Depends() pattern to extract current_user from JWT tokens:
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from jose import JWTError, jwt
from pydantic import BaseModel
import os
security = HTTPBearer()
class CurrentUser(BaseModel):
user_id: str
email: str
name: str = None
async def get_current_user(token: HTTPAuthorizationCredentials = Depends(security)) -> CurrentUser:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
# Get the shared secret from environment variable
SECRET_KEY = os.getenv("BETTER_AUTH_SECRET")
if not SECRET_KEY:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Authentication system not properly configured"
)
try:
payload = jwt.decode(token.credentials, SECRET_KEY, algorithms=["HS256"])
user_id: str = payload.get("sub")
email: str = payload.get("email")
if user_id is None or email is None:
raise credentials_exception
except JWTError:
raise credentials_exception
# Return Pydantic model with user context
return CurrentUser(user_id=user_id, email=email)
Use the dependency in your route definitions:
from fastapi import FastAPI
app = FastAPI()
@app.get("/protected-endpoint")
async def protected_route(current_user: CurrentUser = Depends(get_current_user)):
return {
"message": f"Hello {current_user.email}",
"user_id": current_user.user_id
}
For endpoints requiring user context matching (e.g., user-specific data):
@app.get("/api/{user_id}/tasks")
async def get_user_tasks(
user_id: str,
current_user: CurrentUser = Depends(get_current_user)
):
# Validate that requested user_id matches authenticated user
if current_user.user_id != user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Access denied: Cannot access another user's data"
)
# Return user's tasks
return {"tasks": [], "user_id": current_user.user_id}
Always raise appropriate HTTP exceptions:
Ensure the BETTER_AUTH_SECRET environment variable is set:
import os
SECRET_KEY = os.getenv("BETTER_AUTH_SECRET")
if not SECRET_KEY:
raise RuntimeError("BETTER_AUTH_SECRET environment variable is required")
pip install python-jose[cryptography]
# or
pip install PyJWT
When integrating with Better Auth for frontend authentication:
# In your frontend, attach the JWT token to API requests:
# Authorization: Bearer <jwt_token>
# Backend automatically extracts user context via dependency
@app.post("/api/{user_id}/tasks")
async def create_task(
user_id: str,
task_data: dict,
current_user: CurrentUser = Depends(get_current_user)
):
# Validate user context
if current_user.user_id != user_id:
raise HTTPException(status_code=403, detail="Forbidden")
# Process task creation for authenticated user
return {"task_id": 123, "status": "created"}
Before implementing JWT authentication: