Class EmailVerificationService

java.lang.Object
com.codename1.backend.security.mail.EmailVerificationService
All Implemented Interfaces:
EmailAddressResolver

public final class EmailVerificationService extends Object implements EmailAddressResolver

Proves that an address is a user's, by mailing it a link.

verification.send(username, "ada@example.com", "https://example.com/verify-email");
// ... the link is opened
String username = verification.confirm(token);     // null unless it is good

http.emailVerification() adds the address the link opens. An address is proved once and stays so until send(String, String, String) is called with another one.

The service is an EmailAddressResolver too: it answers a user's proved address, so a password reset or a sign-in code goes to an address the user has shown to be theirs.

http.authorizeHttpRequests(a -> a
        .requestMatchers("/billing/**").access(verification.verified())
        .anyRequest().authenticated());

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

  • Field Details

  • Constructor Details

  • Method Details

    • clock

      public EmailVerificationService clock(Clock clock)
      The clock the moment of proof is read from; a test sets its own.
    • validityMinutes

      public EmailVerificationService validityMinutes(int validityMinutes)
      How long a link works; a day unless set.
    • subject

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

      public EmailVerificationService template(String view)
      The template the mail is rendered from, such as mail/verify-email, in place of the plain mail.
    • send

      public void send(String username, String email, String confirmUrl) throws IOException

      Records email as the address username claims and mails it a link to confirmUrl with a token parameter added. Whatever address the user had proved is no longer theirs until this one is.

      Parameters
      • confirmUrl: the whole address the link opens, https://example.com/verify-email
      Throws
      • IllegalArgumentException: email is not an address
      • IOException: the mail could not be handed over
      Throws:
      IOException
    • confirm

      public String confirm(String token)
      Takes the token of a link. Answers the user whose address is now proved, or null when the token is not good.
    • isVerified

      public boolean isVerified(String username)
      Whether username has proved their address.
    • getVerifiedEmail

      public String getVerifiedEmail(String username)
      The address username has proved, or null.
    • forget

      public void forget(String username)
      Forgets username's address and any link mailed to it.
    • emailFor

      public String emailFor(String username)
      The address username has proved; failing that, the user name where it is an address.
      Specified by:
      emailFor in interface EmailAddressResolver
    • verified

      Grants a request to a signed-in user who has proved their address.