use of io.gravitee.am.gateway.handler.root.service.response.ResetPasswordResponse in project gravitee-access-management by gravitee-io.
the class ResetPasswordSubmissionEndpoint method handle.
@Override
public void handle(RoutingContext context) {
// retrieve the client in context
Client client = context.get(ConstantKeys.CLIENT_CONTEXT_KEY);
// retrieve the user in context
User user = context.get(ConstantKeys.USER_CONTEXT_KEY);
// set user password entered during reset password process
String password = context.request().getParam(ConstantKeys.PASSWORD_PARAM_KEY);
user.setPassword(password);
// reset password
resetPassword(client, user, getAuthenticatedUser(context), h -> {
// prepare response
MultiMap queryParams = RequestUtils.getCleanedQueryParams(context.request());
// if failure, return to the reset password page with an error
if (h.failed()) {
logger.error("An error occurs while ending user reset password process", h.cause());
queryParams.set(ConstantKeys.ERROR_PARAM_KEY, "reset_password_failed");
redirectToPage(context, queryParams, h.cause());
return;
}
// handle response
ResetPasswordResponse resetPasswordResponse = h.result();
// if auto login option is enabled add the user to the session
if (resetPasswordResponse.isAutoLogin()) {
context.setUser(io.vertx.reactivex.ext.auth.User.newInstance(new io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User(resetPasswordResponse.getUser())));
}
// no redirect uri has been set, redirect to the default page
if (resetPasswordResponse.getRedirectUri() == null || resetPasswordResponse.getRedirectUri().isEmpty()) {
queryParams.set(ConstantKeys.SUCCESS_PARAM_KEY, "reset_password_completed");
redirectToPage(context, queryParams);
return;
}
// else, redirect to the custom redirect_uri
context.response().putHeader(HttpHeaders.LOCATION, resetPasswordResponse.getRedirectUri()).setStatusCode(302).end();
});
}
use of io.gravitee.am.gateway.handler.root.service.response.ResetPasswordResponse in project gravitee-access-management by gravitee-io.
the class ResetPasswordSubmissionEndpointTest method shouldInvokeResetPasswordEndpoint.
@Test
public void shouldInvokeResetPasswordEndpoint() throws Exception {
Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
client.setRedirectUris(Collections.singletonList("http://localhost:9999/callback"));
User user = new User();
router.route().order(-1).handler(routingContext -> {
routingContext.put("client", client);
routingContext.put("user", user);
routingContext.next();
});
when(userService.resetPassword(eq(client), eq(user), any())).thenReturn(Single.just(new ResetPasswordResponse()));
testRequest(HttpMethod.POST, "/resetPassword?client_id=client-id", this::postPassword, resp -> {
String location = resp.headers().get("location");
assertNotNull(location);
assertTrue(location.endsWith("/resetPassword?client_id=client-id&success=reset_password_completed"));
}, HttpStatusCode.FOUND_302, "Found", null);
}
use of io.gravitee.am.gateway.handler.root.service.response.ResetPasswordResponse in project gravitee-access-management by gravitee-io.
the class ResetPasswordSubmissionEndpointTest method shouldInvokeResetPasswordEndpoint_redirectUri.
@Test
public void shouldInvokeResetPasswordEndpoint_redirectUri() throws Exception {
Client client = new Client();
client.setId("client-id");
client.setClientId("client-id");
client.setRedirectUris(Collections.singletonList("http://localhost:9999/callback"));
User user = new User();
ResetPasswordResponse resetPasswordResponse = new ResetPasswordResponse();
resetPasswordResponse.setAutoLogin(true);
resetPasswordResponse.setUser(user);
resetPasswordResponse.setRedirectUri("http://custom_uri");
router.route().order(-1).handler(routingContext -> {
routingContext.put("client", client);
routingContext.put("user", user);
routingContext.next();
});
when(userService.resetPassword(eq(client), eq(user), any())).thenReturn(Single.just(resetPasswordResponse));
testRequest(HttpMethod.POST, "/resetPassword?client_id=client-id", this::postPassword, resp -> {
String location = resp.headers().get("location");
assertNotNull(location);
assertEquals("http://custom_uri", location);
}, HttpStatusCode.FOUND_302, "Found", null);
}
use of io.gravitee.am.gateway.handler.root.service.response.ResetPasswordResponse in project gravitee-access-management by gravitee-io.
the class UserServiceImpl method resetPassword.
@Override
public Single<ResetPasswordResponse> resetPassword(Client client, User user, io.gravitee.am.identityprovider.api.User principal) {
// get account settings
final AccountSettings accountSettings = AccountSettings.getInstance(domain, client);
// if user registration is not completed and force registration option is disabled throw invalid account exception
if (user.isInactive() && !forceUserRegistration(domain, client)) {
return Single.error(new AccountInactiveException("User needs to complete the activation process"));
}
// only idp manage password, find user idp and update its password
return identityProviderManager.getUserProvider(user.getSource()).switchIfEmpty(Maybe.error(new UserProviderNotFoundException(user.getSource()))).flatMapSingle(userProvider -> userProvider.findByUsername(user.getUsername()).switchIfEmpty(Maybe.error(new UserNotFoundException(user.getUsername()))).flatMapSingle(idpUser -> {
// set password
((DefaultUser) idpUser).setCredentials(user.getPassword());
return userProvider.update(idpUser.getId(), idpUser);
}).onErrorResumeNext(ex -> {
if (ex instanceof UserNotFoundException) {
// idp user not found, create its account
return userProvider.create(convert(user));
}
return Single.error(ex);
})).flatMap(idpUser -> {
// if user was in pre-registration mode, end the registration process
if (user.isPreRegistration()) {
user.setRegistrationCompleted(true);
user.setEnabled(true);
}
user.setAccountNonLocked(true);
user.setAccountLockedAt(null);
user.setAccountLockedUntil(null);
user.setPassword(null);
user.setExternalId(idpUser.getId());
user.setLastPasswordReset(new Date());
user.setUpdatedAt(new Date());
// additional information
extractAdditionalInformation(user, idpUser.getAdditionalInformation());
// set login information
if (accountSettings != null && accountSettings.isAutoLoginAfterResetPassword()) {
user.setLoggedAt(new Date());
user.setLoginsCount(user.getLoginsCount() + 1);
}
return userService.update(user);
}).flatMap(user1 -> {
LoginAttemptCriteria criteria = new LoginAttemptCriteria.Builder().domain(user1.getReferenceId()).client(user1.getClient()).username(user1.getUsername()).build();
return loginAttemptService.reset(criteria).andThen(Single.just(user1));
}).flatMap(user1 -> {
if (accountSettings != null && accountSettings.isDeletePasswordlessDevicesAfterResetPassword()) {
return credentialService.deleteByUserId(user1.getReferenceType(), user1.getReferenceId(), user1.getId()).andThen(Single.just(user1));
}
return Single.just(user1);
}).flatMap(userService::enhance).map(user1 -> new ResetPasswordResponse(user1, accountSettings != null ? accountSettings.getRedirectUriAfterResetPassword() : null, accountSettings != null ? accountSettings.isAutoLoginAfterResetPassword() : false)).doOnSuccess(response -> auditService.report(AuditBuilder.builder(UserAuditBuilder.class).domain(domain.getId()).client(client).principal(principal).type(EventType.USER_PASSWORD_RESET).user(user))).doOnError(throwable -> auditService.report(AuditBuilder.builder(UserAuditBuilder.class).domain(domain.getId()).client(client).principal(principal).type(EventType.USER_PASSWORD_RESET).user(user).throwable(throwable)));
}
Aggregations