Passwords leak: people use the same one on many sites, and stolen lists get passed around. The shop's POST /login checks only the password, so an attacker who has Ana's password signs in as Ana. The starting code even answers 200 to a request with no code at all.
Two-step login closes that hole. When Ana turned it on, the shop and her authenticator app saved the same secret key (TOTP_KEYS). Every 30 seconds both make a new 6-digit code, a TOTP (RFC 6238). The time step is now() // 30. The code is the HMAC-SHA1 of the step, as 8 bytes, with the secret, and the lines already in totp cut it down to 6 digits. Without Ana's phone, the password is no longer enough.
>>> 95 // 30 # second 95 is in time step 3
3
>>> 119 // 30 # so is every second up to 119
3
>>> (3).to_bytes(8, "big")
b'\x00\x00\x00\x00\x00\x00\x00\x03'
Two rules make the codes both usable and safe. Ana may type a code just as her app moves on, so the shop also accepts the code of the previous step. That means a code works for up to a minute. Anyone who sees a code could type it again in that minute, so the shop keeps the last step that signed each email in, in LAST_STEP, and refuses a step that isn't newer. A missing, wrong, old or reused code all get the same 401 {"error": "Wrong code"}.
The check sets the clock with an X-Test-Clock header that now() reads, so the codes are the same on every run. Every request is for ana@example.com. At second 1111111111, 050471 is the current step's code and 081804 is the previous step's. At 2000000045, 279037 is two steps old.
POST /login password "guess-123", code "050471", clock 1111111111 → 401 {"error": "Wrong email or password"}
POST /login password "ana-pass-123", no code, clock 1111111111 → 401 {"error": "Wrong code"}
POST /login password "ana-pass-123", code "123456", clock 1111111111 → 401 {"error": "Wrong code"}
POST /login password "ana-pass-123", code "081804", clock 1111111111 → 200 {"email": "ana@example.com"}
POST /login password "ana-pass-123", code "050471", clock 1111111111 → 200 {"email": "ana@example.com"}
POST /login password "ana-pass-123", code "050471", clock 1111111125 → 401 {"error": "Wrong code"}
POST /login password "ana-pass-123", code "005924", clock 1234567890 → 200 {"email": "ana@example.com"}
POST /login password "ana-pass-123", code "279037", clock 2000000045 → 401 {"error": "Wrong code"}
Your task: in totp, set digest to the HMAC-SHA1 of the step with the key. In login, after the password check, work out the current step and move it one back when the code is the previous step's. Answer 401 {"error": "Wrong code"} when the code isn't the code of step, or when the step isn't newer than this email's LAST_STEP. Once the code is right, save the step in LAST_STEP.
Assignment
Follow the Coding Tutorial to check Ana's 6-digit code after her password. Accept the current and the previous time step, and refuse a code that has already signed in.
Hinthmac.new works with hashlib.sha1 the same way it works with sha256, and .digest() gives the raw bytes. LAST_STEP.get(email, 0) is 0 before Ana's first sign-in, so any real step is newer than that.
Free plan: 30 tutor messages. PRO is unlimited.
