@Test @WithMockUser(value = USERNAME, roles = ROLE_SU) void authenticateExceptionTest() { String secretKey = "secretKey"; String verificationCode = "123456"; TwoFactorAuthenticationToken authToken = new TwoFactorAuthenticationToken(verificationCode, null); when(twoFactorAuthenticationProvider.authenticate(authToken)) .thenThrow(new InvalidVerificationCodeException("Invalid verification code entered")); Model model = new ExtendedModelMap(); String viewTemplate = twoFactorAuthenticationController.authenticate(model, verificationCode, secretKey); assertEquals(secretKey, model.asMap().get(ATTRIBUTE_2FA_SECRET_KEY)); assertEquals(model.asMap().get(ERROR_MESSAGE_ATTRIBUTE), "Invalid verification code entered"); assertEquals("view-2fa-activation-modal", viewTemplate); }
@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); twoFactorAuthenticationService.enableForUser(); } 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; }
TwoFactorAuthenticationController { @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); twoFactorAuthenticationService.enableForUser(); } 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; } }
TwoFactorAuthenticationController { @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); twoFactorAuthenticationService.enableForUser(); } 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; } TwoFactorAuthenticationController(
      TwoFactorAuthenticationProvider authenticationProvider,
      TwoFactorAuthenticationService twoFactorAuthenticationService,
      RecoveryAuthenticationProvider recoveryAuthenticationProvider,
      OtpService otpService); }
TwoFactorAuthenticationController { @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); twoFactorAuthenticationService.enableForUser(); } 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; } TwoFactorAuthenticationController(
      TwoFactorAuthenticationProvider authenticationProvider,
      TwoFactorAuthenticationService twoFactorAuthenticationService,
      RecoveryAuthenticationProvider recoveryAuthenticationProvider,
      OtpService otpService); @GetMapping(TWO_FACTOR_CONFIGURED_URI) String configured(); @PostMapping(TWO_FACTOR_VALIDATION_URI) String validate(Model model, @RequestParam String verificationCode); @GetMapping(TWO_FACTOR_ACTIVATION_URI) String activation(Model model); @PostMapping(TWO_FACTOR_ACTIVATION_AUTHENTICATE_URI) String authenticate(
      Model model, @RequestParam String verificationCode, @RequestParam String secretKey); @PostMapping(TWO_FACTOR_RECOVER_URI) String recoverAccount(Model model, @RequestParam String recoveryCode); }
TwoFactorAuthenticationController { @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); twoFactorAuthenticationService.enableForUser(); } 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; } TwoFactorAuthenticationController(
      TwoFactorAuthenticationProvider authenticationProvider,
      TwoFactorAuthenticationService twoFactorAuthenticationService,
      RecoveryAuthenticationProvider recoveryAuthenticationProvider,
      OtpService otpService); @GetMapping(TWO_FACTOR_CONFIGURED_URI) String configured(); @PostMapping(TWO_FACTOR_VALIDATION_URI) String validate(Model model, @RequestParam String verificationCode); @GetMapping(TWO_FACTOR_ACTIVATION_URI) String activation(Model model); @PostMapping(TWO_FACTOR_ACTIVATION_AUTHENTICATE_URI) String authenticate(
      Model model, @RequestParam String verificationCode, @RequestParam String secretKey); @PostMapping(TWO_FACTOR_RECOVER_URI) String recoverAccount(Model model, @RequestParam String recoveryCode); static final String URI; static final String TWO_FACTOR_ACTIVATION_URI; static final String ATTRIBUTE_2FA_SECRET_KEY; static final String TWO_FACTOR_CONFIGURED_URI; }