Auth Integrator
Designs and implements authentication (verifying identity) and authorization (controlling access) flows for existing applications — covering OAuth 2.0, JWT sessions, API key authentication, and role-based access control (RBAC).
When to Use
- User asks to "add authentication", "implement login", or "secure these endpoints"
- An API needs API key or JWT-based authentication
- OAuth 2.0 social login (Google, GitHub, etc.) needs to be integrated
- Role-based permissions need to be enforced on routes or resources
- Existing auth implementation has security gaps
- User asks to implement refresh token rotation or session management
Process
Clarify the auth requirements:
- Authentication type: session cookies, JWT (stateless), API keys, OAuth 2.0, SAML, magic links
- Identity providers: local (username/password), Google, GitHub, Microsoft, etc.
- Authorization model: no authz, simple auth check, RBAC (roles), ABAC (attributes)
- Client type: SPA (cookie vs. token tradeoffs), mobile, server-to-server
- Framework: Express, FastAPI, Django, Spring Boot, etc.
Design the authentication flow based on the chosen type:
JWT (Stateless Bearer Tokens):
- POST
/auth/login → validate credentials → issue access_token (short-lived, 15min) + refresh_token (long-lived, 7–30d, stored in httpOnly cookie or DB)
- Protected routes: middleware extracts
Authorization: Bearer <token>, verifies signature and expiry, attaches req.user
- POST
/auth/refresh → validate refresh token → issue new access token + rotate refresh token
- POST
/auth/logout → invalidate refresh token (add to denylist or delete from DB)
OAuth 2.0 / OIDC:
- GET
/auth/oauth/google → redirect to provider authorization URL with state and PKCE
- GET
/auth/callback → exchange code for tokens → fetch user profile → upsert user in DB → issue local session/JWT
- Always verify
state param to prevent CSRF
API Keys:
- Generate cryptographically random key (32+ bytes):
crypto.randomBytes(32).toString('hex')
- Store only a hashed version (SHA-256) in the database, return the plaintext once
- Middleware: extract
X-API-Key header → hash it → look up in DB → attach associated user/scope
Design the authorization model:
- RBAC: define roles (
admin, editor, viewer), assign permissions per role, enforce in middleware
- Resource ownership: check
resource.ownerId === req.user.id before mutations
- Implement
requireRole(role) and requirePermission(permission) middleware factories
Implement secure password handling if using local auth:
- Hash with
bcrypt (cost factor 12) or argon2id
- Never store, log, or return plaintext passwords
- Implement rate limiting on login endpoint (max 5 attempts / 15min per IP)
Generate the implementation files:
- Auth router with login, register, refresh, logout endpoints
- Auth middleware for protecting routes
- JWT utility (sign, verify, refresh)
- User model with auth fields
- Role/permission definitions
Add security headers and configurations:
- httpOnly, Secure, SameSite=Strict cookies for refresh tokens
- Short expiry for access tokens
- Token rotation on every refresh
Output Format
### Files Generated
- `src/auth/auth.router.ts` — POST /auth/register, /auth/login, /auth/refresh, /auth/logout
- `src/auth/auth.service.ts` — validateCredentials, issueTokens, refreshTokens, revokeToken
- `src/auth/auth.middleware.ts`— requireAuth, requireRole(role), requirePermission(perm)
- `src/auth/jwt.util.ts` — signAccessToken, signRefreshToken, verifyToken
- `src/models/user.model.ts` — id, email, passwordHash, role, refreshTokens[]
src/auth/auth.middleware.ts
import { Request, Response, NextFunction } from 'express';
import { verifyToken } from './jwt.util';
export function requireAuth(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers.authorization;
if (!authHeader?.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing or invalid Authorization header' });
}
try {
const payload = verifyToken(authHeader.slice(7));
req.user = payload;
next();
} catch {
return res.status(401).json({ error: 'Invalid or expired token' });
}
}
export function requireRole(...roles: string[]) {
return (req: Request, res: Response, next: NextFunction) => {
if (!req.user || !roles.includes(req.user.role)) {
return res.status(403).json({ error: 'Insufficient permissions' });
}
next();
};
}
Examples
Example Input
Add JWT authentication to an Express.js API.
Users should be able to register with email/password.
Admin and user roles. Admins can access /admin/* routes.
Example Output (summary)
Implementation:
- POST /auth/register — hash password (bcrypt cost 12), create user (role: 'user'), return JWT
- POST /auth/login — verify credentials, return access token (15min) + set httpOnly refresh cookie
- POST /auth/refresh — validate cookie refresh token, rotate and return new access token
- POST /auth/logout — revoke refresh token, clear cookie
Middleware:
- requireAuth — validates Bearer JWT on all protected routes
- requireRole('admin') — applied to all /admin/* routes
Security:
- Passwords hashed with bcrypt (cost 12)
- Access tokens expire in 15 minutes
- Refresh tokens stored in DB, rotated on use, revoked on logout
- Rate limiting: 5 login attempts per IP per 15 minutes
Boundaries
- Do NOT implement authentication without HTTPS — note that TLS must be configured at the infrastructure level.
- Do NOT use
Math.random() or weak hashing for token generation — always use crypto.randomBytes() or equivalent.
- Do NOT use
alg: none or symmetric JWT secrets shorter than 256 bits.
- Do NOT implement "remember me" by simply extending JWT expiry — use proper refresh token rotation.
- Do NOT store sensitive auth tokens in
localStorage for web clients — prefer httpOnly cookies.
- If the user requests SAML or enterprise SSO, note that these are complex and recommend using an identity provider library or service (Auth0, Okta, Keycloak) rather than building from scratch.
- Always implement rate limiting on authentication endpoints — note if the framework for this is not detected in the project.
1---2name: auth-integrator3description: Adds authentication and authorization flows — OAuth 2.0, JWT, RBAC, API keys — to an existing application. Invoke when asked to add login, implement authentication, set up OAuth, add JWT tokens, implement role-based access control, or secure API endpoints.4---56# Auth Integrator78Designs and implements authentication (verifying identity) and authorization (controlling access) flows for existing applications — covering OAuth 2.0, JWT sessions, API key authentication, and role-based access control (RBAC).910## When to Use1112- User asks to "add authentication", "implement login", or "secure these endpoints"13- An API needs API key or JWT-based authentication14- OAuth 2.0 social login (Google, GitHub, etc.) needs to be integrated15- Role-based permissions need to be enforced on routes or resources16- Existing auth implementation has security gaps17- User asks to implement refresh token rotation or session management1819## Process20211. **Clarify the auth requirements**:22 - **Authentication type**: session cookies, JWT (stateless), API keys, OAuth 2.0, SAML, magic links23 - **Identity providers**: local (username/password), Google, GitHub, Microsoft, etc.24 - **Authorization model**: no authz, simple auth check, RBAC (roles), ABAC (attributes)25 - **Client type**: SPA (cookie vs. token tradeoffs), mobile, server-to-server26 - **Framework**: Express, FastAPI, Django, Spring Boot, etc.27282. **Design the authentication flow** based on the chosen type:2930 **JWT (Stateless Bearer Tokens)**:31 - POST `/auth/login` → validate credentials → issue `access_token` (short-lived, 15min) + `refresh_token` (long-lived, 7–30d, stored in httpOnly cookie or DB)32 - Protected routes: middleware extracts `Authorization: Bearer <token>`, verifies signature and expiry, attaches `req.user`33 - POST `/auth/refresh` → validate refresh token → issue new access token + rotate refresh token34 - POST `/auth/logout` → invalidate refresh token (add to denylist or delete from DB)3536 **OAuth 2.0 / OIDC**:37 - GET `/auth/oauth/google` → redirect to provider authorization URL with `state` and `PKCE`38 - GET `/auth/callback` → exchange `code` for tokens → fetch user profile → upsert user in DB → issue local session/JWT39 - Always verify `state` param to prevent CSRF4041 **API Keys**:42 - Generate cryptographically random key (32+ bytes): `crypto.randomBytes(32).toString('hex')`43 - Store only a hashed version (SHA-256) in the database, return the plaintext once44 - Middleware: extract `X-API-Key` header → hash it → look up in DB → attach associated user/scope45463. **Design the authorization model**:47 - **RBAC**: define roles (`admin`, `editor`, `viewer`), assign permissions per role, enforce in middleware48 - **Resource ownership**: check `resource.ownerId === req.user.id` before mutations49 - Implement `requireRole(role)` and `requirePermission(permission)` middleware factories50514. **Implement secure password handling** if using local auth:52 - Hash with `bcrypt` (cost factor 12) or `argon2id`53 - Never store, log, or return plaintext passwords54 - Implement rate limiting on login endpoint (max 5 attempts / 15min per IP)55565. **Generate the implementation files**:57 - Auth router with login, register, refresh, logout endpoints58 - Auth middleware for protecting routes59 - JWT utility (sign, verify, refresh)60 - User model with auth fields61 - Role/permission definitions62636. **Add security headers and configurations**:64 - httpOnly, Secure, SameSite=Strict cookies for refresh tokens65 - Short expiry for access tokens66 - Token rotation on every refresh6768## Output Format6970```71### Files Generated7273- `src/auth/auth.router.ts` — POST /auth/register, /auth/login, /auth/refresh, /auth/logout74- `src/auth/auth.service.ts` — validateCredentials, issueTokens, refreshTokens, revokeToken75- `src/auth/auth.middleware.ts`— requireAuth, requireRole(role), requirePermission(perm)76- `src/auth/jwt.util.ts` — signAccessToken, signRefreshToken, verifyToken77- `src/models/user.model.ts` — id, email, passwordHash, role, refreshTokens[]78```7980### `src/auth/auth.middleware.ts`81```ts82import { Request, Response, NextFunction } from 'express';83import { verifyToken } from './jwt.util';8485export function requireAuth(req: Request, res: Response, next: NextFunction) {86 const authHeader = req.headers.authorization;87 if (!authHeader?.startsWith('Bearer ')) {88 return res.status(401).json({ error: 'Missing or invalid Authorization header' });89 }90 try {91 const payload = verifyToken(authHeader.slice(7));92 req.user = payload;93 next();94 } catch {95 return res.status(401).json({ error: 'Invalid or expired token' });96 }97}9899export function requireRole(...roles: string[]) {100 return (req: Request, res: Response, next: NextFunction) => {101 if (!req.user || !roles.includes(req.user.role)) {102 return res.status(403).json({ error: 'Insufficient permissions' });103 }104 next();105 };106}107```108109## Examples110111### Example Input112```113Add JWT authentication to an Express.js API.114Users should be able to register with email/password.115Admin and user roles. Admins can access /admin/* routes.116```117118### Example Output (summary)119```120Implementation:121- POST /auth/register — hash password (bcrypt cost 12), create user (role: 'user'), return JWT122- POST /auth/login — verify credentials, return access token (15min) + set httpOnly refresh cookie123- POST /auth/refresh — validate cookie refresh token, rotate and return new access token124- POST /auth/logout — revoke refresh token, clear cookie125126Middleware:127- requireAuth — validates Bearer JWT on all protected routes128- requireRole('admin') — applied to all /admin/* routes129130Security:131- Passwords hashed with bcrypt (cost 12)132- Access tokens expire in 15 minutes133- Refresh tokens stored in DB, rotated on use, revoked on logout134- Rate limiting: 5 login attempts per IP per 15 minutes135```136137## Boundaries138139- Do NOT implement authentication without HTTPS — note that TLS must be configured at the infrastructure level.140- Do NOT use `Math.random()` or weak hashing for token generation — always use `crypto.randomBytes()` or equivalent.141- Do NOT use `alg: none` or symmetric JWT secrets shorter than 256 bits.142- Do NOT implement "remember me" by simply extending JWT expiry — use proper refresh token rotation.143- Do NOT store sensitive auth tokens in `localStorage` for web clients — prefer httpOnly cookies.144- If the user requests SAML or enterprise SSO, note that these are complex and recommend using an identity provider library or service (Auth0, Okta, Keycloak) rather than building from scratch.145- Always implement rate limiting on authentication endpoints — note if the framework for this is not detected in the project.