Search in sources :

Example 1 with UaaPrincipal

use of org.cloudfoundry.identity.uaa.authentication.UaaPrincipal in project uaa by cloudfoundry.

the class TotpMfaEndpointTest method testGenerateQrUrlAfterMfaProviderSwitch.

@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);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) Model(org.springframework.ui.Model) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with UaaPrincipal

use of org.cloudfoundry.identity.uaa.authentication.UaaPrincipal in project uaa by cloudfoundry.

the class TotpMfaEndpointTest method testEmptyOTP.

@Test
public void testEmptyOTP() throws Exception {
    when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, "uaa", null, null), null, null);
    when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider);
    when(userDb.retrieveUserByName("Marissa", "uaa")).thenReturn(new UaaUser(new UaaUserPrototype().withUsername("Marissa").withOrigin("uaa").withId("1234").withEmail("marissa@example.com")));
    IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName());
    SessionStatus sessionStatus = mock(SessionStatus.class);
    ModelAndView returnView = endpoint.validateCode(mock(Model.class), "", mock(UserGoogleMfaCredentials.class), new MockHttpServletRequest(), sessionStatus);
    assertEquals("mfa/enter_code", returnView.getViewName());
    verifyZeroInteractions(sessionStatus);
    verifyMfaEvent(MfaAuthenticationFailureEvent.class);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) SessionStatus(org.springframework.web.bind.support.SessionStatus) UserGoogleMfaCredentials(org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials) ModelAndView(org.springframework.web.servlet.ModelAndView) Model(org.springframework.ui.Model) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 3 with UaaPrincipal

use of org.cloudfoundry.identity.uaa.authentication.UaaPrincipal in project uaa by cloudfoundry.

the class TotpMfaEndpointTest method testValidOTPActivatesUser.

@Test
public void testValidOTPActivatesUser() throws Exception {
    int code = 1234;
    when(userGoogleMfaCredentialsProvisioning.isValidCode(ArgumentMatchers.any(UserGoogleMfaCredentials.class), eq(code))).thenReturn(true);
    when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, "uaa", null, null), null, null);
    when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider);
    when(userDb.retrieveUserByName("Marissa", "uaa")).thenReturn(new UaaUser(new UaaUserPrototype().withUsername("Marissa").withOrigin("uaa").withId("1234").withEmail("marissa@example.com")));
    IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName());
    SessionStatus sessionStatus = mock(SessionStatus.class);
    endpoint.validateCode(mock(Model.class), Integer.toString(code), mock(UserGoogleMfaCredentials.class), new MockHttpServletRequest(), sessionStatus);
    verify(userGoogleMfaCredentialsProvisioning).saveUserCredentials(ArgumentMatchers.any(UserGoogleMfaCredentials.class));
    verify(sessionStatus).setComplete();
    verifyMfaEvent(MfaAuthenticationSuccessEvent.class);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserGoogleMfaCredentials(org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) SessionStatus(org.springframework.web.bind.support.SessionStatus) Model(org.springframework.ui.Model) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 4 with UaaPrincipal

use of org.cloudfoundry.identity.uaa.authentication.UaaPrincipal in project uaa by cloudfoundry.

the class TotpMfaEndpointTest method testValidOTPReturnsErrorWhenLockedOut.

@Test
public void testValidOTPReturnsErrorWhenLockedOut() throws Exception {
    exception.expect(AuthenticationPolicyRejectionException.class);
    int code = 1234;
    when(mockMfaPolicy.isAllowed(anyString())).thenReturn(new LoginPolicy.Result(false, 0));
    when(userGoogleMfaCredentialsProvisioning.isValidCode(ArgumentMatchers.any(UserGoogleMfaCredentials.class), eq(code))).thenReturn(true);
    when(uaaAuthentication.getPrincipal()).thenReturn(new UaaPrincipal(userId, "Marissa", null, "uaa", null, null), null, null);
    when(mfaProviderProvisioning.retrieveByName(mfaProvider.getName(), IdentityZoneHolder.get().getId())).thenReturn(mfaProvider);
    when(userDb.retrieveUserByName("Marissa", "uaa")).thenReturn(new UaaUser(new UaaUserPrototype().withUsername("Marissa").withOrigin("uaa").withId("1234").withEmail("marissa@example.com")));
    IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName());
    SessionStatus sessionStatus = mock(SessionStatus.class);
    endpoint.validateCode(mock(Model.class), Integer.toString(code), mock(UserGoogleMfaCredentials.class), new MockHttpServletRequest(), sessionStatus);
    verifyZeroInteractions(sessionStatus);
    verifyMfaEvent(MfaAuthenticationFailureEvent.class);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserGoogleMfaCredentials(org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials) UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) SessionStatus(org.springframework.web.bind.support.SessionStatus) Model(org.springframework.ui.Model) CommonLoginPolicy(org.cloudfoundry.identity.uaa.authentication.manager.CommonLoginPolicy) LoginPolicy(org.cloudfoundry.identity.uaa.authentication.manager.LoginPolicy) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) Test(org.junit.Test)

Example 5 with UaaPrincipal

use of org.cloudfoundry.identity.uaa.authentication.UaaPrincipal in project uaa by cloudfoundry.

the class TotpMfaEndpointTest method testManualRegistrationExistingCredential.

@Test
public void testManualRegistrationExistingCredential() 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);
    IdentityZoneHolder.get().getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName());
    String returnValue = endpoint.manualRegistration(mock(Model.class), mock(UserGoogleMfaCredentials.class));
    assertEquals("redirect:/login/mfa/verify", returnValue);
}
Also used : UaaPrincipal(org.cloudfoundry.identity.uaa.authentication.UaaPrincipal) UserGoogleMfaCredentials(org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials) Model(org.springframework.ui.Model) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

UaaPrincipal (org.cloudfoundry.identity.uaa.authentication.UaaPrincipal)103 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)47 UaaUser (org.cloudfoundry.identity.uaa.user.UaaUser)28 Test (org.junit.jupiter.api.Test)28 Test (org.junit.Test)22 Authentication (org.springframework.security.core.Authentication)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)18 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)16 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)15 UaaAuthenticationDetails (org.cloudfoundry.identity.uaa.authentication.UaaAuthenticationDetails)14 Model (org.springframework.ui.Model)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)11 SecurityContext (org.springframework.security.core.context.SecurityContext)11 UserGoogleMfaCredentials (org.cloudfoundry.identity.uaa.mfa.UserGoogleMfaCredentials)10 Matchers.containsString (org.hamcrest.Matchers.containsString)10 BeforeEach (org.junit.jupiter.api.BeforeEach)10 ScimUser (org.cloudfoundry.identity.uaa.scim.ScimUser)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 HashMap (java.util.HashMap)8