Search in sources :

Example 1 with EnforceUserIdentityException

use of io.gravitee.am.service.exception.EnforceUserIdentityException in project gravitee-access-management by gravitee-io.

the class ForgotPasswordSubmissionEndpoint method handle.

@Override
public void handle(RoutingContext context) {
    final String email = context.request().getParam(ConstantKeys.EMAIL_PARAM_KEY);
    final String username = context.request().getParam(ConstantKeys.USERNAME_PARAM_KEY);
    final Client client = context.get(ConstantKeys.CLIENT_CONTEXT_KEY);
    MultiMap queryParams = RequestUtils.getCleanedQueryParams(context.request());
    AccountSettings settings = AccountSettings.getInstance(domain, client);
    final ForgotPasswordParameters parameters = new ForgotPasswordParameters(email, username, settings != null && settings.isResetPasswordCustomForm(), settings != null && settings.isResetPasswordConfirmIdentity());
    userService.forgotPassword(parameters, client, getAuthenticatedUser(context)).subscribe(() -> {
        queryParams.set(ConstantKeys.SUCCESS_PARAM_KEY, "forgot_password_completed");
        redirectToPage(context, queryParams);
    }, error -> {
        // the actual error continue to be stored in the audit logs
        if (error instanceof UserNotFoundException || error instanceof AccountStatusException) {
            queryParams.set(ConstantKeys.SUCCESS_PARAM_KEY, "forgot_password_completed");
            redirectToPage(context, queryParams);
        } else if (error instanceof EnforceUserIdentityException) {
            if (settings.isResetPasswordConfirmIdentity()) {
                queryParams.set(ConstantKeys.WARNING_PARAM_KEY, FORGOT_PASSWORD_CONFIRM);
            } else {
                queryParams.set(ConstantKeys.SUCCESS_PARAM_KEY, "forgot_password_completed");
            }
            redirectToPage(context, queryParams);
        } else {
            queryParams.set(ConstantKeys.ERROR_PARAM_KEY, "forgot_password_failed");
            redirectToPage(context, queryParams, error);
        }
    });
}
Also used : AccountSettings(io.gravitee.am.model.account.AccountSettings) UserNotFoundException(io.gravitee.am.service.exception.UserNotFoundException) AccountStatusException(io.gravitee.am.common.exception.authentication.AccountStatusException) MultiMap(io.vertx.reactivex.core.MultiMap) EnforceUserIdentityException(io.gravitee.am.service.exception.EnforceUserIdentityException) Client(io.gravitee.am.model.oidc.Client) ForgotPasswordParameters(io.gravitee.am.gateway.handler.root.service.user.model.ForgotPasswordParameters)

Example 2 with EnforceUserIdentityException

use of io.gravitee.am.service.exception.EnforceUserIdentityException in project gravitee-access-management by gravitee-io.

the class ForgotPasswordSubmissionEndpointTest method shouldCompleteWithSuccessWhen_EnforceIdentityException_ConfirmIdentityNotEnabled.

@Test
public void shouldCompleteWithSuccessWhen_EnforceIdentityException_ConfirmIdentityNotEnabled() throws Exception {
    Client client = new Client();
    client.setId("client-id");
    client.setClientId("client-id");
    router.route().order(-1).handler(routingContext -> {
        routingContext.put("client", client);
        routingContext.next();
    });
    when(accountSettings.isResetPasswordConfirmIdentity()).thenReturn(false);
    when(userService.forgotPassword(argThat(p -> p.getEmail().equals("email@test.com")), eq(client), any(User.class))).thenReturn(Completable.error(new EnforceUserIdentityException()));
    testRequest(HttpMethod.POST, "/forgotPassword?client_id=client-id", req -> postEmail(req, "email@test.com"), resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertTrue(location.endsWith("/forgotPassword?client_id=client-id&success=forgot_password_completed"));
    }, HttpStatusCode.FOUND_302, "Found", null);
}
Also used : ErrorHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.ErrorHandler) APPLICATION_X_WWW_FORM_URLENCODED(io.vertx.core.http.HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED) ArgumentMatchers(org.mockito.ArgumentMatchers) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) EmailFormatInvalidException(io.gravitee.am.service.exception.EmailFormatInvalidException) Completable(io.reactivex.Completable) RunWith(org.junit.runner.RunWith) UserService(io.gravitee.am.gateway.handler.root.service.user.UserService) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) EnforceUserIdentityException(io.gravitee.am.service.exception.EnforceUserIdentityException) UserNotFoundException(io.gravitee.am.service.exception.UserNotFoundException) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) CONTENT_TYPE(io.vertx.core.http.HttpHeaders.CONTENT_TYPE) User(io.gravitee.am.identityprovider.api.User) HttpMethod(io.vertx.core.http.HttpMethod) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AccountSettings(io.gravitee.am.model.account.AccountSettings) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.reset(org.mockito.Mockito.reset) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.identityprovider.api.User) EnforceUserIdentityException(io.gravitee.am.service.exception.EnforceUserIdentityException) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Example 3 with EnforceUserIdentityException

use of io.gravitee.am.service.exception.EnforceUserIdentityException in project gravitee-access-management by gravitee-io.

the class ForgotPasswordSubmissionEndpointTest method shouldCompleteWithWarningWhen_EnforceIdentityException_TooManyResult.

@Test
public void shouldCompleteWithWarningWhen_EnforceIdentityException_TooManyResult() throws Exception {
    Client client = new Client();
    client.setId("client-id");
    client.setClientId("client-id");
    router.route().order(-1).handler(routingContext -> {
        routingContext.put("client", client);
        routingContext.next();
    });
    when(accountSettings.isResetPasswordConfirmIdentity()).thenReturn(true);
    when(userService.forgotPassword(argThat(p -> p.getEmail().equals("email@test.com")), eq(client), any(User.class))).thenReturn(Completable.error(new EnforceUserIdentityException()));
    testRequest(HttpMethod.POST, "/forgotPassword?client_id=client-id", req -> postEmail(req, "email@test.com"), resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertTrue(location.endsWith("/forgotPassword?client_id=client-id&warning=forgot_password_confirm"));
    }, HttpStatusCode.FOUND_302, "Found", null);
}
Also used : ErrorHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.ErrorHandler) APPLICATION_X_WWW_FORM_URLENCODED(io.vertx.core.http.HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED) ArgumentMatchers(org.mockito.ArgumentMatchers) Client(io.gravitee.am.model.oidc.Client) Mock(org.mockito.Mock) EmailFormatInvalidException(io.gravitee.am.service.exception.EmailFormatInvalidException) Completable(io.reactivex.Completable) RunWith(org.junit.runner.RunWith) UserService(io.gravitee.am.gateway.handler.root.service.user.UserService) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BodyHandler(io.vertx.reactivex.ext.web.handler.BodyHandler) EnforceUserIdentityException(io.gravitee.am.service.exception.EnforceUserIdentityException) UserNotFoundException(io.gravitee.am.service.exception.UserNotFoundException) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) CONTENT_TYPE(io.vertx.core.http.HttpHeaders.CONTENT_TYPE) User(io.gravitee.am.identityprovider.api.User) HttpMethod(io.vertx.core.http.HttpMethod) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) AccountSettings(io.gravitee.am.model.account.AccountSettings) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.reset(org.mockito.Mockito.reset) Buffer(io.vertx.reactivex.core.buffer.Buffer) User(io.gravitee.am.identityprovider.api.User) EnforceUserIdentityException(io.gravitee.am.service.exception.EnforceUserIdentityException) Client(io.gravitee.am.model.oidc.Client) Test(org.junit.Test)

Aggregations

AccountSettings (io.gravitee.am.model.account.AccountSettings)3 Client (io.gravitee.am.model.oidc.Client)3 EnforceUserIdentityException (io.gravitee.am.service.exception.EnforceUserIdentityException)3 UserNotFoundException (io.gravitee.am.service.exception.UserNotFoundException)3 RxWebTestBase (io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase)2 ErrorHandler (io.gravitee.am.gateway.handler.common.vertx.web.handler.ErrorHandler)2 UserService (io.gravitee.am.gateway.handler.root.service.user.UserService)2 User (io.gravitee.am.identityprovider.api.User)2 Domain (io.gravitee.am.model.Domain)2 EmailFormatInvalidException (io.gravitee.am.service.exception.EmailFormatInvalidException)2 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)2 Completable (io.reactivex.Completable)2 APPLICATION_X_WWW_FORM_URLENCODED (io.vertx.core.http.HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED)2 CONTENT_TYPE (io.vertx.core.http.HttpHeaders.CONTENT_TYPE)2 HttpMethod (io.vertx.core.http.HttpMethod)2 Buffer (io.vertx.reactivex.core.buffer.Buffer)2 BodyHandler (io.vertx.reactivex.ext.web.handler.BodyHandler)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers (org.mockito.ArgumentMatchers)2