Search in sources :

Example 1 with ResetPasswordRequestEvent

use of org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent in project uaa by cloudfoundry.

the class UaaResetPasswordServiceTests method forgotPassword_PublishesResetPasswordRequestEvent.

@Test
void forgotPassword_PublishesResetPasswordRequestEvent() {
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    Authentication authentication = mock(Authentication.class);
    uaaResetPasswordService.setApplicationEventPublisher(publisher);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    ScimUser user = new ScimUser("user-id-001", "exampleUser", "firstName", "lastName");
    user.setPrimaryEmail("user@example.com");
    String zoneId = currentZoneId;
    when(scimUserProvisioning.retrieveByUsernameAndOriginAndZone(anyString(), anyString(), eq(zoneId))).thenReturn(Collections.singletonList(user));
    Timestamp expiresAt = new Timestamp(System.currentTimeMillis());
    when(codeStore.generateCode(anyString(), any(Timestamp.class), anyString(), anyString())).thenReturn(new ExpiringCode("code", expiresAt, "user-id-001", null));
    uaaResetPasswordService.forgotPassword("exampleUser", "", "");
    ArgumentCaptor<ResetPasswordRequestEvent> captor = ArgumentCaptor.forClass(ResetPasswordRequestEvent.class);
    verify(publisher).publishEvent(captor.capture());
    ResetPasswordRequestEvent event = captor.getValue();
    assertThat(event.getSource(), equalTo("exampleUser"));
    assertThat(event.getCode(), equalTo("code"));
    assertThat(event.getEmail(), equalTo("user@example.com"));
    assertThat(event.getAuthentication(), sameInstance(authentication));
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) Timestamp(java.sql.Timestamp) ResetPasswordRequestEvent(org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent) Test(org.junit.jupiter.api.Test)

Example 2 with ResetPasswordRequestEvent

use of org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent in project uaa by cloudfoundry.

the class AuditCheckMockMvcTests method passwordResetRequestEvent.

@Test
void passwordResetRequestEvent() throws Exception {
    String loginToken = testClient.getClientCredentialsOAuthAccessToken("login", "loginsecret", "oauth.login");
    resetAuditTestReceivers();
    MockHttpServletRequestBuilder changePasswordPost = post("/password_resets").accept(APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON).session(new MockHttpSession()).header("Authorization", "Bearer " + loginToken).content(testUser.getUserName());
    mockMvc.perform(changePasswordPost).andExpect(status().isCreated());
    assertNumberOfAuditEventsReceived(1);
    ResetPasswordRequestEvent event = (ResetPasswordRequestEvent) testListener.getLatestEvent();
    assertEquals(testUser.getUserName(), event.getAuditEvent().getPrincipalId());
    assertEquals(testUser.getPrimaryEmail(), event.getAuditEvent().getData());
    assertTrue(event.getAuditEvent().getOrigin().contains("sessionId=<SESSION>"));
    assertLogMessageWithSession(testLogger.getLatestMessage(), PasswordResetRequest, testUser.getUserName(), testUser.getPrimaryEmail());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockHttpSession(org.springframework.mock.web.MockHttpSession) ResetPasswordRequestEvent(org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent) Test(org.junit.jupiter.api.Test)

Example 3 with ResetPasswordRequestEvent

use of org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent in project uaa by cloudfoundry.

the class UaaResetPasswordService method forgotPassword.

@Override
public ForgotPasswordInfo forgotPassword(String username, String clientId, String redirectUri) {
    List<ScimUser> results = scimUserProvisioning.retrieveByUsernameAndOriginAndZone(username, OriginKeys.UAA, identityZoneManager.getCurrentIdentityZoneId());
    if (results.isEmpty()) {
        results = scimUserProvisioning.retrieveByUsernameAndZone(username, identityZoneManager.getCurrentIdentityZoneId());
        if (results.isEmpty()) {
            throw new NotFoundException();
        } else {
            throw new ConflictException(results.get(0).getId(), results.get(0).getPrimaryEmail());
        }
    }
    ScimUser scimUser = results.get(0);
    PasswordChange change = new PasswordChange(scimUser.getId(), scimUser.getUserName(), scimUser.getPasswordLastModified(), clientId, redirectUri);
    String intent = FORGOT_PASSWORD_INTENT_PREFIX + scimUser.getId();
    expiringCodeStore.expireByIntent(intent, identityZoneManager.getCurrentIdentityZoneId());
    ExpiringCode code = expiringCodeStore.generateCode(JsonUtils.writeValueAsString(change), new Timestamp(System.currentTimeMillis() + PASSWORD_RESET_LIFETIME), intent, identityZoneManager.getCurrentIdentityZoneId());
    String email = scimUser.getPrimaryEmail();
    if (email == null) {
        email = scimUser.getUserName();
    }
    publish(new ResetPasswordRequestEvent(username, email, code.getCode(), SecurityContextHolder.getContext().getAuthentication(), identityZoneManager.getCurrentIdentityZoneId()));
    return new ForgotPasswordInfo(scimUser.getId(), email, code);
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) PasswordChange(org.cloudfoundry.identity.uaa.scim.endpoints.PasswordChange) Timestamp(java.sql.Timestamp) ResetPasswordRequestEvent(org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent)

Aggregations

ResetPasswordRequestEvent (org.cloudfoundry.identity.uaa.account.event.ResetPasswordRequestEvent)3 Timestamp (java.sql.Timestamp)2 ExpiringCode (org.cloudfoundry.identity.uaa.codestore.ExpiringCode)2 ScimUser (org.cloudfoundry.identity.uaa.scim.ScimUser)2 Test (org.junit.jupiter.api.Test)2 PasswordChange (org.cloudfoundry.identity.uaa.scim.endpoints.PasswordChange)1 MockAuthentication (org.cloudfoundry.identity.uaa.test.MockAuthentication)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Mockito.anyString (org.mockito.Mockito.anyString)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1 Authentication (org.springframework.security.core.Authentication)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1