Security
Two concerns, two layers — authentication answers who is calling, authorization answers what they may do, and the framework wires both transparently.
Security in nestrs splits into two concerns wired by two composable layers. Authentication establishes who the caller is. Authorization declares what they may do, then enforces it on every read, every by-id write, and every response body a unit’s posture names before it leaves the process. Bind the two guards on the controller, write the one line that says what each unit reads, and the framework does the rest: a unit naming nothing reads nothing a grant guards, so nothing fails open.
Here for the first auth task?
Add login and protect a route walks the whole
happy path — issue a JWT on POST /login, bind the two guards, curl the
round-trip — before any of the reference below.
The authn layer leans on jsonwebtoken,
argon2, and the
oauth2 crate. The authz layer is the
framework’s own ability-based engine, with row-level filtering bridged into
SeaORM.
Two concerns, two layers
Section titled “Two concerns, two layers”| Concern | Layer | Who answers it |
|---|---|---|
| Who is calling? | Authentication | A Strategy turns the request into a principal, or says why it could not; the guard answers the 401 |
| What may they do? | Authorization | An Ability declares allowed actions; the framework applies it to queries, by-id loads, and response bodies |
Each layer ships its own guard. Bind them in order — the authn guard
runs first and attaches the principal; the authz guard reads it and
installs the ambient Ability.
#[controller(path = "/users")]#[use_guards(AuthnGuard, AuthzGuard)]pub struct UsersController { #[inject] svc: Arc<UsersService>,}That’s the whole opt-in surface for HTTP. GraphQL, WebSockets and MCP run the same two guards at their own request, and every operation it carries reads the ability they attached — see Endpoint guards.
What you get once both are mounted
Section titled “What you get once both are mounted”- Every read is filtered. The
Repogateway every service uses joins the ambient ability’scondition_forinto the SQL — a member queryingUsers::find()getsWHERE org_id = $caller_org_idappended. - Every by-id write checks access.
#[authorize(A, bind = S)]callsService::access(action, id); missing rows return404, denied rows return403— see By-id binding. - Every wire row is judged. The exposed type a service builds from a
row,
User::from(&row), judges it for the caller whose ability is installed there: a column their grant withholds readsnull, a row their grants refuse answers nobody, and only the columns#[expose]lists exist on the wire, so an unrestricted grant cannot leak an unexposed column. A value your body copies a column into is its own visible line.
The wiring sketch for an app that serves all three transports:
#[module( imports = [ SeaOrmDatabaseModule, UsersHttpModule, UsersGraphqlModule, UsersWsModule, ],)]pub struct ApiModule;Each adapter imports what its guards need — UsersHttpModule and
UsersWsModule import AuthzModule, UsersGraphqlModule the
AuthzGraphqlModule that declares /graphql’s guards — so the root lists the
edges it serves, and the access graph checks each import where it is used.
Where each piece lives
Section titled “Where each piece lives”Directorysecurity/
Directoryauthentication/
- index.mdx —
Strategy,AuthnGuard<S>, reading the principal back withAmbient<P> - jwt.mdx —
JwtStrategy,JwtService, claims - jwt-configuration.mdx — HS, EdDSA or JWK Set keys, issuer and audience
- external-identity-provider.mdx — another issuer’s tokens, against its JWK Set (better-auth end to end)
- oauth2.mdx — authorization-code flow with PKCE
- social-login.mdx — the provider contract, GitHub and Google
- password.mdx — Argon2id hashing and
burn_verify - split-deployment.mdx — the split-deploy pattern
- index.mdx —
Directoryauthorization/
- index.mdx —
Ability,AuthzAbility, where it runs - policies.mdx —
Subject,Action,RuleSpec - predicates.mdx — the predicate AST, relational scoping
- field-grants.mdx — the fields a reply shows and a write may set
- row-level-filtering.mdx —
condition_for,Repo::scoped - by-id-binding.mdx —
#[authorize(A, bind = S)], theAccessoutcome - response-masking.mdx — the judged wire row,
#[expose(nullable)] - endpoint-guards.mdx — HTTP / GraphQL / WS / MCP
- index.mdx —
- threat-model.mdx — the boundary each layer enforces (read before production, not needed for your first feature)
Going further
Section titled “Going further”- Threat model — what these layers cover and what they don’t (explanation — read before going to production, not needed for your first feature).
- Guards — the primitive every security layer builds on.
- Throttler — rate limiting as a guard, composes with
AuthnGuard.