Sub topic 5
Authenticator apps and time-based codes
This chapter assumes you know what a password and two-factor authentication are. It also assumes you have a centralized exchange account and can install an app on your phone. It covers how an authenticator app works from the user's side, what it protects against, and where it fails. The arithmetic behind the codes is in the next chapter, "Under the hood: how a TOTP code is computed and verified".
What an authenticator app does
An authenticator app is a program that shows a short numeric code, usually six digits, that changes every 30 seconds. The exchange asks for the current code after your password. The standard behind these codes is TOTP (time-based one-time password, RFC 6238).
The app and the exchange both hold the same secret value. Each side combines that secret with the current time to calculate the code. When the codes match, the exchange concludes that you hold the secret. The code is never sent to you. Your phone calculates it, and it needs no mobile signal or internet connection.
This design explains most of what follows. The strength of the factor is the secrecy of one value. The weakness is that a code is just six digits a person can type, so a person can also type it into the wrong place.
Enrollment: how the shared secret reaches your phone
When you turn on an authenticator app in your exchange settings, the exchange generates a random secret and shows it as a QR code. Your app scans the code and stores the secret. The QR code is a picture of a URI with the scheme otpauth://:
otpauth://totp/Example%20Exchange:alice%40example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Example%20Exchange&algorithm=SHA1&digits=6&period=30The URI carries everything the app needs:
| Part | Example value | Meaning |
|---|---|---|
| Type | `totp` | Time-based codes. The alternative `hotp` is counter-based. |
| Label | `Example Exchange:[email protected]` | The name shown in the app: service and account. |
| `secret` | `GEZDGNBV...` | The shared secret, encoded in base32 (letters A–Z and digits 2–7). |
| `issuer` | `Example Exchange` | The service name, used for display and grouping. |
| `algorithm` | `SHA1` | The hash used in the calculation. Most apps support only SHA1 in practice. |
| `digits` | `6` | The code length. |
| `period` | `30` | Seconds that each code stays valid. |
The following script reads the URI and prints its fields. It deliberately avoids printing the secret:
from urllib.parse import urlparse, parse_qs, unquote
uri = (
"otpauth://totp/Example%20Exchange:alice%40example.com"
"?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
"&issuer=Example%20Exchange&algorithm=SHA1&digits=6&period=30"
)
parts = urlparse(uri)
params = {key: values[0] for key, values in parse_qs(parts.query).items()}
print("scheme:", parts.scheme) # otpauth
print("type:", parts.netloc) # totp
print("label:", unquote(parts.path.lstrip("/")))
print("issuer:", params["issuer"])
print("algorithm:", params["algorithm"])
print("digits:", params["digits"])
print("period:", params["period"])
print("secret length:", len(params["secret"]), "characters")Anyone who sees the QR code, a screenshot of it, or the "manual entry" text key can generate your codes forever. The enrollment screen is therefore the most sensitive moment in the setup.
The login flow
The exchange has to keep the secret in a form it can use, so it cannot store only a hash the way it does for passwords. A breach of the exchange's database can therefore expose secrets. This is one reason passkeys, which store only a public key on the server, are stronger. Chapter on passkeys covers that.
Clock drift and the verification window
Your phone and the exchange server must agree on the time within a few seconds. The server usually accepts the code for the previous, current and next 30-second step. This tolerance is a trade-off. A wider window forgives more clock error but gives an attacker who has a stolen code more time to use it.
Good servers also remember the last accepted code and reject a second use of it. If a server does not do this, a code captured by a phisher stays valid for up to 90 seconds even after you have used it.
What an authenticator app protects against
Compared with a password alone, or a password plus SMS, an authenticator app stops these attacks:
- Credential stuffing and breached passwords. An attacker with your reused password still lacks the code.
- SIM swapping. The code is not tied to your phone number, so moving your number to a new SIM gives the attacker nothing.
- SMS interception. No message is sent.
- Guessing. A six-digit code gives one chance in a million per attempt, and a good server limits attempts.
What it does not protect against
The code does not depend on the website that asks for it. That causes the main failures:
- Real-time phishing. A fake exchange site collects your password and code and passes both to the real site within seconds. The code is still inside its 30-second step, so the attacker is let in. Nothing in the app tells you that the site is fake.
- Malware on the phone or computer. Malware that can read the app's storage or watch your screen can take the secret or the codes. Session-stealing malware can skip the login altogether, which the sessions chapter covers.
- Secret exposure at enrollment or in backups. Screenshots, cloud-synced notes and unencrypted exports can leak the secret.
- Server-side leaks. The exchange stores the secret, so a breach on its side can expose it.
- Social engineering of support. A convincing caller can persuade support to remove the factor, which is a recovery problem covered in a later chapter.
Common mistakes
- Automatic time is off. If codes are always rejected, check that the phone's date and time are set automatically. Some apps have a "sync time" option that corrects their own offset without changing the phone clock.
- Entering a code at the end of a step. If the countdown is about to end, wait for the next code. The server's window may be narrow.
- Enrolling without confirming. Always type the first code to finish setup. Many exchanges enable the factor only after this check, but some may let you save a broken setup.
- No backup of the secret. If the phone is lost and the secret was never saved, you depend on the exchange's recovery process, which is slow by design. The recovery chapter covers safe options.
- Leaving SMS enabled as a fallback. Attackers choose the weakest allowed factor. If the account still accepts SMS for login or reset, the app adds little. Remove SMS or confirm that it cannot bypass the app.
- Using one app entry for two things. Each service gets its own secret. Do not enroll two exchanges with the same secret, and do not rename entries so that you can no longer tell them apart.
- Mixing up types. Some services use non-standard codes, such as longer alphabets. An entry from the exchange should be a plain six-digit TOTP code. If an app imports it differently, check the digits and period.
Choosing an app
Standard TOTP works in any compliant app, so you can move secrets between apps. Compare apps on these points:
| Feature | Benefit | Risk |
|---|---|---|
| Device-only storage | The secret never leaves the phone. | Losing the phone loses the codes unless you made a backup. |
| Encrypted cloud backup | Recovery after device loss is easy. | The backup is only as strong as its passphrase and the provider's account security. |
| App lock with biometrics or PIN | Limits access if the phone is unlocked and handed over. | Adds little against malware. |
| Export and import | Lets you switch apps. | An export file or QR code contains every secret in one place. |
Keep the authenticator on a separate device from the one you use to log in when you can. A phone with its own screen lock holds up better than a browser extension on the same computer that a phisher or malware targets.
Exercise
An attacker has your exchange password from a data breach, and has also moved your phone number to their own SIM. Which of these stops them: SMS codes or authenticator-app codes? Why?
Show answer
Authenticator-app codes stop them. An SMS code goes to the phone number, which the attacker now controls, so they receive it. An app code is calculated on your own device from a secret that is not tied to the number, so the SIM swap gives them nothing.
Exercise
You log in at a convincing fake exchange page and type your password and the current six-digit code. A moment later your real account shows a login from another country. How did this happen despite the authenticator app, and what kind of factor would have prevented it?
Show answer
The fake site relayed your password and code to the real exchange immediately, while the code was still valid. A TOTP code is not bound to the website that asks for it, so the app cannot tell a real site from a fake one. A passkey or hardware security key would have prevented this, because the browser ties the signature to the real site's origin and a fake domain gets nothing usable.
Exercise
Why can an exchange not store your TOTP secret as a one-way hash, the way it stores your password?
Show answer
The server must calculate the expected code from the secret every time you log in, so it needs the secret itself, not a hash of it. This means a database breach at the exchange can expose TOTP secrets, which is a weakness that passkeys avoid by storing only a public key.