Secrets, Identity, Auth and Access Control
You can allow or deny a request from an access matrix, check an Argon2id password record against a stated cost, and accept or reject a session from its cookie flags, its expiry, and its key id.
- Level
- Navigator
- Time
- 22 h

Jump to
After this course you can
The student can implement an authentication and RBAC authorisation service that enforces a strict access matrix, rotates signing keys without dropping valid sessions, and passes all session-security tests.
Authentication, authorization, and the matrix
Authentication, written AuthN, answers which account is calling. In this lesson the proof is a JSON Web Token, a JWT, whose signature matches a key the service still accepts and whose exp time still lies after the clock. The payload carries sub, the account id. Authorization, written AuthZ, answers whether that account may run the action named in the request. The service loads the role bound to sub from its own table. A role field in the request body stays unused.
The access matrix has one row for each role and one column for each action. Each cell is allow or deny. The rows are viewer, clerk, and admin. The columns are read_ledger, post_entry, and manage_users. viewer has allow only under read_ledger. clerk has allow under read_ledger and post_entry. admin has allow under all three columns. Every remaining cell is deny. Zero trust in this service means each request runs AuthN and then AuthZ again. An allow on request 1 leaves request 2 undecided until those two checks finish.
Least privilege, RBAC, and one attribute
Least privilege means you bind the account to the smallest row whose allow cells cover the job. A person who reads the ledger and never posts is a viewer. Binding that account to admin would open manage_users, an action the job never calls. Role-based access control, RBAC, is the binding of accounts to rows and rows to cells. Edit the clerk cell for post_entry and every clerk account follows that edit. The table of accounts, role bindings, and cells is the service's IAM record, its identity and access management.
Attribute-based access control, ABAC, adds one condition beside a cell. Here the attribute is the UTC hour, and the hour gate binds only to the clerk cell for post_entry. A clerk may post_entry while the hour runs from 8 inclusive up to 18 exclusive. At 07:59 UTC the hour is 7, so post_entry is denied. At 08:00 UTC the hour is 8, so that cell opens. At 17:59 UTC the hour is 17, so it stays open. At 18:00 UTC the hour is 18, so it closes. The hour gate leaves every deny cell closed: manage_users for a clerk stays deny at 08:00 and at 18:00. read_ledger ignores the hour, and so does post_entry on the admin row. Defense in depth is the stack you keep when one check looks like enough on its own: signature, expiry, the matrix cell, and the hour gate. Any one of those checks can stop the action.
Argon2id records, passkeys, and a second factor
The service stores a password as an Argon2id string in PHC form. The string begins with the type and the version, then the cost. RFC 9106 states two uniform profiles. The first profile is Argon2id with t=1, p=4, and m=2097152. The m value counts kibibytes, so 2097152 KiB is 2 GiB, because 2097152 / 1024 / 1024 = 2. The second profile is Argon2id with t=3, p=4, and m=65536. That is 64 MiB, because 65536 × 1024 = 67108864 bytes and 67108864 / 1048576 = 64. Both profiles use a 16-byte salt and a 32-byte tag. This service uses the second profile, so a stored row starts $argon2id$v=19$m=65536,t=3,p=4$. Version 19 is the Argon2 version number 0x13. The password text has no column of its own.
The cost is what a guesser pays. On this lesson's example machine one verification at the second profile takes 50 milliseconds, while a fast hash of a short password runs at 1,000,000,000 guesses per second. A 6-digit PIN has 1,000,000 candidates. The fast path finishes a sweep of all of them in 1,000,000 / 1,000,000,000 = 0.001 seconds. The Argon2id path takes 1,000,000 × 0.05 = 50,000 seconds. Split that: 13 × 3,600 = 46,800, and 50,000 − 46,800 = 3,200 seconds, which is 53 × 60 + 20. The sweep takes 13 hours, 53 minutes, and 20 seconds on this machine. The 50 milliseconds belongs to this machine. When a new machine verifies the same string in less time, you raise m or t until the wall-clock cost comes back.
A passkey row stores a public key, and the private key stays on the device. Login sends a fresh 32-byte challenge. The device signs those bytes, and the service checks the signature with the stored public key. The password string stays off that path. Multi-factor authentication, MFA, means manage_users needs two successes before the matrix is read: the password check or the passkey check, and a one-time code that matches the code the service just issued for that sub. The code stays valid for 60 seconds after it was issued. read_ledger for a viewer needs only the first success. A correct password with a wrong one-time code still stops manage_users.
Cookie flags, JWT expiry, and key rotation
A successful first factor sets a session cookie and returns a JWT. The cookie carries three flags: HttpOnly, Secure, and SameSite=Lax. HttpOnly keeps the cookie out of document.cookie, so script on the page cannot read it. Secure tells the browser to attach the cookie only when the request uses HTTPS. SameSite=Lax attaches it on a same-site request and on a cross-site top-level GET, and withholds it on a cross-site POST. A form on another site that POSTs to post_entry therefore arrives with no session cookie.
The JWT is three base64url segments separated by dots. The first is the header, and it carries alg and kid. The second is the payload, and it carries sub, iat, and exp as whole Unix seconds. The third is the signature over the first two segments. The service accepts the token when three statements hold: the signature matches the key named by kid, that kid is still in the accept set, and the current Unix time is strictly less than exp. A time equal to exp is a rejection.
Rotation keeps a token that is still inside its lifetime. Tokens live 900 seconds. At Unix time 1717401600, which is 2024-06-03 08:00:00 UTC, the service starts signing new tokens with kid k2 and keeps kid k1 in the accept set until 1717402500, which is 08:15:00 UTC the same day. A token for u17 with kid k1, iat 1717401100 (07:51:40 UTC), and exp 1717402000 (08:06:40 UTC) still passes at 1717401700 (08:01:40 UTC), because 1717401700 < 1717402000 and k1 is still accepted. At 1717402000 the expiry check fails. The signing key stays in a secret store. A source line that reads SIGNING_KEY= and then the key bytes is a leaked secret once a commit contains it. A later commit that deletes the line leaves the older commit in the history. Treat that key as burned, put a new kid into the store, and move the accept set. A pre-commit scan that rejects a staged line matching SIGNING_KEY= stops the leak before the commit exists.
A clerk at 08:01, checked by hand
Account u17 is bound to clerk. The password row uses the second profile, so m=65536, t=3, and p=4. At 1717401100, 2024-06-03 07:51:40 UTC, the password check succeeds. The service sets HttpOnly, Secure, and SameSite=Lax, and signs a JWT with kid k1, sub u17, iat 1717401100, and exp 1717402000. That expiry is 900 seconds later, 08:06:40 UTC. At 08:00:00 UTC the signer switches to k2, and k1 remains acceptable through 08:15:00 UTC.
Request D arrives at 1717401540, 07:59:00 UTC, as a same-site HTTPS POST whose action is post_entry. The cookie is attached. The signature matches k1, and 1717401540 is less than 1717402000, so AuthN holds. The role row is clerk. The UTC hour is 7, outside the window from 8 inclusive to 18 exclusive, so the hour gate denies post_entry. Request D stops.
Request A arrives at 1717401700, 08:01:40 UTC, as the same kind of POST, still with the k1 token. The signature matches, 1717401700 is less than 1717402000, k1 is inside the overlap, and the hour is 8. The clerk cell for post_entry allows, and the hour gate allows. Request A runs. Request B at that same second asks for manage_users. The clerk cell is deny, and the hour gate leaves a deny cell closed, so request B stops. Request C is a cross-site POST of post_entry at that same second. SameSite=Lax withholds the cookie, AuthN has no session, and request C stops.
At 1717402000, 08:06:40 UTC, the same k1 token fails the expiry check on read_ledger, because the clock is equal to exp and the rule requires a time strictly below exp. A staged file that contains the line SIGNING_KEY= followed by the signing bytes fails the pre-commit scan, so that commit is refused and the key stays in the secret store.
Score four requests on paper
Use the same matrix, the same hour gate, the same three cookie flags, and the second Argon2id profile. Account u18 is a viewer. The token has kid k2, sub u18, iat 1717437000, and exp 1717437900. You check at 1717437540, which is 2024-06-03 17:59:00 UTC, with a same-site HTTPS call and the cookie attached. Decide read_ledger, post_entry, and manage_users. Then move the clock to 1717437600, which is 18:00:00 UTC, and decide read_ledger again. Say what a cross-site POST does with the session cookie, and what document.cookie shows. A stored row shows m=4096, t=1, p=1. State the memory in MiB and whether the row matches the second profile. On the example machine a verify costs 50 ms. Compute the seconds for a full sweep of an 8-digit PIN, which has 100,000,000 candidates, and split that total into days, hours, minutes, and seconds.
u18 at 17:59:00 UTC may read_ledger and may neither post_entry nor manage_users, because a viewer row allows only read_ledger. At 18:00:00 UTC read_ledger still runs: 1717437600 is less than exp 1717437900, and read_ledger has no hour gate. The cross-site POST arrives without the cookie because SameSite=Lax withholds it, and document.cookie does not show the cookie because HttpOnly is set. m=4096 is 4 MiB, since 4096 / 1024 = 4, and the row misses the second profile, which requires m=65536, t=3, and p=4. The 8-digit sweep takes 100,000,000 × 0.05 = 5,000,000 seconds, which is 57 days, 20 hours, 53 minutes, and 20 seconds: 57 × 86,400 = 4,924,800, the remainder is 75,200 seconds, 20 × 3,600 = 72,000, and 3,200 seconds is 53 minutes and 20 seconds.
Check questions
Which cells are allow on the clerk row, and what does the hour gate do to manage_users for a clerk at 08:00 UTC?
read_ledger and post_entry are allow. manage_users stays deny, and the hour gate leaves that deny cell closed.
What are m, t, and p in the second RFC 9106 profile, and how many MiB is that m?
m=65536, t=3, and p=4. That m is 64 MiB.
A u17 token has kid k1 and exp 1717402000. Does the expiry check pass at 1717401700, and at what Unix time does k1 leave the accept set?
It passes at 1717401700 because that time is strictly below 1717402000. k1 leaves the accept set at 1717402500.
Which cookie flag withholds the session on a cross-site POST, and which flag keeps the cookie out of document.cookie?
SameSite=Lax withholds it on a cross-site POST. HttpOnly keeps it out of document.cookie.
A request runs only when the signature, the key id, and the expiry all hold, the matrix cell for that role and action says allow, and any hour gate on that cell also holds. The password row stores an Argon2id cost you can recompute, and a signing key that reaches a commit is burned.
Final task
Submit an auth and RBAC service that passes 35 automated security tests covering privilege escalation attempts, expired/tampered JWTs, CSRF/cookie flags, zero-downtime key rotation, and git history secret scans.
Terms in this course
7 terms in the glossary belong to this course.
The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist