Class EmailCodeService
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddressResolver(EmailAddressResolver addressResolver) Where each user's mail goes.The clock expiry is measured by; a test sets its own.voidStops askingusernamefor a mailed code.voidMakesusernamesign in with a mailed code from now on.static booleanisCodeShaped(String code) Whethercodeis written as a mailed code is: six digits.booleanWhetherusernamesigns in with a mailed code.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.resendSeconds(int resendSeconds) How long after one mail the next may be sent; 30 seconds unless set.booleanMailsusernamea new code, which replaces any that was waiting.The subject of the mail.The template the mail is rendered from, such asmail/sign-in-code, in place of the plain mail.validitySeconds(int validitySeconds) How long a code works; 600 seconds unless set.booleanWhethercodeis the one mailed tousername.
-
Constructor Details
-
EmailCodeService
-
-
Method Details
-
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
The clock expiry is measured by; a test sets its own. -
validitySeconds
How long a code works; 600 seconds unless set. -
resendSeconds
How long after one mail the next may be sent; 30 seconds unless set. -
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
The subject of the mail. -
template
The template the mail is rendered from, such asmail/sign-in-code, in place of the plain mail. -
isEnabled
Whetherusernamesigns in with a mailed code. -
enable
Makesusernamesign in with a mailed code from now on. Call it once the user's address is known to be theirs. -
disable
Stops askingusernamefor a mailed code. -
send
Mailsusernamea 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
-
isCodeShaped
Whethercodeis written as a mailed code is: six digits.
-