Interface OneTimeCodeRepository

All Known Implementing Classes:
JdbcOneTimeCodeRepository

public interface OneTimeCodeRepository
Port that stores the numeric one-time codes. An implementation never returns null.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Spends a code, in one atomic step, so that two concurrent requests cannot both use it.
    int
    Deletes every code that has expired.
    findActive(String bindingId, String username, Instant now)
    Finds the code that is still active for a binding and a user.
    int
    Counts a wrong submission, in one atomic step.
    void
    Stores a code, replacing any other active code of the same binding and user name so that only the latest one can be used.
  • Method Details

    • save

      void save(OneTimeCode code)
      Stores a code, replacing any other active code of the same binding and user name so that only the latest one can be used.
      Parameters:
      code - the code to store.
    • findActive

      Optional<OneTimeCode> findActive(String bindingId, String username, Instant now)
      Finds the code that is still active for a binding and a user.
      Parameters:
      bindingId - identifier binding the code to the requesting browser session.
      username - user name of the account.
      now - current instant; an expired code is not active.
      Returns:
      the active code, or empty when there is none.
    • incrementAttempts

      int incrementAttempts(UUID id)
      Counts a wrong submission, in one atomic step.
      Parameters:
      id - identifier of the code.
      Returns:
      the number of attempts after the increment, or 0 when the code no longer exists.
    • consume

      boolean consume(UUID id)
      Spends a code, in one atomic step, so that two concurrent requests cannot both use it.
      Parameters:
      id - identifier of the code.
      Returns:
      true when the code existed and is now spent.
    • deleteExpired

      int deleteExpired(Instant now)
      Deletes every code that has expired.
      Parameters:
      now - current instant.
      Returns:
      the number of deleted codes.