java.lang.Object
com.irurueta.authserver.authorization.infrastructure.mail.ThymeleafOttMailer
All Implemented Interfaces:
OttDelivery

public class ThymeleafOttMailer extends Object implements OttDelivery
OttDelivery that e-mails one-time codes and magic links as multipart messages (plain text and HTML) rendered with Thymeleaf. Only OttChannel.EMAIL is handled: asking it to deliver through another channel is a programming error and is refused with an IllegalArgumentException, so a token is never silently dropped. Subject and texts come from a MessageSource resolved with the explicitly configured locale, never the platform's default. A delivery failure becomes an OttDeliveryException with a generic message and a sanitized cause: neither the address, nor the code, nor the link is ever logged or carried by an exception.
  • Field Details

    • CODE_TEMPLATE

      static final String CODE_TEMPLATE
      Template of the code message (without extension).
      See Also:
    • HTML_EXTENSION

      static final String HTML_EXTENSION
      Extension of the HTML templates.
      See Also:
    • TEXT_EXTENSION

      static final String TEXT_EXTENSION
      Extension of the plain-text templates.
      See Also:
    • CODE_SUBJECT

      static final String CODE_SUBJECT
      Message key of the subject of the code message.
      See Also:
    • CODE_VARIABLE

      static final String CODE_VARIABLE
      Template variable holding the code.
      See Also:
    • VALID_MINUTES_VARIABLE

      static final String VALID_MINUTES_VARIABLE
      Template variable holding the validity in minutes.
      See Also:
    • FAILURE_MESSAGE

      static final String FAILURE_MESSAGE
      Generic message of every delivery failure.
      See Also:
    • SECONDS_PER_MINUTE

      private static final long SECONDS_PER_MINUTE
      Seconds in a minute.
      See Also:
    • mailSender

      private final org.springframework.mail.javamail.JavaMailSender mailSender
      Sends the messages.
    • templateEngine

      private final org.thymeleaf.TemplateEngine templateEngine
      Renders the templates; private to this mailer.
    • messageSource

      private final org.springframework.context.MessageSource messageSource
      Source of the subjects.
    • properties

      private final MailProperties properties
      Sender, base URL and locale.
  • Constructor Details

    • ThymeleafOttMailer

      public ThymeleafOttMailer()
  • Method Details

    • deliverCode

      public void deliverCode(OttChannel channel, String destination, String code, Duration validFor)
      Description copied from interface: OttDelivery
      Delivers a numeric code.
      Specified by:
      deliverCode in interface OttDelivery
      Parameters:
      channel - channel to use.
      destination - e-mail address or E.164 phone number, according to the channel.
      code - the code in clear text.
      validFor - how long the code stays valid, shown to the user.
    • deliverLink

      public void deliverLink(OttChannel channel, String destination, URI link, Duration validFor)
      Description copied from interface: OttDelivery
      Delivers a magic link.
      Specified by:
      deliverLink in interface OttDelivery
      Parameters:
      channel - channel to use.
      destination - e-mail address or E.164 phone number, according to the channel.
      link - the link that opens the confirmation page.
      validFor - how long the link stays valid, shown to the user.
    • send

      private void send(String destination, String subjectKey, String template, String variable, String value, Duration validFor)
      Renders and sends one message.
      Parameters:
      destination - recipient address.
      subjectKey - message key of the subject.
      template - template name without extension.
      variable - name of the variable that holds the secret value.
      value - the code or link.
      validFor - validity shown to the user.
      Throws:
      OttDeliveryException - if the message cannot be rendered or delivered.
    • minutes

      private static long minutes(Duration validFor)
      Rounds a duration up to whole minutes, at least one.
      Parameters:
      validFor - the validity.
      Returns:
      the minutes to show.
    • requireEmail

      private static void requireEmail(OttChannel channel)
      Refuses a channel other than e-mail.
      Parameters:
      channel - requested channel.
      Throws:
      IllegalArgumentException - if the channel is not OttChannel.EMAIL.