TotpMfaEndpoint implements ApplicationEventPublisherAware { @RequestMapping(value = {"/register"}, method = RequestMethod.GET) public String generateQrUrl(Model model, @ModelAttribute("uaaMfaCredentials") UserGoogleMfaCredentials credentials) throws WriterException, IOException, UaaPrincipalIsNotInSession { UaaPrincipal uaaPrincipal = getSessionAuthPrincipal(); MfaProvider provider = getMfaProvider(); if (mfaCredentialsProvisioning.activeUserCredentialExists(uaaPrincipal.getId(), provider.getId())) { return "redirect:/login/mfa/verify"; } else { String url = mfaCredentialsProvisioning.getOtpAuthURL(provider.getConfig().getIssuer(), credentials, uaaPrincipal.getName()); model.addAttribute("qrurl", url); model.addAttribute("identity_zone", IdentityZoneHolder.get().getName()); return "mfa/qr_code"; } } TotpMfaEndpoint(
            final UserGoogleMfaCredentialsProvisioning mfaCredentialsProvisioning,
            final MfaProviderProvisioning mfaProviderProvisioning,
            final @Qualifier("mfaCompleteUrl") String mfaCompleteUrl,
            final UaaUserDatabase userDatabase,
            final @Qualifier("mfaGlobalUserLoginPolicy") CommonLoginPolicy mfaPolicy); @ModelAttribute("uaaMfaCredentials") UserGoogleMfaCredentials getUaaMfaCredentials(); @RequestMapping(value = {"/register"}, method = RequestMethod.GET) String generateQrUrl(Model model,
                                @ModelAttribute("uaaMfaCredentials") UserGoogleMfaCredentials credentials); @RequestMapping(value = {"/manual"}, method = RequestMethod.GET) String manualRegistration(
      Model model,
      @ModelAttribute("uaaMfaCredentials") UserGoogleMfaCredentials credentials
    ); @RequestMapping(value = {"/verify"}, method = RequestMethod.GET) ModelAndView totpAuthorize(Model model); @RequestMapping(value = {"/verify.do"}, method = RequestMethod.POST) ModelAndView validateCode(Model model,
                                     @RequestParam("code") String code,
                                     @ModelAttribute("uaaMfaCredentials") UserGoogleMfaCredentials credentials,
                                     HttpServletRequest request,
                                     SessionStatus sessionStatus); @ExceptionHandler(UaaPrincipalIsNotInSession.class) ModelAndView handleUaaPrincipalIsNotInSession(); @ExceptionHandler(AuthenticationPolicyRejectionException.class) ModelAndView handleMFALockedOut(); @Override void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);  }
@Test public void testGenerateQrUrl() throws Exception{ when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, null, null, null), null, null); when(userGoogleMfaCredentialsProvisioning.activeUserCredentialExists(userId, mfaProvider.getId())).thenReturn(false); when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider); IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName()); String returnView = endpoint.generateQrUrl(mock(Model.class), null); assertEquals("mfa/qr_code", returnView); }
@Test public void testGenerateQrUrlForNewUserRegistration() throws Exception{ when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, null, null, null), null, null); when(userGoogleMfaCredentialsProvisioning.activeUserCredentialExists(userId, mfaProvider.getId())).thenReturn(true); IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName()); when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider); String returnView = endpoint.generateQrUrl(mock(Model.class), null); assertEquals("redirect:/login/mfa/verify", returnView); }
@Test public void testGenerateQrUrlAfterMfaProviderSwitch() throws Exception{ when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, null, null, null), null, null); when(userGoogleMfaCredentialsProvisioning.activeUserCredentialExists(userId, mfaProvider.getId())).thenReturn(true); when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider); when(mfaProviderProvisioning.retrieveByName(otherMfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(otherMfaProvider); IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(otherMfaProvider.getName()); String returnView = endpoint.generateQrUrl(mock(Model.class), null); assertEquals("mfa/qr_code", returnView); }