← All articlesWeb security

Single-use OTPs for MFA login: implementation and rejection testing

A one-time code must stop working for login after successful verification, even if it has not yet expired. Checking the code’s correctness and expiry is therefore insufficient: the server handler must track its use. We explain how to define this requirement for a web product and verify the result using your own test accounts. The main criterion is that presenting an accepted OTP again neither completes login nor creates a new authenticated session.

Define the requirement

MFA requires confirmation of at least two different authentication factors. Its primary use in an application is user login. This article covers the stage where the first factor has already been completed and the server accepts an OTP to finish login.

The single-use requirement can be stated as follows: a correct, unused OTP is accepted within its validity period; after successful verification, it becomes invalid. Presenting an already accepted code again must not complete MFA. This gives developers a concrete behavior to implement and product owners an observable acceptance criterion.

Why a short validity period is not enough

OWASP recommends giving OTPs a short validity period, ensuring single use and invalidating them after successful verification. These are separate conditions: remaining validity does not mean an already used code can be accepted again.

The trust boundary here lies between the code presented by the client and the server handler’s decision to complete login. The requirement must be checked on the server: does the handler track successful use and reject the next presentation? An interface message alone does not reveal the state the server actually accepted.

Fictional example: logging in to Draft

Imagine a wholly fictional web service called Draft and a test account owned by its team. The team has prepared its own protected test resource for this account. All data is synthetic, and testing takes place only in an environment the team owns.

A tester completes the first factor and enters a correct OTP before it expires. Login completes. Then, in a separate client, the tester completes the first factor again for the same account and presents the already accepted code while its original validity period is still running. If the server completes the second login, the single-use requirement has not been met. This checks the tracking of use specifically, rather than expiry.

Positive control: an unused code

The following plan contains original acceptance tests we propose, rather than an official OWASP procedure. Start with a positive control so you can compare rejection of a reused code with a working normal login using the same data you own.

  • Prepare your own test account and a client without a previously obtained authenticated session.
  • Complete the first factor and present a correct, unused OTP before it expires.
  • Confirm MFA completion and the creation of an authenticated session using server state.
  • Check access to your own protected test resource and record the result without the OTP value.

Presenting the code again: what counts as rejection

After the successful control, start a separate login attempt for the same account in an isolated client without a previously obtained authenticated session. Complete the first factor and present the accepted OTP again before its original validity period ends. The test conditions must let you distinguish rejection of a used code from rejection of an expired one.

Acceptance requires three consistent observations: MFA has not completed, server state shows no new authenticated session, and the protected test resource is inaccessible. Compare these with the positive control. An error message or a successful HTTP response alone is insufficient: neither proves that the server allowed or rejected the relevant state change.

What to fix in the server handler

The first priority is to trace the entire transition using your own synthetic data: successful OTP verification records its use and invalidates it; the next attempt checks that state and rejects the already accepted code, even if time remains. The fix must enforce this connection rather than merely change the message shown to the user.

Next, check that the validity period is short and prevent OTP values from being written to logs, as OWASP recommends. For acceptance results, record the verification outcome and the presence or absence of a new authenticated session without the code itself. After the fix, repeat both scenarios: the first login must complete, and the repeated presentation must be rejected under all the stated criteria.

Testing boundaries and the next step

These checks assess single use of OTPs during login under the selected conditions and provide no statistical guarantee of product security. The code-generation algorithm is not covered here. Replacing and recovering MFA factors, password resets, lockout after failed attempts and concealing account existence remain separate requirements. Session identifier renewal, session storage, revocation and timeouts are also outside the scope of this article.

The practical result is a testable handler requirement and two comparable scenarios that observe server state. If you need help assessing the implementation, CheckMyApp offers a free audit of your website or the web portion of your product. A discussion of testing can start with this requirement: an already accepted OTP must not create a new authenticated session.

Sources and documentation

  1. Multifactor Authentication Cheat Sheet
  2. Authentication Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free