What Is Two-Factor Authentication (2FA)? Methods and Setup. Two-factor authentication (2FA) asks for two kinds of proof at login. Compare SMS, app codes, push and passkeys against real attacks, and learn to turn it on.
Two-factor authentication (2FA) is a login requiring two kinds of proof, usually a password plus an authenticator app code, passkey or security key, so a stolen password alone won’t get an attacker in. Passkeys and security keys resist phishing best, and codes sent by SMS or email are the weakest.
If you have just been asked to turn on 2FA, this post explains what it is, how a login with it works, which option to pick and where to switch it on. If you are a developer or an admin deciding which method your users must use, the later sections compare the methods against the attacks that defeat them and show how to require a second factor in Keycloak.
What is two-factor authentication?
A password is one kind of proof: something you know. Two-factor authentication adds a second proof of a different kind, so someone who has stolen or guessed your password still cannot sign in. NIST’s digital identity guidelines say of their second assurance level that “proof of the possession and control of two distinct authentication factors is required” (NIST SP 800-63B-4, introduction), and the same guidelines name the three types of authentication factors as “something you know, something you have, and something you are” (NIST SP 800-63B-4, definitions).
| Factor | What it means | Everyday examples |
|---|---|---|
| Something you know | A secret you remember | A password, a PIN, the passcode that unlocks your phone |
| Something you have | A device or object only you hold | Your phone with an authenticator app, a USB or NFC security key, a smart card |
| Something you are | A physical trait | A fingerprint, a face scan |
The two proofs have to be of different kinds. A password plus a security question is still two things you know, so an attacker who phished one can usually phish the other in the same conversation. A password plus a code from your phone adds a proof of the second kind, something you have, although the sections below show how some codes can still be intercepted or phished.
NIST adds a detail that matters for the third row: “A biometric characteristic is not recognized as an authenticator by itself.” A fingerprint only counts when it is checked together with a physical device, which then serves as “something you have” while the biometric match serves as “something you are” (NIST SP 800-63B-4, section 2.2.1). That is why the fingerprint reader on your laptop is used to unlock a key stored on that laptop, rather than being sent to the website as proof on its own. Our post on biometric authentication covers this in more depth.
People use 2FA, two-step verification and multi-factor authentication (MFA) almost interchangeably. Strictly, 2FA is MFA with exactly two factors, and most logins that call themselves MFA use two. Our older post on what multi-factor authentication is covers the wider term.
How does 2FA work?
Here is one login with an authenticator app, from start to finish:
- You enter your username and password, and the site checks the password. That is the first factor.
- The site does not sign you in yet, and asks for a second proof instead.
- You open the authenticator app on your phone, read the six-digit code it shows and type it in.
- The site works out the same code on its own and compares the two. If they match, you are signed in.
An attacker who bought your password from a data leak gets as far as step 2 and stops there, because they do not have the app that produces the code.
You send your password (1) and the website asks for a code (2). Your app works out a code (3) and you type it (4). The website works out its own code from the same secret and time (5), and because they match you are signed in (6). An attacker with only your password stops at step 2.
How an authenticator app makes its code
An authenticator code is a TOTP, short for time-based one-time password. When you set up the app, it is given a secret key that the site also keeps, which RFC 6238 calls the shared secret (RFC 6238, section 4). After that, the app and the site never need to talk to each other, because each one calculates the code from two things they both have: the secret and the current time.
The calculation is short. TOTP counts how many 30-second steps have passed, and feeds that number and the secret into HOTP, the older counter-based one-time password algorithm (RFC 6238, section 4.2), which hashes them and shortens the result to a number of at least six digits (RFC 4226, section 5.3):
T = floor(current Unix time / 30)
code = Truncate(HMAC-SHA-1(secret, T))
This explains two things you may have noticed. The app works with no signal, because nothing is sent to it at login, and Google’s help page suggests code apps for exactly the time “when you don’t have an internet connection or mobile service” (Google Account Help). A code can also be rejected when your phone’s clock has drifted too far, because the server only allows a little slack for timing: RFC 6238 recommends allowing “at most one time step” for network delay (RFC 6238, section 5.2).
Other second factors change step 3. SMS sends you the code instead of having your phone calculate it, a push app asks you to tap approve, and a passkey has your device sign a challenge from the site with a private key, as our post on what a passkey is explains. We covered one-time codes more generally in what an OTP is.
What is a two-factor authentication method?
A 2FA method is the specific way the second proof is collected. These are the ones you will meet:
- SMS or voice codes. The site texts or calls you with a short code that you type in.
- Email codes. The same idea, delivered to your inbox.
- Authenticator app codes (TOTP). An app such as Google Authenticator or FreeOTP shows a six-digit code that changes every 30 seconds, as described above.
- Push approvals. An app on your phone pops up “Is this you signing in?” and you tap approve. Some versions ask you to type a number shown on the login screen, which is called number matching.
- Passkeys. A cryptographic key pair stored on your phone, laptop or password manager, unlocked with your fingerprint, face or device PIN. The FIDO Alliance calls passkeys “FIDO cryptographic credentials that are tied to a user’s account on a website or application” (FIDO Alliance, FIDO2).
- Hardware security keys. A small USB or NFC device, such as a YubiKey, that holds the same kind of key pair and never lets it be copied off.
Passkeys and security keys both use FIDO2, which the FIDO Alliance describes as WebAuthn and CTAP (FIDO Alliance, FIDO2). WebAuthn is the W3C’s browser API for public key credentials (W3C WebAuthn Level 2), and CTAP is the FIDO Alliance’s Client to Authenticator Protocol, which that specification references.
2FA methods compared: phishing resistance, cost and friction
The table scores each method on the three things an admin usually trades off. “Phishing resistant” uses NIST’s definition: the protocol prevents your secrets from being handed to an attacker posing as the real site “without relying on the vigilance of the claimant”, which means it still works when you have been fooled (NIST SP 800-63B-4, section 3.2.5). The cost and friction columns are our own judgement of typical deployments, not measured figures.
| Method | Phishing resistant | Attacks that beat it | Cost to run | Friction for users |
|---|---|---|---|---|
| SMS or voice code | No | Real-time phishing, SIM swap, SS7 interception | Per-message fees and a gateway to run | Low to set up, a short wait for each code |
| Email code | No | Real-time phishing, mailbox takeover | Low, uses your existing mail setup | Low, but slow if mail is delayed |
| Authenticator app (TOTP) | No | Real-time phishing within the code’s window | Free apps, no per-login cost | Moderate: scanning a QR code once, typing a code each time |
| Push without number matching | No | Push fatigue, real-time phishing | Usually a vendor app or service | Low: one tap |
| Push with number matching | No | Real-time phishing where you read the number to the fake page | Usually a vendor app or service | Low to moderate |
| Passkey (synced or device-bound) | Yes | None of the above; recovery of the sync account becomes the weak point | No per-login cost; users’ devices already support it | Lowest: a fingerprint or face scan |
| Hardware security key | Yes | None of the above; a lost key needs a recovery path | Buying a key, ideally two, per user | Moderate: carry the key and plug or tap it |
CISA’s fact sheet on phishing-resistant MFA calls phishing-resistant MFA “the gold standard” (CISA, Implementing Phishing-Resistant MFA). The same sentence of the fact sheet says that “any form of MFA is better than no MFA” (SecurityWeek, quoting that sentence of the fact sheet), which is worth remembering if SMS is the only option a site gives you.
How real attacks defeat specific 2FA methods
SIM swap: taking over your phone number
In a SIM swap, the attacker convinces your mobile carrier to activate a new SIM card connected to your phone number on a phone they own, and from then on the text messages with your login codes go to them (FTC, SIM swap scams). CISA’s fact sheet lists flaws in the SS7 signalling protocol of the phone network’s communications infrastructure as a second way to intercept codes sent by text or voice. Neither attack involves your phone or your behaviour, so being careful does not protect you, and app codes, push, passkeys and security keys are not affected by either.
Push fatigue: approving just to make it stop
CISA describes push bombing, also called push fatigue, as attackers sending you push notifications over and over until you tap accept (CISA, Implementing Phishing-Resistant MFA). NIST no longer accepts the variant where you compare a code shown on both screens and then tap approve, because it “increases the likelihood that the subscriber would approve an authentication request without actually comparing the secrets as required”, adding that “this has been observed with ‘authentication fatigue’ attacks” (NIST SP 800-63B-4, section 3.1.3). It also says verifiers that send push notifications “SHOULD implement a reasonable limit on the rate or total number of push notifications that will be sent since the last successful authentication” (NIST SP 800-63B-4, section 3.1.3.2). Number matching fixes the case where you approve a prompt you did not start, and we went through the helpdesk side of these attacks in MFA fatigue and helpdesk vishing.
Real-time phishing proxies: relaying your code
This is the attack that beats every method where you type or read a code. You get an email, click a link to a page that looks exactly like your company’s login, and type your password and your six-digit code; CISA’s fact sheet uses exactly this example, with the code coming from the user’s authenticator app (CISA, Implementing Phishing-Resistant MFA). A proxy behind the fake page passes both to the real site while the code is still valid, and the real site hands the logged-in session back to the attacker. A TOTP code gives the attacker roughly 30 seconds plus whatever delay window the server allows (RFC 6238, section 5.2), and that is plenty of time for software.
NIST states the reason directly: authenticators that involve manual entry, such as OTP and out-of-band codes, “SHALL NOT be considered phishing-resistant because the manual entry does not bind the authenticator output to the specific session being authenticated”, so “an impostor verifier could relay an authenticator output to the verifier and successfully authenticate” (NIST SP 800-63B-4, section 3.2.5).
Passkeys and security keys break this attack because there is nothing for you to type. The WebAuthn specification says the authenticator “ensures that all operations are scoped to a particular origin, and cannot be replayed against a different origin, by incorporating the origin in its responses” (W3C WebAuthn Level 2, section 5), and that “a public key credential can only be used for authentication with the same entity (as identified by RP ID) it was registered with” (W3C WebAuthn Level 2, RP ID). A look-alike domain cannot ask for your real site’s passkey, and if a proxy relays the exchange anyway, the signature names the fake origin and the real site rejects it.
With an app code, you type your password and code into the fake page (1), the proxy relays the same code to the real site in time (2), and the real site hands the session to the proxy (3). With a passkey, your browser signs over the fake page’s origin (4), the proxy relays that signature (5), and the real site rejects it because the origin does not match.
For a deeper look at which methods count as phishing resistant, see what phishing-resistant MFA is.
Where NIST and CISA stand on SMS
NIST has not banned SMS, but it labels it. The PSTN (the public phone network that carries SMS and voice calls) is the one “restricted” authenticator in SP 800-63B-4: “Use of the PSTN for out-of-band verification is restricted” (NIST SP 800-63B-4, section 3.1.3.3), and an organisation that accepts it has to “assess, understand, and accept the risks associated with that authenticator and acknowledge that risks will likely increase over time” (NIST SP 800-63B-4, section 3.2.9). It also tells verifiers to look at risk signals such as “device swap, SIM change, number porting” before sending a code by phone (NIST SP 800-63B-4, section 3.1.3.3).
Email fares no better. NIST says “Email SHALL NOT be used for out-of-band authentication because it may be vulnerable to” three things: “Access using only a password”, “Interception in transit or at intermediate mail servers” and “Rerouting attacks, such as those caused by Domain Name System (DNS) spoofing” (NIST SP 800-63B-4, section 3.1.3.1). You should not count email codes as strong.
At the second assurance level, NIST says “Verifiers SHALL offer at least one phishing-resistant authentication option at AAL2” and that verifiers “SHOULD encourage the use of phishing-resistant authentication at AAL2 whenever practical” (NIST SP 800-63B-4, section 2.2.2). Our post on Entra’s SMS MFA retirement and moving to passkeys covers how Microsoft is retiring its own SMS and voice MFA delivery in Entra.
Identity management as a service, on open source
Skycloak is a managed identity platform in the same category as Auth0 or Okta, built on real upstream Keycloak. You get SSO, MFA, SCIM and audit logs without running the servers, and you can export and self-host whenever you want.
Why a passkey is one step but still two factors
A passkey counts as two factors even though you only touch a fingerprint sensor once, because the passkey itself is the “something you have” (a private key on a device you hold), and your fingerprint, face or device PIN is the second factor that unlocks it. NIST calls this a multi-factor authenticator: it “requires two factors to execute a single authentication event, such as a cryptographically secure device with an integrated biometric sensor that is required to activate the device” (NIST SP 800-63B-4, section 2.2.1).
The FIDO Alliance notes that biometric information “continues to stay on the device and is never sent to any remote server”, and that the server “only sees an assurance that the biometric check was successful” (FIDO Alliance, FIDO2).
One difference between synced passkeys and hardware keys matters for high-risk accounts. NIST says “Since syncable authenticators (described in Appendix B) require the private key to be exportable, syncable authenticators SHALL NOT be used at AAL3”, its highest level (NIST SP 800-63B-4, section 2.3.2), while they “may be offered as a new option for applications that seek to implement up to AAL2” (NIST SP 800-63B-4, Appendix B). For most users a synced passkey is the right choice, and hardware keys are worth requiring for administrators. Our post on FIDO2 for passwordless security covers the standard in more detail.
How to turn on 2FA for your own accounts
Which method should you pick?
Pick the strongest option the site offers that you will actually keep using:
- A passkey, if the site offers one. It is the fastest to use and the one a fake login page cannot steal.
- A hardware security key, if you have one or your employer issues them. Register two and keep the spare somewhere safe.
- An authenticator app, if the site only offers codes. Check whether your app can back up its codes, since that saves you when you replace your phone.
- SMS, only if nothing else is available. It is still far better than a password alone.
The steps that work on most sites
On most sites the steps go like this, and the FTC’s guide to using two-factor authentication gives the same general advice:
- Open the account’s settings and find the security or sign-in section.
- Choose a method. For a passkey, confirm with your fingerprint, face or device PIN. For an app, scan the QR code the site shows and type the first code to prove it worked.
- Save the recovery or backup codes the site gives you, somewhere other than the phone you just set up, so that losing that phone does not lock you out.
- Sign out and sign back in once, to check that the second step works while you can still fix it.
Start with your email account, and then move on to any account that controls your money or your work.
Turning on 2FA for a Google Account
Google calls it 2-Step Verification. Open your Google Account, tap Security & sign-in, select 2-Step Verification under “How you sign in to Google”, and follow the on-screen steps. Its options include Google prompts, passkeys and hardware security keys, code apps, and codes by text or voice call, and you can print or download a set of backup codes (Google Account Help, 2-Step Verification).
Turning on 2FA for an Apple Account
On an iPhone or iPad, go to Settings, or on a Mac to System Settings, then choose your name, then Sign-In & Security, turn on two-factor authentication and follow the onscreen instructions. Apple says that when you then sign in on a new device or the web, “you might need to enter a verification code”, which is “automatically displayed on your trusted devices or sent to your trusted phone number” (Apple Support, two-factor authentication).
If a label on your screen differs from these steps, follow the linked official page.
Turning on 2FA on a login that uses Keycloak
If your employer or an app you use signs you in through Keycloak, you set this up in its account console. Click Account security, then Signing in, and choose Set up Authenticator application or Set up a Passkey, then sign out and back in to test it (Keycloak Server Administration Guide, two-factor authentication with OTP and with WebAuthn).
For admins: how to require 2FA for every app
If every application implements its own second factor, you end up with several enrolment screens, several sets of recovery codes and no single place to change the policy. When applications sign users in through one identity provider over OpenID Connect or SAML, you can set the second-factor rules in the identity provider’s login flow instead of in each application. We compared this model across vendors in Okta policies versus Keycloak authentication flows, and looked at rollout patterns in how to set up MFA for enterprise applications.
How Keycloak asks for a second factor today
In a Keycloak realm (a realm “manages a set of users, credentials, roles, and groups”, and realms are isolated from one another, as the Keycloak core concepts put it), the default browser flow first shows the Username Password Form, then runs a Browser - Conditional 2FA sub-flow. That sub-flow runs “only when the user has a configured OTP credential”, so out of the box a user who never set up an authenticator app is not asked for one (Keycloak Server Administration Guide, built-in flows). The same sub-flow already contains WebAuthn Authenticator and Recovery Authentication Code Form executions, disabled by default, which you switch to Alternative to let users choose a security key or recovery codes as their second step. It also contains a Condition - credential step, so that when passkeys are enabled in the realm the second factor is skipped for a user who already signed in with a passkey (Keycloak Server Administration Guide, passkeys).

Require an authenticator app for everyone
- Set the code rules under Authentication, Policy tab, OTP Policy tab. Keycloak supports time-based (TOTP) and counter-based (HOTP) codes, and its guide says TOTP “is more secure than HOTP because the matchable OTP is valid for a short window of time” (Keycloak Server Administration Guide, OTP policies).
- Make enrolment part of the first login. Under Authentication, Required Actions tab, tick Set as default action for Configure OTP, so new users must set up an app the first time they sign in (Keycloak Server Administration Guide, default required actions). For users who already exist, add Configure OTP as a required action on the user’s Details tab (Keycloak Server Administration Guide, required actions). Configure OTP can also hand out recovery codes when you turn on Add recovery codes (Keycloak Server Administration Guide, recovery codes).
- Close the gap for users without a second factor. Because the default sub-flow is conditional, a copy of the browser flow needs one more change. The Keycloak guide gives two patterns: add a step that denies access when no second factor ran, or make the OTP Form required so that “if the user has none of the 2FA methods configured, the OTP setup will be enforced to continue the login” (Keycloak Server Administration Guide, Conditional 2FA sub-flow with OTP default).
If only some applications need a second factor, or only some actions inside an application, ask for it there instead of for everyone; our Keycloak step-up authentication guide shows how, and the ultimate guide to custom authentication flows explains how flows and conditions fit together.
Require a passkey or security key instead
For security keys as a second factor, enable the Webauthn Register required action, optionally as a default action, and set the matching WebAuthn Authenticator step in the conditional 2FA sub-flow (Keycloak Server Administration Guide, WebAuthn setup). Registration rules such as the relying party ID, allowed signature algorithms and user verification live under Authentication, Policy tab, WebAuthn Policy tab (Keycloak Server Administration Guide, managing WebAuthn policy).
Passkeys used to sign in without a password have their own policy. Keycloak keeps a separate WebAuthn Passwordless Policy, a Webauthn Register Passwordless required action and a WebAuthn Passwordless Authenticator, so you can set stricter rules for passwordless credentials, such as requiring a PIN (Keycloak Server Administration Guide, passwordless WebAuthn together with two-factor). To offer passkeys on the standard login page, turn on Enable Passkeys under Realm settings, Login tab (Keycloak Server Administration Guide, passkeys). The full walkthrough, including the browser side, is in our Keycloak passkeys and WebAuthn guide.
The same settings as recipes on Skycloak
On a managed Keycloak realm in Skycloak, you can apply these settings from the dashboard instead of editing flows by hand. Select your cluster and realm, click Recipes, and choose one of two:
- Multi-factor authentication (MFA) offers an authenticator app, a security key or both as the second factor, and its Enforce for all users setting is there “to prompt every user, including existing ones, to set up a second factor at their next login”, in the words of the Skycloak security hardening guide.
- Passkeys registers passkeys as the way to sign in, in a separate identity-first flow that keeps the password and a conditional OTP as a fallback for anyone without a passkey yet, and it has the same Enforce for all users option (Skycloak docs, passkeys recipe).
The two can run together, so users who have registered a passkey sign in with it while everyone else gets a password plus a second factor. If you keep realm settings as files instead, our Keycloak config generator builds a realm configuration, including its clients and roles, that you can start from.
How to roll 2FA out without locking people out
You do not have to enforce a second factor on every user at once, and going in stages gives you time to fix enrolment problems. You can start with the accounts attackers want most, such as administrators, and with the resources you most need to protect. After that, let the rest of your users opt in for a while, then turn on enforcement once most of them have enrolled.
Before you enforce anything, decide what happens when someone loses their device. Turn on recovery codes, ask users to register a second method where they can, and write down how your helpdesk checks a person’s identity before it resets their second factor, because our post on MFA fatigue and helpdesk vishing shows attackers calling helpdesks for exactly that. Then test the whole login in a private browser window with a test user who has no second factor yet, since that is the path every unenrolled user will take.
Frequently asked questions
Is 2FA the same as MFA?
2FA is a kind of MFA with exactly two factors. MFA means two or more, but in practice most MFA logins use two, so the terms are used almost interchangeably.
Is SMS 2FA better than nothing?
Yes. CISA’s fact sheet says that “any form of MFA is better than no MFA” (CISA, Implementing Phishing-Resistant MFA). SMS stops an attacker who only has your password, but it does not stop SIM swaps or real-time phishing, so move to an app or a passkey when the site allows it.
Can 2FA be hacked?
Every method that relies on you typing or approving something can be defeated by a convincing phishing page or by repeated push prompts. Passkeys and security keys resist both because the credential is bound to the real site’s origin. Attackers can still steal a session cookie after you log in, which our post on infostealers and stolen sessions covers.
What happens if I lose my phone?
Use the recovery codes you saved when you set up 2FA, or a second registered method such as a spare security key or a passkey synced to another device. If you have neither, you will need the site’s account recovery process, which is usually slower and stricter.
Does a passkey replace my password and 2FA?
It can. A passkey proves possession of a device and is unlocked by your fingerprint, face or PIN, which NIST describes as a multi-factor authenticator that “requires two factors to execute a single authentication event” (NIST SP 800-63B-4, section 2.2.1). When passkeys are enabled in a Keycloak realm, its default browser flow treats it that way and skips the extra 2FA step after a passkey sign-in (Keycloak Server Administration Guide, passkeys). Google does the same, saying a passkey sign-in “bypasses your second authentication step” (Google Account Help).
What to do next
If you are a user, turn on the strongest method your accounts offer, starting with your email account, and save your recovery codes. If you are an admin, require an authenticator app or a passkey for every user at your identity provider, encourage passkeys, and keep SMS as a fallback only where you must. Skycloak is a fully managed Keycloak service, so you can require OTP or passkeys for every app in a realm; the page on multi-factor authentication shows where each setting lives, and you can try managed Keycloak on Skycloak.
Sources
- NIST, SP 800-63B-4, “Digital Identity Guidelines: Authentication and Authenticator Management”, 2025, retrieved 2026-11-04, https://pages.nist.gov/800-63-4/sp800-63b.html
- CISA, “Implementing Phishing-Resistant MFA” fact sheet, 2022, retrieved 2026-11-04, https://www.cisa.gov/sites/default/files/publications/fact-sheet-implementing-phishing-resistant-mfa-508c.pdf
- SecurityWeek, article quoting CISA’s fact sheet, 2022, retrieved 2026-11-05, https://www.securityweek.com/cisa-urges-organizations-implement-phishing-resistant-mfa
- IETF, RFC 6238, “TOTP: Time-Based One-Time Password Algorithm”, 2011, retrieved 2026-11-04, https://datatracker.ietf.org/doc/html/rfc6238
- IETF, RFC 4226, “HOTP: An HMAC-Based One-Time Password Algorithm”, 2005, retrieved 2026-11-04, https://datatracker.ietf.org/doc/html/rfc4226
- W3C, “Web Authentication: An API for accessing Public Key Credentials Level 2”, 2021, retrieved 2026-11-04, https://www.w3.org/TR/webauthn-2/
- FIDO Alliance, “FIDO2”, retrieved 2026-11-04, https://fidoalliance.org/fido2/
- FTC, “SIM Swap Scams: How to Protect Yourself”, 2019, retrieved 2026-11-04, https://consumer.ftc.gov/consumer-alerts/2019/10/sim-swap-scams-how-protect-yourself
- FTC, “Use Two-Factor Authentication to Protect Your Accounts”, retrieved 2026-11-04, https://consumer.ftc.gov/articles/use-two-factor-authentication-protect-your-accounts
- Google Account Help, “Turn on 2-Step Verification”, retrieved 2026-11-04, https://support.google.com/accounts/answer/185839
- Apple Support, “Two-factor authentication for Apple Account”, retrieved 2026-11-04, https://support.apple.com/en-us/102660
- Keycloak, “Server Administration Guide”, retrieved 2026-11-04, https://www.keycloak.org/docs/latest/server_admin/index.html



