Who are you, and what may you do?
Your app checks passwords itself, and the database password is written in the code. 5,000 req/s arrive, and every one of them must know who the user is.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Authentication asks who you are, for example with a password, a passkey or Google sign-in. Authorization asks what you may do, for example whether you may see this invoice.
Never store passwords, even encrypted. Store a slow hash made with bcrypt or Argon2, so a stolen table is still hard to use. After sign-in, a signed token such as a JWT lets any server check the user without a database call. Keep tokens short-lived, because a signed token can't easily be taken back.
Encrypt data in transit with TLS and at rest on disk. Keys and passwords belong in a secrets manager, not in code, where every copy of the repository would hold them.
What you have to achieve
5,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- Use an Auth Service with at least 3 instances
- Use Secrets & Config
- No component is overloaded
- No single point of failure
What you learn
Sign-in now lives in one service with enough room for every request, and no password sits in your code. Who you are and what you may do are separate questions, answered on every request.
Check yourself
Which question does authorization answer?
- Who are you?
- What are you allowed to do?
- Is the server up?
Show the answer
What are you allowed to do?. Authentication checks who you are. Authorization checks whether that person may do this particular thing.
How should passwords be stored?
- Encrypted, so you can read them back
- As a slow hash, such as bcrypt or Argon2
- As plain text in a safe database
Show the answer
As a slow hash, such as bcrypt or Argon2. You never need to read a password back, only to check one. A slow hash makes a stolen table very costly to crack.
What's the main risk of a JWT that lasts a month?
- It's too big
- If it's stolen, it can't easily be taken back
- Servers can't read it
Show the answer
If it's stolen, it can't easily be taken back. Servers trust a signed token without asking anyone, so a stolen one works until it expires. Short-lived tokens with refresh tokens limit the damage.
Where should the database password live?
- In the code
- In a secrets manager
- In the README
Show the answer
In a secrets manager. Code is copied to every laptop and build server. A secrets manager controls who can read it and lets you change it without a release.