Class VerifyOneTimeCodeUseCase

java.lang.Object
com.irurueta.authserver.authorization.application.ott.VerifyOneTimeCodeUseCase

@Service @Transactional public class VerifyOneTimeCodeUseCase extends Object
Verifies a one-time code.

A code is checked at most OttPolicy.maxAttempts() times: the attempt counter is incremented atomically before the comparison, and the code is burnt (consumed) after the last wrong attempt or when the counter is already past the limit. A correct code is accepted once: consuming it must succeed, so of two concurrent submissions of the same code only one is accepted. Wrong, expired, burnt, already used and mismatching-purpose codes all give the same OttVerificationResult.failure(). So does a code delivered through a channel the caller does not accept (for a second factor, the channel that signed the user in, or e-mail for a user with another confirmed second factor): it is burnt and, as a second factor, counted against the lockout and never audited as a success.

For OttPurpose.SECOND_FACTOR the shared second-factor lockout applies exactly as for TOTP: it is checked first (a locked user gets OttVerificationResult.locked() and nothing is compared), every failure is counted, and a success clears the counter and is audited. A OttPurpose.LOGIN code is not counted: the framework's own authentication events audit it.

  • Field Details

    • AUDIT_DETAIL

      private static final String AUDIT_DETAIL
      Detail stored in the audit event of an accepted code.
      See Also:
    • oneTimeCodeRepository

      private final OneTimeCodeRepository oneTimeCodeRepository
      Port that stores the codes.
    • oneTimeCodeHasher

      private final OneTimeCodeHasher oneTimeCodeHasher
      Hasher of codes.
    • ottPolicy

      private final OttPolicy ottPolicy
      Policy of the codes.
    • findUserAccountUseCase

      private final FindUserAccountUseCase findUserAccountUseCase
      Use case that finds the account of the user.
    • checkSecondFactorLockoutUseCase

      private final CheckSecondFactorLockoutUseCase checkSecondFactorLockoutUseCase
      Use case that tells whether the second factor is locked (and serializes the attempts of a user).
    • registerSecondFactorFailureUseCase

      private final RegisterSecondFactorFailureUseCase registerSecondFactorFailureUseCase
      Use case that counts a failure.
    • resetSecondFactorFailuresUseCase

      private final ResetSecondFactorFailuresUseCase resetSecondFactorFailuresUseCase
      Use case that clears the failures.
    • recordSecurityEventUseCase

      private final RecordSecurityEventUseCase recordSecurityEventUseCase
      Use case that records the audit events.
    • clock

      private final Clock clock
      Source of the current time.
  • Constructor Details

    • VerifyOneTimeCodeUseCase

      public VerifyOneTimeCodeUseCase()
  • Method Details

    • execute

      public OttVerificationResult execute(String bindingId, String username, String code, OttPurpose purpose, Set<OttChannel> channels)
      Verifies a code.
      Parameters:
      bindingId - identifier of the sign-in attempt the code was bound to.
      username - name of the user.
      code - the code as typed.
      purpose - what the code is for.
      channels - the channels whose codes are accepted; a code of any other channel is a failure.
      Returns:
      the outcome.
    • recordSecondFactorOutcome

      private void recordSecondFactorOutcome(OttVerificationResult result, UserAccount account)
      Counts the outcome of a second-factor verification.
      Parameters:
      result - the outcome.
      account - the account of the user.
    • check

      private OttVerificationResult check(String bindingId, String username, String code, OttPurpose purpose, Set<OttChannel> channels)
      Checks a code against the active one, counting the attempt.
      Parameters:
      bindingId - identifier of the sign-in attempt.
      username - name of the user.
      code - the code as typed.
      purpose - what the code is for.
      channels - the channels whose codes are accepted.
      Returns:
      success when the code is valid and was consumed, failure otherwise.
    • accept

      private OttVerificationResult accept(OneTimeCode active)
      Consumes a correct code; a code consumed by a concurrent request is refused.
      Parameters:
      active - the stored code.
      Returns:
      success when this request consumed it, failure otherwise.