Security
The docs' Security chapter on the same cats API: sign visitors in with JWTs and hash their passwords, decide what each of them may do with roles and policies, do the same the Passport way, then harden the service with helmet, CORS, CSRF protection and rate limiting, and finish with the whole API locked down.
- lessons
- 10
- level
- intermediate
- time
- 5 hours
Identity
Who is asking, proven with a token; passwords that are never stored; and what each identity is allowed to do.
- 1Authentication
Sign the shelter's staff in: exchange a username and password for a signed JWT, verify it on every later request in a guard bound to the whole app, and mark the routes anyone may use with @Public().
Read the theory - 2Hashing and Encryption
Stop storing passwords: hash them with bcrypt at registration and compare at sign-in. Then the other direction, for data that must be read back: encrypt each cat's microchip number with AES under a key derived with scrypt, a fresh IV per record.
Read the theory - 3Roles
Signed in is not the same as allowed: give each member of staff roles, carry them in the token, and let a second global guard decide, route by route, which role a request needs.
Read the theory - 4Policies
Roles run out when the answer depends on the thing itself: a member of staff may rename the cat they adopted, nobody may delete an adopted cat. Describe abilities with CASL, check them in a guard for the route and in the service for the record.
Read the theory - 5Passport
The same sign-in, the way most of the Node world writes it: Passport strategies for the password check and for the bearer token, wrapped by @nestjs/passport into guards, with one Nest 12 trap the docs' recipe walks into.
Read the theory
Hardening
The headers, origins, tokens and limits that stand between the API and the rest of the internet.
- 6Helmet
A dozen response headers that tell browsers what not to do with your responses, set once by helmet on every response the API sends, with one directive adjusted for the shelter's image CDN.
Read the theory - 7CORS
A browser will not let a page on one site read this API's answers unless the API says so: cross-origin resource sharing is that permission, granted origin by origin, method by method, in headers, with a preflight request the browser sends first.
Read the theory - 8CSRF
When the browser sends the login cookie by itself, any page can make it send one: cross-site request forgery. Protect the routes that change state with a double-submit token that only your own page can present, bound to the session it was issued for.
Read the theory - 9Rate Limiting
Nothing in the API stops a script from trying ten thousand passwords, or from calling one route until the database gives up. A throttler counts requests per caller and route and answers 429 past the limit, tighter on sign-in, not at all on the health check.
Read the theory
The whole API
Everything from the course on one service, wired in the right order.
- 10Securing the API
Everything from the course on one service, in the order the request meets it: helmet's headers and CORS at the door, the throttler, then who is asking, then what they may do, with passwords hashed and a health check that is never counted.
Read the theory