@Override
public ForgotPasswordInfo forgotPassword(String username, String clientId, String redirectUri) {
String jsonUsername = JsonUtils.writeValueAsString(username);
List<ScimUser> results = scimUserProvisioning.query("userName eq " + jsonUsername + " and origin eq \"" + OriginKeys.UAA + "\"", IdentityZoneHolder.get().getId());
if (results.isEmpty()) {
results = scimUserProvisioning.query("userName eq " + jsonUsername, IdentityZoneHolder.get().getId());
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, IdentityZoneHolder.get().getId());
ExpiringCode code = expiringCodeStore.generateCode(JsonUtils.writeValueAsString(change), new Timestamp(System.currentTimeMillis() + PASSWORD_RESET_LIFETIME), intent, IdentityZoneHolder.get().getId());
String email = scimUser.getPrimaryEmail();
if (email == null) {
email = scimUser.getUserName();
}
publish(new ResetPasswordRequestEvent(username, email, code.getCode(), SecurityContextHolder.getContext().getAuthentication()));
return new ForgotPasswordInfo(scimUser.getId(), email, code);
}