use of org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken in project molgenis by molgenis.
the class TwoFactorAuthenticationController method authenticate.
@PostMapping(TWO_FACTOR_ACTIVATION_AUTHENTICATE_URI)
public String authenticate(Model model, @RequestParam String verificationCode, @RequestParam String secretKey) {
String redirectUrl = "redirect:/menu/main/useraccount?showCodes=true#security";
try {
TwoFactorAuthenticationToken authToken = new TwoFactorAuthenticationToken(verificationCode, secretKey);
Authentication authentication = authenticationProvider.authenticate(authToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
} catch (AuthenticationException err) {
model.addAttribute(ATTRIBUTE_2FA_SECRET_KEY, secretKey);
model.addAttribute(ATTRIBUTE_2FA_AUTHENTICATOR_URI, otpService.getAuthenticatorURI(secretKey));
model.addAttribute(MolgenisLoginController.ERROR_MESSAGE_ATTRIBUTE, determineErrorMessage(err));
redirectUrl = VIEW_2FA_ACTIVATION_MODAL;
}
return redirectUrl;
}
use of org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken in project molgenis by molgenis.
the class AuthenticationAuthoritiesUpdaterImplTest method testUpdateAuthenticationTwoFactorAuthenticationToken.
@Test
public void testUpdateAuthenticationTwoFactorAuthenticationToken() {
Object principal = mock(Object.class);
Object credentials = mock(Object.class);
String verificationCode = "dummyVerificationCode";
String secretKey = "secretKey";
TwoFactorAuthenticationToken twoFactorAuthenticationToken = new TwoFactorAuthenticationToken(principal, credentials, Collections.emptyList(), verificationCode, secretKey);
Authentication updatedAuthentication = authenticationAuthoritiesUpdaterImpl.updateAuthentication(twoFactorAuthenticationToken, updatedAuthorities);
assertEquals(updatedAuthentication, new TwoFactorAuthenticationToken(principal, credentials, updatedAuthorities, verificationCode, secretKey));
}
use of org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken in project molgenis by molgenis.
the class TwoFactorAuthenticationController method validate.
@PostMapping(TWO_FACTOR_VALIDATION_URI)
public String validate(Model model, @RequestParam String verificationCode) {
String redirectUri = "redirect:/";
try {
TwoFactorAuthenticationToken authToken = new TwoFactorAuthenticationToken(verificationCode, null);
Authentication authentication = authenticationProvider.authenticate(authToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
} catch (AuthenticationException err) {
model.addAttribute(MolgenisLoginController.ERROR_MESSAGE_ATTRIBUTE, determineErrorMessage(err));
redirectUri = VIEW_2FA_CONFIGURED_MODAL;
}
return redirectUri;
}
use of org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken in project molgenis by molgenis.
the class AuthenticationAuthoritiesUpdaterImpl method updateAuthentication.
@Override
public Authentication updateAuthentication(Authentication authentication, List<GrantedAuthority> updatedAuthorities) {
Authentication newAuthentication;
if (authentication instanceof TwoFactorAuthenticationToken) {
TwoFactorAuthenticationToken twoFactorAuthenticationToken = (TwoFactorAuthenticationToken) authentication;
newAuthentication = new TwoFactorAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), updatedAuthorities, twoFactorAuthenticationToken.getVerificationCode(), twoFactorAuthenticationToken.getSecretKey());
} else if (authentication instanceof SystemSecurityToken) {
newAuthentication = authentication;
} else if (authentication instanceof RestAuthenticationToken) {
RestAuthenticationToken restAuthenticationToken = (RestAuthenticationToken) authentication;
newAuthentication = new RestAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), updatedAuthorities, restAuthenticationToken.getToken());
} else if (authentication instanceof RecoveryAuthenticationToken) {
RecoveryAuthenticationToken recoveryAuthenticationToken = (RecoveryAuthenticationToken) authentication;
newAuthentication = new RecoveryAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), updatedAuthorities, recoveryAuthenticationToken.getRecoveryCode());
} else if (authentication instanceof UsernamePasswordAuthenticationToken) {
newAuthentication = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), updatedAuthorities);
} else if (authentication instanceof RunAsUserToken) {
RunAsUserToken runAsUserToken = (RunAsUserToken) authentication;
newAuthentication = new RunAsUserTokenDecorator(runAsUserToken, updatedAuthorities);
} else if (authentication instanceof AnonymousAuthenticationToken) {
AnonymousAuthenticationToken anonymousAuthenticationToken = (AnonymousAuthenticationToken) authentication;
newAuthentication = new AnonymousAuthenticationTokenDecorator(anonymousAuthenticationToken, updatedAuthorities);
} else {
throw new SessionAuthenticationException(format("Unknown authentication type '%s'", authentication.getClass().getSimpleName()));
}
return newAuthentication;
}
Aggregations