Sub topic 9
Verification codes for two-factor sign-in
This chapter assumes you can save and fill a login in Safari, and that iCloud Keychain is on for your Apple Account. Earlier chapters covered both. Here you will add a second factor to a saved login: a verification code that the Passwords app generates for you.
What a verification code is
Many sites ask for two things at sign-in: your password, and a six-digit code that changes every 30 seconds. That second item is a time-based one-time password (TOTP). Two inputs produce it:
- A secret value that the site gives you once, called the setup key.
- The current time.
The site and your Passwords app both hold the same secret. Each side computes the code independently, so no code travels between them until you type it in. If the two results match, the site lets you in.
An authenticator app does this job, and so does the Passwords app. It stores the setup key inside the same entry as the username and password. The code is then available wherever that entry is available.
How the Mac fills a code
When you sign in to a site whose entry has a verification code, the sequence looks like this:
Safari usually fills the code without extra clicks once the login is unlocked. In apps and other browsers, you may need to copy the code yourself (see below).
Setting up a code for a saved login
Start from the site's security settings, where you turn on two-factor authentication with an authenticator app. The site will show a QR code and often a text setup key beneath it. Keep that page open.
- Open the Passwords app and select the entry for that site. Create the entry first if it does not exist.
- Click Edit.
- Click Set Up Verification Code.
- Choose how to provide the secret:
- Enter Setup Key if the site shows a text string. Paste it in.
- Scan QR Code if the site shows only a QR code. Follow the prompt to capture the code from your screen.
- Click Done.
The entry now shows a Verification Code row with the live six-digit number and a countdown. Before you leave the site's setup page, type the current code into the page's confirmation box. Most sites require this step, and it proves that your stored secret works.
The setup key is the whole secret
Anyone who has the setup key can generate valid codes forever. Treat it like a password. Do not paste it into chat, email or screenshots. Once the code shows correctly in the Passwords app, close the site's setup page.
Setting codes up in bulk with CSV
A CSV file can carry codes for many logins. The last column in the format from the import chapter is OTPAuth, and it holds an otpauth:// link. The link packs the account name, issuer and secret into one string:
Title,URL,Username,Password,Notes,OTPAuth
Example Site,https://example.com,[email protected],replace-with-real-password,,otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=ExampleThe secret above is a placeholder. A file like this contains every password and secret in plain text. Delete it once the import finishes, and empty the Trash as well.
Using a code
In Safari
After the password step, Safari offers the code or fills it automatically. If the site shows a code box but nothing appears, click into the box and look for the AutoFill suggestion. Select the entry.
In apps and other browsers
Open the Passwords app, select the entry and click the verification code row to copy it. Paste it into the site or app within the 30-second window. Prefer a code that has more than a few seconds left, because a code can expire while you type.
Codes sent by text message
Some sites send a code by SMS instead of using an authenticator secret. Those are not stored in the Passwords app. If your Mac receives the message, Safari can often offer the code from Messages as an AutoFill suggestion. This saves typing, but the code is still a one-time value delivered to your phone number, and you cannot set it up in the entry.
Syncing and what it means
Verification codes sync through iCloud Keychain together with the rest of the entry. Set up a code on your Mac and the same code appears on your iPhone and iPad signed in to the same Apple Account. This helps you: if you lose one device, you can still sign in from another.
It also changes the security picture. A second factor is useful because it comes from a different place than the password. When both live in one app, someone who unlocks that app gets both. Compare the options:
| Approach | Strength | Weakness |
|---|---|---|
| Password only | Simple | One stolen password lets an attacker in |
| Password and code in the Passwords app | Strong against reused or leaked passwords, and convenient | Anyone who unlocks the app gets both |
| Password in the app, code in a separate authenticator or hardware key | Two separate places to compromise | More steps, and you must back up the second place |
For most accounts, keeping both in the Passwords app is a large improvement over a password alone. Phishing is the typical threat: a copied password from a fake site is useless to the attacker if they cannot also produce your current code. For accounts where a break-in would be severe, such as your Apple Account itself or your primary email, consider a separate hardware security key.
Changing or removing a code
To replace a code, for example after the site resets its two-factor settings, open the entry, click Edit and use the verification code row to set up a new one. The old secret stops working on the site as soon as the site issues a new one, so do both steps in one session. To remove a code, edit the entry and delete the verification code, then also turn off or reconfigure two-factor on the site, so the site does not keep asking for a code you can no longer produce.
Common errors
- The site says the code is invalid. The usual cause is the clock. A TOTP depends on the current time, and a Mac with a wrong clock produces wrong codes. In System Settings, open General, then Date & Time, and turn on automatic time setting.
- The code expired while you pasted it. Wait for the next code and try again.
- The code works on the Mac but not the iPhone. The secret may not have synced. Check that iCloud Keychain is on for both devices and that they use the same Apple Account.
- No code row appears in the entry. The entry has no setup key yet. Edit the entry and add one.
- You scanned the wrong QR code. The site rejects every code. Remove the code from the entry, restart two-factor setup on the site and scan again.
- Safari does not offer the code. The entry may be for a different site address than the one you are on. Check the URL field on the entry.
Exercise
A site shows a QR code for two-factor setup. You scan it into the Passwords app, and the entry now displays a changing six-digit number. The site then says "invalid code" when you type that number. Name two likely causes and how you would check each.
Show answer
First, the Mac's clock may be wrong. Codes depend on the current time, so open System Settings, General, Date & Time, and turn on automatic time setting. Second, the code may have expired while you typed it, or you may have scanned a QR code that belongs to a previous setup attempt. Wait for a fresh code and type it quickly. If it still fails, remove the code from the entry, restart two-factor setup on the site and scan the new QR code.
Exercise
Why is storing both the password and the verification code in the same Passwords app weaker than using a separate hardware key, and why is it still better than a password alone?
Show answer
A person who unlocks the Passwords app gets both factors, so the two factors no longer come from separate places. It is still better than a password alone because most attacks, such as phishing or reuse of a leaked password, obtain only the password. The attacker would still need the current code, which needs the setup key and cannot be guessed.