Class RequestOneTimeCodeUseCase

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

@Service @Transactional public class RequestOneTimeCodeUseCase extends Object
Sends a one-time code to a user, by e-mail or SMS.

The request is enumeration-safe: for an unknown identifier, a disabled account or an account without a verified address for the channel nothing is stored or sent, yet the same result is returned and a code is still generated and hashed, so the work done is alike. The code is never delivered inside this transaction: an e-mail is requested through an EmailCodeRequested event and an SMS through SendSmsOneTimeCodeUseCase, which records its cost and publishes an SmsRequested event; both are handled after the transaction commits and off the request thread, so no remote call holds a database connection and the latency of the mail server or the SMS gateway cannot tell an existing account from an unknown one. A refused SMS (country, budget) is not reported to the caller.

A new code replaces the previous one of the same binding and user (the repository saves one active code per binding and username). Magic links are not handled here: they use the framework's token service.

  • Field Details

    • resolveOttDestinationUseCase

      private final ResolveOttDestinationUseCase resolveOttDestinationUseCase
      Use case that finds where the code is delivered.
    • oneTimeCodeGenerator

      private final OneTimeCodeGenerator oneTimeCodeGenerator
      Generator of codes.
    • oneTimeCodeHasher

      private final OneTimeCodeHasher oneTimeCodeHasher
      Hasher of codes.
    • oneTimeCodeRepository

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

      private final org.springframework.context.ApplicationEventPublisher eventPublisher
      Publishes the request of an e-mail delivery.
    • sendSmsOneTimeCodeUseCase

      private final SendSmsOneTimeCodeUseCase sendSmsOneTimeCodeUseCase
      Use case that records the cost of an SMS and requests its delivery.
    • ottPolicy

      private final OttPolicy ottPolicy
      Policy of the codes.
    • clock

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

    • RequestOneTimeCodeUseCase

      public RequestOneTimeCodeUseCase()
  • Method Details

    • execute

      public OneTimeCodeRequestResult execute(String identifier, OttChannel channel, OttPurpose purpose, String bindingId)
      Requests a code.
      Parameters:
      identifier - the name the user signs in with, as typed.
      channel - the channel to deliver the code through.
      purpose - what the code is for.
      bindingId - identifier of the sign-in attempt the code is bound to (for example the session id).
      Returns:
      the same result for every identifier.
    • requestDelivery

      private void requestDelivery(OttDestination destination, String code, Duration ttl)
      Requests the delivery of a code, swallowing an SMS refusal so that it cannot be told apart from success.
      Parameters:
      destination - where to deliver it.
      code - the clear-text code.
      ttl - how long it is valid.