One-time tokens (e-mail, magic links and SMS)
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
A one-time token is a short-lived secret sent over a channel the user controls: an e-mail code, a magic link or an SMS
code. It works as a passwordless primary sign-in and as a second factor. The framework mechanism is Spring Security’s
http.oneTimeTokenLogin(…) (POST /ott/generate, POST /login/ott); this server adds the numeric code, the
factors, the SMS gateways, the cost control and the pages.
Factors
OttFactors registers four factors. The second-factor variants are separate authorities, so a code used to sign in is
never also counted as the second factor. See Multi-factor authentication and step-up for how the policy, amr and step-up use them.
/**
* Registers the one-time token methods as authentication factors. One authority cannot carry two strengths and the
* registry refuses two factors with the same authority, so each use of each channel is a factor of its own: the
* e-mail code or magic link as a sign-in ({@code FACTOR_OTT}, Spring Security's own constant) and the SMS code as a
* sign-in are {@link FactorStrength#PRIMARY} (they prove the possession of a mailbox or a phone, which is as strong as
* a password and no more), and the same two as the second factor of a user who already signed in with a primary
* factor are {@link FactorStrength#SECOND}. The {@code amr} values are those of RFC 8176: {@code otp} for the code or
* the link sent by e-mail and {@code sms} for the code sent by SMS.
*/
@Configuration(proxyBeanMethods = false)
public class OttFactors {
/** RFC 8176 {@code amr} value of a one-time password sent by e-mail. */
public static final String EMAIL_AMR_VALUE = "otp";
/** RFC 8176 {@code amr} value of a one-time password sent by SMS. */
public static final String SMS_AMR_VALUE = "sms";
/** Authority of the e-mail code or magic link as a sign-in. */
public static final String EMAIL_AUTHORITY = FactorGrantedAuthority.OTT_AUTHORITY;
/** Authority of the SMS code as a sign-in. */
public static final String SMS_AUTHORITY = "FACTOR_OTT_SMS";
/** Authority of the e-mail code or magic link as a second factor. */
public static final String EMAIL_SECOND_FACTOR_AUTHORITY = "FACTOR_OTT_EMAIL_2FA";
/** Authority of the SMS code as a second factor. */
public static final String SMS_SECOND_FACTOR_AUTHORITY = "FACTOR_OTT_SMS_2FA";
/**
* Declares the e-mail code and magic link as a sign-in.
*
* @return the factor.
*/
@Bean
public AuthenticationFactor ottEmailAuthenticationFactor() {
return new AuthenticationFactor("ott-email", EMAIL_AMR_VALUE, EMAIL_AUTHORITY, FactorStrength.PRIMARY);
}
/**
* Declares the SMS code as a sign-in.
*
* @return the factor.
*/
@Bean
public AuthenticationFactor ottSmsAuthenticationFactor() {
return new AuthenticationFactor("ott-sms", SMS_AMR_VALUE, SMS_AUTHORITY, FactorStrength.PRIMARY);
}
/**
* Declares the e-mail code and magic link as a second factor.
*
* @return the factor.
*/
@Bean
public AuthenticationFactor ottEmailSecondAuthenticationFactor() {
return new AuthenticationFactor("ott-email-2fa", EMAIL_AMR_VALUE, EMAIL_SECOND_FACTOR_AUTHORITY,
FactorStrength.SECOND);
}
/**
* Declares the SMS code as a second factor.
*
* @return the factor.
*/
@Bean
public AuthenticationFactor ottSmsSecondAuthenticationFactor() {
return new AuthenticationFactor("ott-sms-2fa", SMS_AMR_VALUE, SMS_SECOND_FACTOR_AUTHORITY,
FactorStrength.SECOND);
}
Generating a token
POST /ott/generate always answers with the same redirect to /login/ott, whether or not the account exists, so
nothing reveals accounts. The request resolver sets the lifetime of a magic link from the policy.
/**
* Resolves the request of {@code POST /ott/generate} to the token Spring's filter asks the token service for. The
* identifier is the {@code username} field of a sign-in and, for a second factor, the name of the signed-in user (never
* a field a client could point at somebody else), cleaned of control characters and cut to what the token store
* holds. The lifetime of the token is the one {@link OttPolicy} sets for its kind. A request that names no identifier
* is not resolved, so the chain goes on as it would for any other path. Whether the identifier belongs to an account
* is not looked at here: a token is issued for any identifier, and what is delivered is decided afterwards, so the
* answer never tells.
*/
@RequiredArgsConstructor
public class OttGenerateRequestResolver implements GenerateOneTimeTokenRequestResolver {
/** The rules of one-time tokens, which set the lifetime of each kind. */
private final OttPolicy ottPolicy;
/**
* Resolves the request.
*
* @param request the request.
* @return the token to generate, or {@code null} when the request names no identifier.
*/
@Override
public GenerateOneTimeTokenRequest resolve(final HttpServletRequest request) {
final var current = SecurityContextHolder.getContext().getAuthentication();
final var parameters = OttGenerationParameters.from(request, current);
final var secondFactor = parameters.purpose() == OttPurpose.SECOND_FACTOR;
final var identifier = LinkTokenUsername.clean(
secondFactor ? current.getName() : request.getParameter(OttGenerationParameters.USERNAME_PARAMETER));
if (!StringUtils.hasText(identifier)) {
return null;
}
final var username = LinkTokenUsername.forLink(identifier,
secondFactor && parameters.kind() == OttKind.LINK);
return new GenerateOneTimeTokenRequest(username, ottPolicy.ttl(parameters.kind()));
}
}
The token is never delivered on the request thread nor inside a database transaction. The use cases store the code
(or resolve the address of a link, or record the cost of an SMS) and publish a delivery event; OttDeliveryListener
sends it after the transaction commits (@TransactionalEventListener(phase = AFTER_COMMIT)) and off the request
thread (@Async, on Spring Boot’s applicationTaskExecutor, enabled once by AsyncConfiguration). A slow or failing
mail server or SMS gateway therefore holds no database connection, never shows in the response, and cannot tell an
existing account from an unknown one by the time the request takes. A failed delivery is logged without the address,
the number or the token.
/**
* Delivers the one-time tokens the use cases asked for, once the transaction that stored them has committed
* ({@link TransactionPhase#AFTER_COMMIT}; an event published outside a transaction is delivered at once) and off the
* request thread ({@code @Async}, on Spring Boot's {@code applicationTaskExecutor}). So no mail server or SMS gateway
* is ever called inside a transaction, a slow one holds no database connection, a rolled-back request sends nothing,
* and the time the request takes is the same whether or not something is sent, which keeps the latency from telling
* an existing account from an unknown one. A failure is logged without the address, the number or the token, and
* never reaches the response.
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class OttDeliveryListener {
/** Delivers the tokens. */
private final DeliverOneTimeTokenUseCase deliverOneTimeTokenUseCase;
/**
* E-mails a code.
*
* @param event what to deliver.
*/
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
public void onEmailCode(final EmailCodeRequested event) {
try {
deliverOneTimeTokenUseCase.deliverEmailCode(event);
} catch (final OttDeliveryException e) {
log.warn("A one-time code was not delivered by e-mail: {}", e.getClass().getSimpleName());
}
}
/**
* E-mails a magic link.
*
* @param event what to deliver.
*/
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
public void onEmailLink(final EmailLinkRequested event) {
try {
deliverOneTimeTokenUseCase.deliverEmailLink(event);
} catch (final OttDeliveryException e) {
log.warn("A magic link was not delivered: {}", e.getClass().getSimpleName());
}
}
/**
* Sends a text message.
*
* @param event what to send.
*/
@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
public void onSms(final SmsRequested event) {
try {
deliverOneTimeTokenUseCase.deliverSms(event);
} catch (final SmsDeliveryException e) {
log.warn("An SMS was not delivered: {}", e.getClass().getSimpleName());
}
}
}
The sign-in page shows the e-mail / SMS form as a login method fragment.
/**
* Contributes the one-time token sign-in to the login page: a form that asks for the identifier and whether to receive
* a code or a magic link by e-mail, or a code by SMS, and posts it to {@code POST /ott/generate}. It renders below the
* passkey sign-in.
*/
@Component
public class OttLoginMethodFragment implements LoginMethodFragment {
/** Position of the one-time token sign-in in the login page: after the passkey sign-in. */
public static final int ORDER = 300;
/**
* Gets the template that holds the fragment.
*
* @return {@code login-ott}.
*/
@Override
public String templateName() {
return "login-ott";
}
/**
* Gets the fragment of the template.
*
* @return {@code login}.
*/
@Override
public String fragmentName() {
return "login";
}
/**
* Gets the position of the fragment.
*
* @return {@link #ORDER}.
*/
@Override
public int order() {
return ORDER;
}
}
<form th:fragment="login" class="ott-login" method="post" th:action="@{/ott/generate}">
<h2 th:text="#{ott.login.heading}">Sign in with a code or a link</h2>
<input type="hidden" name="purpose" value="login">
<p>
<label for="ott-username" th:text="#{ott.login.username}">Username</label>
<input type="text" id="ott-username" name="username" required autocomplete="username" aria-describedby="ott-hint">
</p>
<p id="ott-hint" th:text="#{ott.login.hint}">We will send you a code or a link if the account exists.</p>
<fieldset>
<legend th:text="#{ott.login.channel}">Receive it by</legend>
<label><input type="radio" name="channel" value="email" checked>
<span th:text="#{ott.login.channel.email}">E-mail</span></label>
<label><input type="radio" name="channel" value="sms">
<span th:text="#{ott.login.channel.sms}">SMS (only if you have a verified phone)</span></label>
</fieldset>
<fieldset>
<legend th:text="#{ott.login.kind}">Use</legend>
<label><input type="radio" name="kind" value="code" checked>
<span th:text="#{ott.login.kind.code}">A code</span></label>
<label><input type="radio" name="kind" value="link">
<span th:text="#{ott.login.kind.link}">A link (e-mail only)</span></label>
</fieldset>
<button type="submit" th:text="#{ott.login.submit}">Send</button>
</form>
As a second factor
Which channels may be the second factor of a signed-in user is one rule, OttSecondFactorChannels, applied in three
places: what the second-factor page offers, whether a requested token is delivered at all (POST /ott/generate is
still answered with the same redirect, but nothing is sent), and whether a presented code or link is accepted. The rule
is checked before a code is compared, so a code of a channel that is not allowed counts against the second-factor
lockout and is never audited as a success.
-
The channel that signed the user in is never its own second factor.
-
SMS needs a verified phone.
-
E-mail is the second factor only of a user without any other confirmed second factor (authenticator app, passkey, verified phone). The password is reset by e-mail too, so a mailbox is not independent from the password: whoever controls it could otherwise skip the stronger factor the user enrolled. For a user whose multi-factor authentication was forced by an administrator before anything was enrolled, the e-mail is then the only second factor available.
/**
* Decides through which channels a one-time code or link may be the second factor of a signed-in user. The same rule
* decides what the second-factor page offers, whether a requested token is delivered at all and whether a presented
* one is accepted, so a channel that is not offered can neither be asked for nor used by posting the forms by hand:
* <ul>
* <li>never the channel that signed the user in (the same mailbox or phone would be proved twice);</li>
* <li>SMS only to a user with a verified phone;</li>
* <li>e-mail only to a user without any other confirmed second factor (authenticator app, passkey, verified phone).
* The password is reset by e-mail too, so a mailbox is not independent from the password: whoever controls it could
* otherwise skip the stronger factor the user enrolled. For a user whose multi-factor authentication was forced by an
* administrator before any factor was enrolled, the e-mail is then the only second factor available.</li>
* </ul>
*/
@Component
@RequiredArgsConstructor
public class OttSecondFactorChannels {
/** Rules about the session a second factor is presented in. */
private final PrimaryFactorSession primaryFactorSession;
/** Resolves the identifier of the account of the signed-in user. */
private final SecondFactorUserResolver userResolver;
/** Tells whether the user has a confirmed second factor of any kind. */
private final HasConfirmedSecondFactorUseCase hasConfirmedSecondFactorUseCase;
/** Tells whether the user has a verified phone. */
private final HasVerifiedPhoneUseCase hasVerifiedPhoneUseCase;
/**
* Gets the channels a signed-in user may use as the second factor.
*
* @param authentication the authentication of the session, which holds a primary factor.
* @return the channels; empty when none may be used.
*/
public Set<OttChannel> allowed(final Authentication authentication) {
final var userId = userResolver.userIdOf(authentication.getName());
final var channels = EnumSet.noneOf(OttChannel.class);
if (!primaryFactorSession.signedInThrough(authentication, OttChannel.EMAIL)
&& !hasConfirmedSecondFactorUseCase.execute(userId)) {
channels.add(OttChannel.EMAIL);
}
if (!primaryFactorSession.signedInThrough(authentication, OttChannel.SMS)
&& hasVerifiedPhoneUseCase.execute(userId)) {
channels.add(OttChannel.SMS);
}
return channels;
}
/**
* Tells whether a signed-in user may use a channel as the second factor.
*
* @param authentication the authentication of the session, which holds a primary factor.
* @param channel the channel.
* @return {@code true} when the channel is one of {@link #allowed(Authentication)}.
*/
public boolean isAllowed(final Authentication authentication, final OttChannel channel) {
return allowed(authentication).contains(channel);
}
}
A consequence of the e-mail rule: a user whose only second factor is the verified phone, and who signs in with an SMS code, has no one-time token left to complete the second factor (SMS signed in, and e-mail is not allowed next to the phone); that user signs in with the password, or enrols another factor.
A second-factor magic link carries a marker in the user name Spring’s token store keeps, so a user name longer than
LinkTokenUsername.MAX_SECOND_FACTOR_LENGTH (99 characters) would not fit whole; such a link is not delivered.
Phone
Starting the enrolment of a phone (PUT /api/account/phone and the form of /account/phone) needs a recent
multi-factor sign-in while the account already has a confirmed second factor, as removing it does, because the new
number replaces a second factor. Replacing a verified phone revokes every trusted device of the user and is audited
(SECOND_FACTOR_REMOVED); removing it is audited and, when no other second factor is left, revokes the trusted devices
too. Two users confirming the same number at once are serialised by a PostgreSQL advisory lock on the number, so the
second one is refused as an invalid code and never fails the request.
Schema and settings
Spring’s own one_time_tokens table holds the link tokens; the numeric codes are ours and only their hash is stored.
-- Spring Security's one_time_tokens schema (JdbcOneTimeTokenService), adapted to PostgreSQL. The service issues the
-- UUID of a magic link; its SQL is fixed, so the table and column names must not change.
CREATE TABLE one_time_tokens
(
token_value varchar(36) NOT NULL,
username varchar(100) NOT NULL,
expires_at timestamp with time zone NOT NULL,
PRIMARY KEY (token_value)
);
CREATE INDEX ix_one_time_tokens_expires_at ON one_time_tokens (expires_at);
# Digits of the e-mail / SMS code, how long a code and a magic link stay valid, and how many wrong codes are
# accepted before the code is spent. The values shown are the defaults of OttProperties.
code-length: 6
code-ttl: PT10M
link-ttl: PT15M
max-attempts: 5
Rate limits
Generation and verification are limited per client address and per identifier (Bucket4j with Caffeine); an exceeded
limit answers 429. The filter is added before Spring’s generate filter, which answers POST /ott/generate itself.
/**
* Rate limiter of the one-time token endpoints, on the pattern of the account endpoints' limiter: Bucket4j token
* buckets in Caffeine caches ({@link FixedWindowBuckets}). Requests for a token (generation) and submissions of a token
* or code (verification) are limited separately, and each twice: per client address and per identifier (the user name
* the request names), so a single client cannot flood the mailboxes and the SMS budget, many clients cannot aim at one
* account, and a code cannot be guessed faster than the limits allow, on top of the attempt counter of the code
* itself. The identifier is limited whether or not it belongs to an account, so the limit never tells which ones
* exist.
*
* <p>Each bucket holds the limit of its key and is refilled all at once every
* {@link OttRateLimitProperties#window()}. The buckets live in this instance only: several instances each apply the
* limits on their own. The client address is {@code request.getRemoteAddr()}, so behind a proxy
* {@code server.forward-headers-strategy} must be set for it to be the real client.
*/
@Component
@EnableConfigurationProperties(OttRateLimitProperties.class)
public final class OttRateLimiter {
/** Key used when the client address is unknown. */
private static final String UNKNOWN_CLIENT = "unknown";
/** Limits, window and bound of the tracked keys. */
private final OttRateLimitProperties properties;
/** Buckets of the token requests per client address. */
private final FixedWindowBuckets generationClients;
/** Buckets of the token requests per identifier. */
private final FixedWindowBuckets generationIdentifiers;
/** Buckets of the submissions per client address. */
private final FixedWindowBuckets verificationClients;
/** Buckets of the submissions per identifier. */
private final FixedWindowBuckets verificationIdentifiers;
/**
* Creates the limiter.
*
* @param properties limits, window and bound of the tracked keys.
* @param clock source of the current time.
*/
public OttRateLimiter(final OttRateLimitProperties properties, final Clock clock) {
this.properties = properties;
generationClients = buckets(clock);
generationIdentifiers = buckets(clock);
verificationClients = buckets(clock);
verificationIdentifiers = buckets(clock);
}
/**
* Counts one request for a token and rejects it once a limit is exceeded.
*
* @param clientAddress address of the calling client.
* @param identifier identifier the request names, or {@code null} when it names none.
* @throws OttRateLimitExceededException if the client or the identifier exceeded its limit.
*/
public void checkGeneration(final String clientAddress, final String identifier) {
check(generationClients, properties.generationPerClient(), generationIdentifiers,
properties.generationPerIdentifier(), clientAddress, identifier);
}
/**
* Counts one submission of a token or a code and rejects it once a limit is exceeded.
*
* @param clientAddress address of the calling client.
* @param identifier identifier the submission is for, or {@code null} when it names none (a magic link).
* @throws OttRateLimitExceededException if the client or the identifier exceeded its limit.
*/
public void checkVerification(final String clientAddress, final String identifier) {
check(verificationClients, properties.verificationPerClient(), verificationIdentifiers,
properties.verificationPerIdentifier(), clientAddress, identifier);
}
/**
* Tells how many keys are tracked, after any pending eviction.
*
* @return the number of keys of the four kinds.
*/
long trackedKeys() {
return generationClients.trackedKeys() + generationIdentifiers.trackedKeys()
+ verificationClients.trackedKeys() + verificationIdentifiers.trackedKeys();
}
/**
* Applies the client and the identifier limits of one kind of request.
*
* @param clients buckets per client address.
* @param clientLimit requests per client address per window.
* @param identifiers buckets per identifier.
* @param identifierLimit requests per identifier per window.
* @param clientAddress address of the calling client.
* @param identifier identifier, or {@code null}.
* @throws OttRateLimitExceededException if a limit is exceeded.
*/
private void check(final FixedWindowBuckets clients, final int clientLimit, final FixedWindowBuckets identifiers,
final int identifierLimit, final String clientAddress, final String identifier) {
if (!properties.enabled()) {
return;
}
consume(clients, clientAddress == null ? UNKNOWN_CLIENT : clientAddress, clientLimit);
if (identifier != null) {
consume(identifiers, identifier.toLowerCase(Locale.ROOT), identifierLimit);
}
}
/**
* Takes one token from the bucket of a key.
*
* @param buckets buckets of one kind of key.
* @param key the key.
* @param limit requests the key may make per window.
* @throws OttRateLimitExceededException if the bucket is empty.
*/
private static void consume(final FixedWindowBuckets buckets, final String key, final int limit) {
final var wait = buckets.tryConsume(key, limit);
if (wait.isPresent()) {
throw new OttRateLimitExceededException(wait.get());
}
}
/**
* Builds the buckets of one kind of key.
*
* @param clock source of the current time.
* @return the buckets.
*/
private FixedWindowBuckets buckets(final Clock clock) {
return new FixedWindowBuckets(clock, properties.window(), properties.maxTrackedKeys());
}
}
SMS
The domain port SmsSender has three implementations chosen by a Spring profile, never by a property: twilio, sns,
and, only under the dev or test profile, a logging sender with a bounded in-memory outbox (shown at /dev/sms-inbox
under dev). With none of these profiles the application refuses to start (MissingSmsGatewayConfiguration), so a
deployment never silently keeps its messages in memory. The gateway credentials are supplied only through environment
variables (AUTHSERVER_SMS_TWILIO_*, the AWS default credential chain); none is stored in the repository.
/**
* Port that sends a text message through a gateway. No gateway type crosses it.
*/
public interface SmsSender {
/**
* Sends a text message.
*
* @param to recipient.
* @param text text of the message.
* @throws SmsDeliveryException if the gateway failed or refused the message.
*/
void send(PhoneNumber to, String text);
}
/**
* Wires the phone normaliser and the logging {@link SmsSender} of development and tests. The profile expressions make
* the three senders (logging, Twilio, SNS) mutually exclusive in every combination of profiles: the logging one only
* under {@code dev} or {@code test} and when neither gateway profile is active, Twilio unless SNS is also active, and
* SNS whenever it is active. With none of those profiles there is no sender at all and
* {@link MissingSmsGatewayConfiguration} stops the startup: a deployment never silently keeps its messages in memory.
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SmsOutboxProperties.class)
public class SmsAdapterConfiguration {
/** Profiles under which the logging sender (and its outbox) is used: development or tests, and no gateway. */
public static final String LOGGING_SENDER_PROFILES = "(dev | test) & !twilio & !sns";
/**
* Creates the phone normaliser.
*
* @return the libphonenumber based normaliser.
*/
@Bean
public PhoneNormalizer phoneNormalizer() {
return new LibPhoneNumberNormalizer(PhoneNumberUtil.getInstance());
}
/**
* Creates the resolver of the country of a phone number.
*
* @return the libphonenumber based resolver.
*/
@Bean
public PhoneCountryResolver phoneCountryResolver() {
return new LibPhoneNumberCountryResolver(PhoneNumberUtil.getInstance());
}
/**
* Creates the outbox of the logging sender.
*
* @param properties outbox settings.
* @return the outbox, readable by the development inbox.
*/
@Bean
@Profile(LOGGING_SENDER_PROFILES)
public SmsOutbox smsOutbox(final SmsOutboxProperties properties) {
return new SmsOutbox(properties.capacity());
}
/**
* Creates the logging sender of development and tests.
*
* @param outbox records the messages.
* @param clock source of the recording instant.
* @return the logging sender.
*/
@Bean
@Profile(LOGGING_SENDER_PROFILES)
public SmsSender loggingSmsSender(final SmsOutbox outbox, final Clock clock) {
return new LoggingSmsSender(outbox, clock);
}
}
Cost control checks the country allow-list and the per-window and per-day caps before each message. The caps are
checked and the cost recorded in one step (SmsCostLedgerRepository.tryRecord: a conditional insert after a
PostgreSQL transaction-level advisory lock), so concurrent senders cannot overshoot a budget and a refused message
leaves no ledger row; the message is handed to the gateway only after that transaction commits.
sms:
# Region numbers typed without a country prefix are read in, and the regions SMS may be sent to (ISO 3166-1
# alpha-2, upper case). The values shown are the defaults of SmsProperties.
default-region: US
allowed-countries: US,ES,GB
# Cost caps in millionths of the billing currency, so a bug or an attack cannot run up the bill: the price assumed
# for a country without an entry in cost-per-country-micros, and the most that may be spent per window and per day.
default-cost-micros: 50000
window: PT1H
window-cap-micros: 5000000
daily-cap-micros: 20000000
# Verification code of a phone being enrolled, and how long the spend ledger is kept.
verification-code-length: 6
verification-code-ttl: PT10M
verification-max-attempts: 5
ledger-retention: P2D
# Which gateway sends the messages is chosen with a Spring profile, never with a property: "twilio" sends through
# Twilio (application-twilio.yml) and "sns" through Amazon SNS (application-sns.yml, default AWS credential chain);
# only "dev" and "test" log the message to a bounded in-memory outbox (the dev profile shows it at
# /dev/sms-inbox). With none of these profiles the application refuses to start, so a deployment never silently
# keeps its messages in memory.