Class EmailOtpPlugin
java.lang.Object
io.meeds.social.security.plugin.EmailOtpPlugin
- All Implemented Interfaces:
io.meeds.web.security.plugin.OtpPlugin
- Direct Known Subclasses:
AccountDeactivationEmailOtpPlugin, EmailChangeEmailOtpPlugin, UserAccountEmailOtpPlugin
@Service
public class EmailOtpPlugin
extends Object
implements io.meeds.web.security.plugin.OtpPlugin
Generates, caches and validates email OTP codes: the code is stored under a
purpose-bound cache key (
getCacheKey(String)) and the email is sent
through the common BrandedEmailSender with a per-plugin content
fragment and subject key.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanconsumeOtp(String userName, String otpCode) Removes the code from the cache when it is the one given, so that it never validates again.voidgenerateOtpCode(String userName) protected StringgetCacheKey(String userName) Key under which the OTP code of a user is stored, distinct by plugin so that a code generated for one purpose can never validate another onelongprotected StringgetName()The cache that holds the sent codes, undergetCacheKey(String), for the code's TTLlongprotected StringgetStoredCode(String otpCode, String recipientEmail) The value a sent code is stored as.protected booleanWhether a wrong code lifts the send lock of this method.protected voidSends an email with no code to the user, in their language, through the common branded layout: to the given address when there is one, else to the address of the user's accountprotected voidsendOtpCode(String userName, String recipientEmail) Sends a new code to the user, under the code key and the send lock of this method: to the given address when there is one, else to the address of the user's account.protected voidsendOtpCodeUnderLock(String userName, String recipientEmail) Sends a new code assendOtpCode(String, String)does, under the send lock the caller took withtakeSendLock(String).protected voidtakeSendLock(String userName) Takes the send lock of the user at once, before anything is checked or sent: the lock then holds for the send interval whatever the request answers, a refusal included.booleanvalidateOtp(String userName, String otpCode) A wrong code keeps the send lock, unless the method releases it (releasesSendLockOnWrongCode()): the user then may ask for a new code at once instead of waiting out the send interval, bounded by the attempts the validating service counts per code
-
Constructor Details
-
EmailOtpPlugin
public EmailOtpPlugin()
-
-
Method Details
-
getName
- Specified by:
getNamein interfaceio.meeds.web.security.plugin.OtpPlugin
-
canUse
- Specified by:
canUsein interfaceio.meeds.web.security.plugin.OtpPlugin
-
validateOtp
A wrong code keeps the send lock, unless the method releases it (releasesSendLockOnWrongCode()): the user then may ask for a new code at once instead of waiting out the send interval, bounded by the attempts the validating service counts per code- Specified by:
validateOtpin interfaceio.meeds.web.security.plugin.OtpPlugin
-
releasesSendLockOnWrongCode
protected boolean releasesSendLockOnWrongCode()Whether a wrong code lifts the send lock of this method. False here: the email change, API key and account deactivation flows keep waiting out the send interval after a wrong code -
consumeOtp
Removes the code from the cache when it is the one given, so that it never validates again. Only the request that removes it wins: a concurrent one finds it gone. Another code the user holds, such as one sent after this one was validated, stays: it is not removed, or put back when it arrived between the read and the removal.- Specified by:
consumeOtpin interfaceio.meeds.web.security.plugin.OtpPlugin
-
getCodeTtlMinutes
public long getCodeTtlMinutes()- Specified by:
getCodeTtlMinutesin interfaceio.meeds.web.security.plugin.OtpPlugin
-
getSendIntervalSeconds
public long getSendIntervalSeconds()- Specified by:
getSendIntervalSecondsin interfaceio.meeds.web.security.plugin.OtpPlugin
-
generateOtpCode
- Specified by:
generateOtpCodein interfaceio.meeds.web.security.plugin.OtpPlugin
-
sendOtpCode
Sends a new code to the user, under the code key and the send lock of this method: to the given address when there is one, else to the address of the user's account. The code is stored asgetStoredCode(String, String)answers, the value thatvalidateOtp(String, String)andconsumeOtp(String, String)compare.- Parameters:
userName- the user the code is forrecipientEmail- the address to send the code to, null for the address of the user's account
-
takeSendLock
Takes the send lock of the user at once, before anything is checked or sent: the lock then holds for the send interval whatever the request answers, a refusal included. The code is then sent bysendOtpCodeUnderLock(String, String).- Parameters:
userName- the user the code is for- Throws:
IllegalStateException- when the lock is already held
-
sendOtpCodeUnderLock
Sends a new code assendOtpCode(String, String)does, under the send lock the caller took withtakeSendLock(String). When the send fails, the lock is released and the code dropped: no code left, so the user may ask again at once.- Parameters:
userName- the user the code is forrecipientEmail- the address to send the code to, null for the address of the user's account
-
sendEmail
protected void sendEmail(String userName, String recipientEmail, String subjectKey, String templatePath) throws Exception Sends an email with no code to the user, in their language, through the common branded layout: to the given address when there is one, else to the address of the user's account- Parameters:
userName- the user the email is aboutrecipientEmail- the address to send it to, null for the address of the user's accountsubjectKey- the shared bundle key of its subjecttemplatePath- the path of its content fragment- Throws:
Exception- when the email cannot be built or sent
-
getStoredCode
The value a sent code is stored as. The code itself here: the email, API key, account deactivation and administrator session methods compare the typed code alone- Parameters:
otpCode- the generated coderecipientEmail- the address the code is sent to, null for the address of the user's account- Returns:
- the value to store
-
getCacheKey
-
getEmailSubjectKey
-
getOtpCache
The cache that holds the sent codes, undergetCacheKey(String), for the code's TTL
-