Class EmailCodeService

java.lang.Object
com.codename1.backend.security.mfa.EmailCodeService

public final class EmailCodeService extends Object

A code mailed to the user as the second step of signing in.

@Bean
EmailCodeService emailCodes(DataSource dataSource, MailSender mail) {
    return new EmailCodeService(new JdbcEmailCodeRepository(dataSource), mail);
}

With the bean declared, http.mfa(...) asks every user for whom enable(String) was called for a code after their password, and mails it. A user who has an authenticator app too is asked for either, and the mail is sent when they ask for it.

A code is six digits, works once and for ten minutes, and is given up on after five wrong tries: those five in a million are what protect it, so the count is kept with the code and is the same count on every server. Asking for another code does not start the count again: it is carried to the new code until a code is entered right or runs out, and once the five are used no code is mailed until then: the user waits out what is left of the ten minutes, and asking again does not make the wait longer. Only a hash of the code is kept. Another is not mailed within half a minute of the last.

A mailed code is as strong as the mailbox it goes to, and weaker than an authenticator app or a passkey: offer it as the easy option, not the only one.

The mail is plain by default. Name a template to send the application's own: it is rendered with code, username and minutes.

  • Constructor Details

  • Method Details

    • addressResolver

      public EmailCodeService addressResolver(EmailAddressResolver addressResolver)
      Where each user's mail goes. Without one, a user name that is an address is used and other users are sent nothing.
    • clock

      public EmailCodeService clock(Clock clock)
      The clock expiry is measured by; a test sets its own.
    • validitySeconds

      public EmailCodeService validitySeconds(int validitySeconds)
      How long a code works; 600 seconds unless set.
    • resendSeconds

      public EmailCodeService resendSeconds(int resendSeconds)
      How long after one mail the next may be sent; 30 seconds unless set.
    • maxAttempts

      public EmailCodeService maxAttempts(int maxAttempts)
      How many tries a user is given, over however many codes, before one is entered right or the last runs out; 5 unless set.
    • subject

      public EmailCodeService subject(String subject)
      The subject of the mail.
    • template

      public EmailCodeService template(String view)
      The template the mail is rendered from, such as mail/sign-in-code, in place of the plain mail.
    • isEnabled

      public boolean isEnabled(String username)
      Whether username signs in with a mailed code.
    • enable

      public void enable(String username)
      Makes username sign in with a mailed code from now on. Call it once the user's address is known to be theirs.
    • disable

      public void disable(String username)
      Stops asking username for a mailed code.
    • send

      public boolean send(String username)
      Mails username a new code, which replaces any that was waiting. False, and nothing sent, when the user does not sign in with a mailed code, the last was mailed too recently, the tries were used up and the code they were used on has not run out, the user has no address, or the mail could not be handed over.
    • verify

      public boolean verify(String username, String code)
      Whether code is the one mailed to username. A right code is used up; a wrong one is counted against the code that is waiting.
    • isCodeShaped

      public static boolean isCodeShaped(String code)
      Whether code is written as a mailed code is: six digits.