-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MFA): Added support for custom verification checks
Added support for custom MFA verification checks, such as receiving MFA codes using e-mail and SMS. These usually are valid for a longer period, so the default TOTP validator cannot be used. The current version of rest-secure did not allow such checks, since if it detected a `verificationCode` and the user's `isMfaConfigured()` returned true, it would throw a bad credentials exception. Since these checks are usually in some part dependent on the implementing application, I've chosen not to implement the checks in the library, but made it easy to extend the default flow by adding support for custom checks.
- Loading branch information
Showing
9 changed files
with
345 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ain/java/nl/_42/restsecure/autoconfigure/authentication/mfa/MfaTotpVerificationCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nl._42.restsecure.autoconfigure.authentication.mfa; | ||
|
||
import nl._42.restsecure.autoconfigure.authentication.RegisteredUser; | ||
import nl._42.restsecure.autoconfigure.errorhandling.DefaultLoginAuthenticationExceptionHandler; | ||
|
||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
public class MfaTotpVerificationCheck implements MfaVerificationCheck { | ||
|
||
private final MfaValidationService mfaValidationService; | ||
|
||
public MfaTotpVerificationCheck(MfaValidationService mfaValidationService) { | ||
this.mfaValidationService = mfaValidationService; | ||
} | ||
|
||
@Override | ||
public boolean validate(RegisteredUser user, MfaAuthenticationToken authenticationToken) throws AuthenticationException { | ||
// If no pre-authorized code assigned, validate the code supplied against the currently-valid TOTP code. | ||
if (!mfaValidationService.verifyMfaCode(user.getMfaSecretKey(), authenticationToken.getVerificationCode())) { | ||
throw new BadCredentialsException(DefaultLoginAuthenticationExceptionHandler.SERVER_LOGIN_FAILED_ERROR); | ||
} | ||
|
||
return true; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/nl/_42/restsecure/autoconfigure/authentication/mfa/MfaVerificationCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package nl._42.restsecure.autoconfigure.authentication.mfa; | ||
|
||
import nl._42.restsecure.autoconfigure.authentication.RegisteredUser; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
|
||
public interface MfaVerificationCheck { | ||
|
||
/** | ||
* Validates the MFA Authentication credentials for the given RegisteredUser. | ||
* If the credentials are valid, return true. The user will be logged in and no further checks will take place. | ||
* If this check is not applicable for this user, return false. The next check will then be tried. | ||
* If the credentials are not valid (but this check *is* applicable for this user), throw an AuthenticationException. | ||
* @param user User that is trying to log in. | ||
* @param authenticationToken Supplied authentication credentials (username, password, MFA token) | ||
* @return Returns true if this authentication is valid | ||
* @throws AuthenticationException if the supplied credentials are not valid | ||
*/ | ||
boolean validate(RegisteredUser user, MfaAuthenticationToken authenticationToken) throws AuthenticationException; | ||
} |
Oops, something went wrong.