Project 2 — Expense tracker
Hashing, JWTs, middleware, and sessions you can actually end.
How should a password be stored?
All questions with explanations (answers shown)
1. How should a password be stored?
Answer: A bcrypt or argon2id hash
bcrypt is slow on purpose, which makes guessing passwords from a stolen database expensive. SHA-256 is far too fast, and anything reversible defeats the point.
2. Login fails. Which error should you return?
Answer: The same generic "invalid credentials" for both
Different messages let anyone enumerate which emails have accounts. Ideally the timing matches too — hash against a dummy value in the missing-user case.
3. What protects the payload of a JWT?
Answer: Nothing — it is base64, readable by anyone. The signature only proves it was not altered.
Put an id in it, never a secret. The value of a JWT is that your server can trust it without a database lookup, not that it hides anything.
4. You skip checking the signing method when parsing a JWT. What is the consequence?
Answer: A complete authentication bypass — an attacker signs their own token with
alg: noneAssert
t.Method.(*jwt.SigningMethodHMAC)inside the key function, or passjwt.WithValidMethods. Currentgolang-jwtrefusesnoneunless you opt in, but algorithm-confusion bugs have shipped in real products more than once.5. Why use an unexported struct type as a context key?
Answer: A string key can collide with, or be read by, any other package including your dependencies
ctxKey{}from your package is a different key from anyone else’s, because the types differ. Export typed accessors, never the key.6. Why are access tokens only 15 minutes?
Answer: Because a JWT cannot be revoked — the short window limits how long a stolen one is useful
Checking a list of cancelled tokens on every request would bring back the database lookup the JWT was meant to avoid. The revocable half is the stored refresh token.
7. What do you store in the
refresh_tokenstable?Answer: A SHA-256 hash of the token
Same idea as a password: someone who steals the table gets nothing they can use. SHA-256 is fine here because the token is already high-entropy random, so bcrypt’s slowness buys nothing.
8. Why should a refresh token be single-use, replaced on every refresh?
Answer: It makes theft detectable — a token used twice means a bug or a stolen token
Without rotation you cannot detect theft at all. On a reuse, revoke every refresh token for that user and force a fresh login.
9. Why validate config like
JWT_SECRETinsideconfig.Load()?Answer: So a missing secret stops the deploy immediately instead of producing a 500 on the first login
Fail at startup, not at 3am. A deploy that will not boot is far cheaper than one that boots broken.
10. Where does the "is this note mine?" check belong?
Answer: In the SQL:
WHERE id = $1 AND user_id = $2In the query it is impossible to forget and has no race. In Go, someone will eventually write a query that skips it. This is IDOR, listed first in the OWASP API Security Top 10 (as BOLA).
11. A user requests someone else’s note. What status should you return?
Answer: 404 Not Found
A 403 confirms the resource exists, which leaks information. Not yours and not there should look identical.
12. In a PATCH request body, how do you tell "the client sent an empty title" from "the client did not mention the title"?
Answer: Use a
*stringfield —nilmeans absentA pointer’s nil-ness carries the "was it present" bit.
map[string]anyalso works but throws away all your types.13. Which test most matters for an API with per-user data?
Answer: Creating a note as user A, then reading/updating/deleting it as user B — all must be 404
That single test is what stops you shipping an IDOR. Everything else on the list is nice to have.
14. How should you test the repository layer?
Answer: Against a real, throwaway Postgres
A fake repository only proves your fake works. It cannot catch a mistyped column, a constraint violation, or a NULL you did not handle.