Search in sources :

Example 1 with RecoveryAuthenticationToken

use of org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken in project molgenis by molgenis.

the class AuthenticationAuthoritiesUpdaterImplTest method testUpdateAuthenticationRecoveryAuthenticationToken.

@Test
public void testUpdateAuthenticationRecoveryAuthenticationToken() {
    Object principal = mock(Object.class);
    Object credentials = mock(Object.class);
    String recoveryCode = "recoveryCode";
    RecoveryAuthenticationToken recoveryAuthenticationToken = new RecoveryAuthenticationToken(principal, credentials, emptyList(), recoveryCode);
    Authentication updatedAuthentication = authenticationAuthoritiesUpdaterImpl.updateAuthentication(recoveryAuthenticationToken, updatedAuthorities);
    assertEquals(updatedAuthentication, new RecoveryAuthenticationToken(principal, credentials, updatedAuthorities, recoveryCode));
}
Also used : RecoveryAuthenticationToken(org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken) Authentication(org.springframework.security.core.Authentication) Test(org.testng.annotations.Test)

Example 2 with RecoveryAuthenticationToken

use of org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken in project molgenis by molgenis.

the class TwoFactorAuthenticationController method recoverAccount.

@PostMapping(TWO_FACTOR_RECOVER_URI)
public String recoverAccount(Model model, @RequestParam String recoveryCode) {
    String redirectUrl = "redirect:/";
    try {
        RecoveryAuthenticationToken authToken = new RecoveryAuthenticationToken(recoveryCode);
        Authentication authentication = recoveryAuthenticationProvider.authenticate(authToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
    } catch (AuthenticationException e) {
        model.addAttribute(ATTRIBUTE_2FA_RECOVER_MODE, true);
        model.addAttribute(MolgenisLoginController.ERROR_MESSAGE_ATTRIBUTE, determineErrorMessage(e));
        redirectUrl = VIEW_2FA_CONFIGURED_MODAL;
    }
    return redirectUrl;
}
Also used : RecoveryAuthenticationToken(org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with RecoveryAuthenticationToken

use of org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken 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;
}
Also used : RecoveryAuthenticationToken(org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken) SessionAuthenticationException(org.springframework.security.web.authentication.session.SessionAuthenticationException) RunAsUserToken(org.springframework.security.access.intercept.RunAsUserToken) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SystemSecurityToken(org.molgenis.security.core.runas.SystemSecurityToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) RestAuthenticationToken(org.molgenis.security.token.RestAuthenticationToken) TwoFactorAuthenticationToken(org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken)

Aggregations

RecoveryAuthenticationToken (org.molgenis.security.twofactor.auth.RecoveryAuthenticationToken)3 Authentication (org.springframework.security.core.Authentication)3 SystemSecurityToken (org.molgenis.security.core.runas.SystemSecurityToken)1 RestAuthenticationToken (org.molgenis.security.token.RestAuthenticationToken)1 TwoFactorAuthenticationToken (org.molgenis.security.twofactor.auth.TwoFactorAuthenticationToken)1 RunAsUserToken (org.springframework.security.access.intercept.RunAsUserToken)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 SessionAuthenticationException (org.springframework.security.web.authentication.session.SessionAuthenticationException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 Test (org.testng.annotations.Test)1