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));
}
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());
}
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);
}
Aggregations