This commit is contained in:
olehhapuk
2026-12-01 12:12:13 +02:00
parent 6698ce32c7
commit ebf6ec2e39
67 changed files with 7317 additions and 1261 deletions
@@ -0,0 +1,285 @@
# @nestjs/authentication recipes
Full tutorial and reference: https://docs.nestjs.com/security/authentication.md (about 200 KB: fetch it and read the section you need). Imports are from `@nestjs/authentication` unless shown otherwise; ESM projects end relative imports in `.js`.
Everything marked "checked" ran in one app on Nest 12.5.1, `@nestjs/authentication` 0.1.6, `@nestjs/authorization` 0.1.4 and `@nestjs/jwt` 12.0.5: sessions, bearer tokens with refresh, API keys, the context service and the restrictions below.
Contents: how it fits together, the user type, password sign-in, sessions, bearer tokens, a secret the task fixes, API keys, reading the user in services, route options, production, testing, mistakes.
## How it fits together
- `AuthenticationModule.forRoot({...})` (or `forRootAsync()`) takes plain values only: lifetimes, secrets, URLs. It registers a **global guard**: every route answers 401 without credentials. `@Public()` (route or controller) skips authentication; `@Authenticate({ optional: true })` lets anonymous callers in with `user === null`.
- **Credential providers** are your own `@Injectable()` classes extending `SessionCookieProvider`, `JwtBearerProvider`, `ApiKeyProvider`, or `AuthenticationProvider` for anything else. Each registers itself in its constructor: `registry.registerProvider(this, { order })`. The guard asks them in ascending `order`; the first that returns a user wins. Two providers with the same order fail at startup.
- `validate()` (or `findKey()`) runs on every request and loads the current user, so a deleted user or a changed role takes effect at once. Keep roles out of token claims.
- Lifetimes are milliseconds or strings (`'15m'`, `'14d'`). `3600` is 3.6 seconds; a numeric lifetime under a minute logs a warning.
```typescript
// Tell the package your user type once; @CurrentUser(), AuthenticationContext and the providers follow it.
declare module '@nestjs/authentication' {
interface AuthenticationTypes {
user: User;
// refreshTokens: false; // with `refreshToken: false`, so issue() is typed without one
}
}
```
## Password sign-in
```typescript
@Injectable()
export class CredentialsService {
constructor(private readonly users: UsersRepository, private readonly passwordHasher: PasswordHasher) {}
async register(email: string, password: string) {
// Insert and map the unique violation to 409 (see nestjs-data); a lookup first races.
return this.users.create(email, await this.passwordHasher.hash(password));
}
/** The user, or null when the email or the password is wrong. */
async verify(email: string, password: string) {
const found = await this.users.findCredentials(email); // { user, passwordHash } | null
// No account: verify() checks a dummy hash, so the timing doesn't reveal which emails exist.
const valid = await this.passwordHasher.verify(password, found?.passwordHash);
if (!valid || !found) return null;
if (this.passwordHasher.needsRehash(found.passwordHash)) {
await this.users.updatePasswordHash(found.user.id, await this.passwordHasher.hash(password));
}
return found.user;
}
}
```
- Keep the hash inside the repository: everything else gets a user without it, so it can't leak into a response or a log.
- Answer a wrong password and an unknown email identically (same status, same body). Sign-up can still reveal that an address exists (409); if that matters, answer sign-up the same way too and email the owner instead.
- `hash()` refuses an empty password and one over 4 KiB; length and strength rules are yours (validate at sign-up, not at sign-in, so passwords set under older rules still work).
- Throttle sign-in and token routes per IP and per account (`nestjs-production`, and "Rate-limit sign-in" in the docs).
## Sessions (browser apps)
```typescript
@Injectable()
export class SessionAuth extends SessionCookieProvider<User> {
constructor(private readonly users: UsersRepository, registry: AuthenticationRegistry) {
super();
registry.registerProvider(this); // order 0
}
validate(session: SessionRecord) {
return this.users.findById(session.userId); // null ends the session
}
}
@Public()
@Controller('auth')
export class AuthController {
constructor(private readonly credentials: CredentialsService, private readonly signInService: SignInService) {}
@Post('sign-in')
@HttpCode(200)
async signIn(@Body({ schema: credentialsSchema }) body: Credentials) {
const user = await this.credentials.verify(body.email, body.password);
if (!user) throw new UnauthorizedException('Invalid email or password');
const { session } = await this.signInService.signIn(user.id, { method: 'password' }); // sets the cookie itself
return { mfaRequired: session.mfa === 'pending' };
}
@Post('sign-out')
@HttpCode(204)
async signOut() {
if (!(await this.signInService.signOut())) throw new UnauthorizedException(); // no session cookie
}
}
```
- `signIn()` creates the session, sets the cookie (`__Host-sid`: `HttpOnly`, `Secure`, `SameSite=Lax`), drops the browser's previous session (session fixation) and refuses a sign-in posted from another site (403).
- Locally over plain HTTP on a host other than `localhost`, set `session: { cookie: { secure: false } }`; the cookie is then named `sid` (checked).
- `SessionService.list(userId)` and `revoke(sessionId, { userId })` for a "where you're signed in" page; `signInService.signOutEverywhere(userId)` ends every session and refresh token. Rotate with `signInService.rotateSession()` after a privilege change.
- Cross-origin writes don't count as signed in. A web app on another origin goes in `session.trustedOrigins`; on Nest 12.1+ also call `app.enableCsrfProtection()`.
## Bearer tokens you issue (secret of 32+ bytes)
Checked on 0.1.6: with a 12-byte key the app stops at startup (`the key is not a valid HS256 key; HS256 needs a secret of at least 32 bytes`); with a 32-byte key the code below signs in and authenticates.
```typescript
// app.module.ts
AuthenticationModule.forRoot({
accessToken: { key: process.env.JWT_SECRET!, ttl: '15m' }, // issuer and audience are optional, checked when set
// refreshToken: false, // bearer-only APIs that make clients sign in again: no refresh-token store needed
}),
@Injectable()
export class JwtAuth extends JwtBearerProvider<User> {
constructor(private readonly users: UsersRepository, registry: AuthenticationRegistry) {
super(); // verifies with the module's accessToken options
registry.registerProvider(this, { order: 1 });
}
validate({ sub }: JwtClaims) {
return sub ? this.users.findById(sub) : null; // sub is a string: convert it for numeric ids
}
}
@Public()
@Controller('auth/token')
export class TokensController {
constructor(private readonly credentials: CredentialsService, private readonly tokens: TokenService) {}
@Post()
@HttpCode(200)
async issue(@Body({ schema: credentialsSchema }) body: Credentials) {
const user = await this.credentials.verify(body.email, body.password);
if (!user) throw new UnauthorizedException('Invalid email or password');
return this.tokens.issue(String(user.id), { method: 'password' }); // { accessToken, expiresIn, refreshToken }
}
@Post('refresh')
@HttpCode(200)
refresh(@Body('refreshToken') refreshToken: string) {
return this.tokens.refresh(refreshToken); // a new pair; an unknown, expired or reused token is a 401
}
@Post('revoke')
@HttpCode(204)
async revoke(@Body('refreshToken') refreshToken: string) {
await this.tokens.revoke(refreshToken); // same answer for unknown tokens (RFC 7009)
}
}
```
- Checked: refreshing works once per token. Reusing a spent refresh token is a 401 and revokes its whole family, so the newer token from the same sign-in stops working too: whoever stole one, both parties must sign in again.
- Access tokens stay valid until they expire, even after sign-out everywhere: keep them short (`15m`).
- A request with no credentials gets 401 with `errorCode: 'missing_credentials'` and `WWW-Authenticate: Bearer`; a bad or expired token gets 401 with an RFC 6750 `error="invalid_token"` challenge, which tells clients to refresh.
## A secret the task fixes
`accessToken.key` refuses an HS256 secret under 32 bytes. When the secret is given (an environment variable with a short default, a value other services verify with), keep it and don't transform it. Sign with `@nestjs/jwt` (`npm i @nestjs/jwt`), and verify through your own provider, so the global guard, `@Public()` and `@CurrentUser()` keep working. Checked on Nest 12.5 with `@nestjs/authentication` 0.1.6 and `@nestjs/jwt` 12.0.5, with an 11-byte secret: tokens signed elsewhere with the raw secret verify, a forged one gets 401.
```typescript
// app.module.ts
@Module({
imports: [
AuthenticationModule.forRoot({}), // no accessToken: this app's tokens come from @nestjs/jwt
JwtModule.register({ secret: process.env.JWT_SECRET!, signOptions: { expiresIn: '15m' } }), // the secret as given: no padding, no hashing
// ...
],
providers: [JwtAuth],
})
export class AppModule {}
```
```typescript
// jwt-auth.provider.ts
import { Injectable, type ExecutionContext } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthenticationError, AuthenticationProvider, AuthenticationRegistry } from '@nestjs/authentication';
@Injectable()
export class JwtAuth extends AuthenticationProvider<User> {
constructor(private readonly jwt: JwtService, private readonly users: UsersRepository, registry: AuthenticationRegistry) {
super();
registry.registerProvider(this);
}
async authenticate(context: ExecutionContext) {
const header = this.header(context, 'authorization');
if (!header?.startsWith('Bearer ')) return null; // no credentials: the guard answers 401
let sub: number;
try {
({ sub } = await this.jwt.verifyAsync<{ sub: number }>(header.slice(7))); // HS256, checks exp
} catch {
throw new AuthenticationError('Invalid or expired token'); // 401
}
const user = await this.users.findById(sub); // current user and role, on every request
if (!user) throw new AuthenticationError('Invalid or expired token');
return { user };
}
challenge() {
return 'Bearer'; // WWW-Authenticate on a 401
}
}
```
Sign in with `PasswordHasher.verify()` as above, then `return { accessToken: await this.jwt.signAsync({ sub: user.id }) }`. Tell the user the secret is too short for production use.
## API keys
```typescript
@Injectable()
export class ApiKeyAuth extends ApiKeyProvider<User> {
constructor(private readonly keys: ApiKeysRepository, private readonly users: UsersRepository, registry: AuthenticationRegistry) {
super({ prefix: 'acme' }); // keys look like acme_<id>_<secret>; the prefix lets secret scanners find leaks
registry.registerProvider(this, { order: 2 });
}
// Called with the id part of a key; the provider compares the SHA-256 in constant time and checks expiresAt.
async findKey(id: string) {
const key = await this.keys.findById(id);
const user = key && (await this.users.findById(key.userId));
return user ? { hash: key.hash, user, expiresAt: key.expiresAt } : null; // null: unknown, revoked, or owner gone
}
}
// Managed with a session only: a leaked key can't create more keys or revoke its owner's.
@Authenticate({ providers: [SessionAuth] })
@Controller('api-keys')
export class ApiKeysController {
constructor(private readonly apiKeyAuth: ApiKeyAuth, private readonly keys: ApiKeysRepository) {}
@Post()
async create(@CurrentUser('id') userId: string) {
const { key, id, hash } = this.apiKeyAuth.generate();
const expiresAt = new Date(Date.now() + 365 * 24 * 3600 * 1000);
await this.keys.insert({ id, userId, hash, expiresAt }); // store the hash, never the key
return { id, key, expiresAt }; // the only response that ever contains the key
}
@Delete(':id')
@HttpCode(204)
async revoke(@CurrentUser('id') userId: string, @Param('id') id: string) {
if (!(await this.keys.delete({ id, userId }))) throw new NotFoundException(); // another user's key: 404
}
}
```
- Checked: a key sent as `Authorization: Bearer acme_...` signs its owner in; on `@Authenticate({ providers: [SessionAuth] })` routes it's a 401 (so is a valid JWT); after `DELETE` the same key is a 401 on the next request.
- `@CurrentSession()` on a key request is `{ keyId, expiresAt, method: 'api-key' }`: log `keyId` to know which key did what. Keys never satisfy `@Authenticate({ mfa: true })`.
- Scopes or per-key limits: store them with the key, return them from `findKey()` through your own user or session shape, and check them in a policy.
- A key in another header (`X-API-Key`) needs your own `AuthenticationProvider` that reads `this.header(context, 'x-api-key')`, looks the key up by its SHA-256, throws `AuthenticationError` when it's unknown, and returns `{ user, session: { keyId } }`.
- Open event streams keep running after a key is revoked: end them (`nestjs-feature`, `references/http.md`, "Ending streams on revocation").
## Reading the user
- In controllers: `@CurrentUser()`, `@CurrentUser('id')` for one property, `@CurrentSession()` for the provider's session (checked shapes: a cookie session's `{ id, userId, createdAt, lastActiveAt, expiresAt }`, a JWT's claims, an API key's `{ keyId, expiresAt, method }`).
- In services: inject `AuthenticationContext` and call `requireUser()` (throws `AuthenticationError`, a 401 under a handler) or read `.user` (`null` for a guest). `run(result, fn)` runs code as a user outside a request: queue workers, cron jobs, tests. It's a singleton backed by `AsyncLocalStorage`, correct across concurrent requests and in anything the handler awaits (checked). No request-scoped providers needed.
## Route options
`@Authenticate({...})` on a class or method:
| Option | Effect |
| --- | --- |
| `optional: true` | Anonymous callers get through with `null`; bad credentials are still a 401. |
| `providers: [SessionAuth]` | Only these providers may authenticate the route; other credentials count as none (checked). |
| `mfa: true` | Requires a verified second factor (API keys never qualify). |
| `verifiedEmail: true` | Requires `emailVerified`; 403 `email_unverified` otherwise. |
## Production
- Stores: sessions, refresh tokens and the other features' state live in memory until you register a store (`AuthenticationStorage.registerSource()` with `sessions`, `refreshTokens`, `mfa`, `magicLinks`, `oidcStates`, `emailTokens`). With `NODE_ENV=production` the module refuses to start on in-memory stores that a configured feature uses. The docs' "Keep sessions and tokens in your database" builds a Drizzle store and ships contract tests to run against yours.
- Audit: `AuthenticationEvents.events$` (and diagnostics channels `nestjs:authentication:<type>`) emits `sign-in`, `sign-out`, `refresh-token-reused`, `mfa-failed`, `password-reset-requested` and more. Alert on `refresh-token-reused`.
- Secrets from a secret manager; HTTPS (the session cookie is `Secure`); `trustedOrigins` for a web app on another origin.
## Testing
- `.overrideProvider(PasswordHasher).useValue(new PasswordHasher({ logN: 10 }))`: the default cost is ~0.3 s and 128 MiB per hash.
- Sign a test user in without the sign-in route: `await app.get(TokenService).issue(user.id)` returns a bearer token; `SessionService.create()` a session cookie (checked for tokens).
- Run service code as a user without HTTP: `app.get(AuthenticationContext).run({ user }, () => service.create(...))` (checked).
- Keep one e2e test that signs in through the real route and calls a protected one: it catches a wrong guard order or a provider that never registered.
## Mistakes
- Returning early for an unknown email (`if (!user) throw`) before checking the password: the timing reveals which emails exist.
- `@Post()` sign-in without `@HttpCode(200)`.
- Hashing, padding or deriving a 32-byte key from a short secret: tokens no longer verify with the configured secret.
- Putting roles or permissions in token claims, then trusting them after they change.
- Forgetting `@Public()` on sign-up/sign-in (every route is closed by default), or putting it on a whole controller that also has private routes.
- Checking keys or tokens with `===` instead of the package's constant-time comparison; storing keys or refresh tokens in plaintext.
@@ -0,0 +1,172 @@
# @nestjs/authorization recipes
Tutorial and reference: https://docs.nestjs.com/security/authorization.md. Everything marked "checked" ran on Nest 12.5.1 with `@nestjs/authorization` 0.1.4 and `@nestjs/authentication` 0.1.6.
Contents: register, policies, `before()`, policies with services, route checks, record checks in services, 401 or 403, errors and events, another authentication, testing, production.
## Register
```typescript
@Module({
imports: [
AuthenticationModule.forRoot({ ... }), // first: its guard sets the user that authorization reads
AuthorizationModule.forRoot(), // global module + global guard
NotesModule,
],
})
export class AppModule {}
```
- Global guards run in the order modules are scanned. With `AuthenticationModule` second, the app refuses to start.
- **Deny by default** (checked): a handler with no `@Can()`, `@Can.Anyone()` or `@Public()` answers 403 to a signed-in user (401 to a guest), and the startup log lists every such handler: `AuthorizationGuard denies every call to AuthController.me, ...: they declare no check`. Read that line after adding the module: it's the list of routes you just closed.
## Policies
```typescript
import { Policy, type PolicyBefore } from '@nestjs/authorization';
@Policy()
export class NotePolicy implements PolicyBefore<User> {
constructor(private readonly quotas: QuotaService) {} // a policy is a provider: it can inject services
// Runs ahead of every ability of this policy, on routes and in services alike.
before(user: User | null) {
if (user?.role === 'admin') return true; // true allows, false denies, nothing lets the ability decide
}
// Route abilities: the guard has no record, and may have a guest (null).
create(user: User | null) {
return !!user;
}
// Record abilities: called from the service with the record.
edit(user: User, note: Note) {
return note.ownerId === user.id;
}
// Abilities may be async; guard, authorize() and can() await them.
async createMore(user: User, current: number) {
return current < (await this.quotas.maxNotesFor(user));
}
}
```
- An ability's first parameter is the user; it returns `boolean` or `Promise<boolean>`, and only a literal `true` allows.
- Register the policy in its feature module's `providers` with the services it injects; `@Policy()` makes it injectable and the module finds it anywhere.
- Policies must be singletons: a request-scoped policy, or one depending on a request-scoped provider, throws at startup. Pass per-request facts as arguments.
- `before()` skips every ability of its policy, including rules that should bind admins too ("nobody refunds a shipped order"). For those, check the ability name `before()` receives, or keep them in a policy without a bypass.
- Checked: with `before()` above, an admin creates past the quota (`createMore` never runs) and edits other users' notes; a user over the quota gets 403.
## Route checks
```typescript
@Controller('notes')
export class NotesController {
@Post()
@Can(NotePolicy, 'create') // the guard calls create(user) before the handler
create(@Body({ schema: createNoteSchema }) body: CreateNote) { ... }
@Get()
@Can.Anyone() // anyone authentication lets through; the service decides what each caller sees
list() { ... }
// viewNotesOf(user: User | null, userId: string) in the policy: the third argument supplies userId
@Get('users/:userId/notes')
@Can(NotePolicy, 'viewNotesOf', (context) => [context.switchToHttp().getRequest().params.userId])
listFor(@Param('userId') userId: string) { ... }
}
```
- `@Can()` is type-checked: a misspelt ability, or one that needs a record, doesn't compile on a route.
- The argument function runs in the guard, before pipes: params are raw strings. Pass ids, not records (loading the record in the guard loads it twice and answers 403 before the handler can answer 404).
- `@Can()` on the class applies to every method; `@Can.Anyone()` or `@Public()` on a method lifts it there.
## Record checks in services
```typescript
@Injectable()
export class NotesService {
constructor(private readonly authorization: AuthorizationService, private readonly context: AuthenticationContext) {}
async edit(id: string, changes: UpdateNote) {
const user = this.context.requireUser();
const note = await this.repository.findById(id);
if (!note) throw new NotFoundException(); // 404 first
await this.authorization.authorize(NotePolicy, 'edit', user, note); // 403 (401 for a guest) when denied
return this.repository.update(id, changes);
}
async list() {
const user = this.context.requireUser();
const notes = await this.repository.findVisibleTo(user); // filter in the query, not by checking every row
return Promise.all(notes.map(async (note) => ({ ...note, canEdit: await this.authorization.can(NotePolicy, 'edit', user, note) })));
}
}
```
- `authorize()` throws on a denial; `can()` resolves to `true` or `false` and never throws. Checked: `can()` flags come out per row (`one:true`, `admin0:false`).
- Each `can()` flag runs the ability once per row, async work included: cache slow lookups (limits, plans) per request.
- Re-check state when you write: two requests can both pass `authorize()` on a `paid` order. Make the write conditional (`UPDATE ... WHERE status = 'paid'`).
- Where existence is sensitive (sequential ids, other users' records), answer 404 instead of 403: scope the query by owner, so someone else's record is simply not found.
## 401 or 403
| Caller | Status | From |
| --- | --- | --- |
| No or bad credentials on a route that needs a user | 401 (`WWW-Authenticate`) | authentication, before any policy |
| Guest on an optional route, policy denies `null` | 401 | authorization: signing in might change the answer |
| Signed-in user, policy denies | 403 | authorization |
## Errors and events
- `authorize()` throws `AuthorizationError` (`reason: 'unauthenticated' | 'forbidden'`, `status`, `policy`, `ability`). Leaving a handler, it becomes `UnauthorizedException`/`ForbiddenException`, so a global exception filter shapes it. Thrown in your own guard or middleware, it isn't mapped and becomes a 500: use `can()` there.
- The 403 body doesn't name the ability. To know which check refused whom, subscribe to `AuthorizationEvents.events$` (checked: `forbidden:NotePolicy.createMore`, `forbidden:NotePolicy.administer`; guests stopped by authentication emit nothing here). `can()` returning `false` isn't a denial and emits nothing.
```typescript
@Injectable()
export class DenialLog implements OnModuleInit {
private readonly logger = new Logger(DenialLog.name);
constructor(private readonly events: AuthorizationEvents) {}
onModuleInit() {
this.events.events$.subscribe((e) => this.logger.warn('Denied', { policy: e.policy, ability: e.ability, reason: e.reason, userId: (e.user as User | null)?.id }));
}
}
```
## Another authentication
- The guard reads the user from `request.user` (and the equivalents for WebSockets, microservices and GraphQL). With another authentication guard (Passport's `AuthGuard()`, your own), register it as `APP_GUARD` in a module imported before `AuthorizationModule`, never with `app.useGlobalGuards()` (those run after every `APP_GUARD`); the order check only goes by class names there, so it logs instead of refusing to start.
- User elsewhere: `AuthorizationModule.forRoot({ getUser: (context) => ... })`. Authentication with `@UseGuards()` per route: `forRoot({ globalGuard: false })` and `@UseGuards(MyAuthGuard, AuthorizationGuard)`.
## Testing
```typescript
// Unit: a policy is a plain class. Calling an ability directly skips before(); test before() on its own.
const quotas = new QuotaService();
const policy = new NotePolicy(quotas);
vi.spyOn(quotas, 'maxNotesFor').mockResolvedValue(1);
expect(await policy.createMore(alice, 0)).toBe(true);
expect(await policy.createMore(alice, 1)).toBe(false);
expect(policy.before({ ...alice, role: 'admin' })).toBe(true);
// Service: the real policy and module, no HTTP. A denial stays an AuthorizationError outside a handler.
const moduleRef = await Test.createTestingModule({
imports: [AuthorizationModule.forRoot()],
providers: [
NotesService,
NotePolicy,
{ provide: QuotaService, useValue: { maxNotesFor: vi.fn().mockResolvedValue(0) } },
{ provide: AuthenticationContext, useValue: { requireUser: () => alice } },
],
}).compile();
await expect(moduleRef.get(NotesService).create('x')).rejects.toMatchObject({ reason: 'forbidden', ability: 'createMore' });
```
Checked with Vitest on the scaffold's config. End-to-end, sign in through the real route and check one allowed and one denied request per rule; `.overrideProvider(NotePolicy).useValue(...)` replaces a policy everywhere at once.
## Production
- Review every `@Can.Anyone()`: it opens the route to everyone authentication lets through, so its service must authorize.
- Don't cache responses by URL on routes that check records: `CacheInterceptor` answers before the handler, so it would serve one user's record to another without asking the policy.
- Hybrid apps: `connectMicroservice(..., { inheritAppConfig: true })`, or `@Can()` on message handlers isn't enforced.
- Put record checks in services, not controllers, so HTTP handlers, resolvers and queue workers share them.