Search in sources :

Example 6 with IdentityProvider

use of io.gravitee.am.model.IdentityProvider in project gravitee-access-management by gravitee-io.

the class IdentifierFirstLoginEndpoint method redirect.

private void redirect(RoutingContext routingContext) {
    final List<IdentityProvider> socialProviders = routingContext.get(SOCIAL_PROVIDER_CONTEXT_KEY);
    final String username = routingContext.request().getParam(USERNAME_PARAM_KEY);
    final String[] domainName = username.split("@");
    // username is not an email, continue
    if (domainName.length < 2) {
        doInternalRedirect(routingContext);
        return;
    }
    // no social providers configured, continue
    if (socialProviders == null) {
        doInternalRedirect(routingContext);
        return;
    }
    final IdentityProvider identityProvider = socialProviders.stream().filter(s -> s.getDomainWhitelist() != null && s.getDomainWhitelist().stream().anyMatch(domainName[1]::equals)).findFirst().orElse(null);
    // no IdP has matched, continue
    if (identityProvider == null) {
        doInternalRedirect(routingContext);
        return;
    }
    // else, redirect to the external provider
    doExternalRedirect(routingContext, identityProvider);
}
Also used : IdentityProvider(io.gravitee.am.model.IdentityProvider)

Example 7 with IdentityProvider

use of io.gravitee.am.model.IdentityProvider in project gravitee-access-management by gravitee-io.

the class IdentifierFirstLoginEndpointTest method shouldInvokeLoginEndpoint_redirectUsernameMatchesDomain.

@Test
public void shouldInvokeLoginEndpoint_redirectUsernameMatchesDomain() throws Exception {
    router.route(HttpMethod.POST, "/login/identifier").handler(routingContext -> {
        final IdentityProvider idp = new IdentityProvider();
        idp.setId("provider-id");
        idp.setDomainWhitelist(List.of("domain.com"));
        routingContext.put(SOCIAL_PROVIDER_CONTEXT_KEY, List.of(idp));
        routingContext.put(SOCIAL_AUTHORIZE_URL_CONTEXT_KEY, Map.of(idp.getId(), "https://host/some/provider/oauth/authorize"));
        routingContext.next();
    }).handler(identifierFirstLoginEndpoint::handle);
    when(clientSyncService.findByClientId(appClient.getClientId())).thenReturn(Maybe.just(appClient));
    testRequest(HttpMethod.POST, "/login/identifier?username=username@domain.com&client_id=" + appClient.getClientId() + "&response_type=code&redirect_uri=somewhere.com", null, resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertTrue(location.contains("https://host/some/provider/oauth/authorize?login_hint=username%40domain.com"));
    }, HttpStatusCode.FOUND_302, "Found", null);
}
Also used : ErrorHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.ErrorHandler) Client(io.gravitee.am.model.oidc.Client) TemplateEngine(io.vertx.reactivex.ext.web.common.template.TemplateEngine) Mock(org.mockito.Mock) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) ConstantKeys(io.gravitee.am.common.utils.ConstantKeys) IdentityProvider(io.gravitee.am.model.IdentityProvider) BotDetectionManager(io.gravitee.am.gateway.handler.manager.botdetection.BotDetectionManager) TreeSet(java.util.TreeSet) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) ClientSyncService(io.gravitee.am.gateway.handler.common.client.ClientSyncService) RequestUtils(io.gravitee.am.gateway.handler.common.vertx.utils.RequestUtils) ClientRequestParseHandler(io.gravitee.am.gateway.handler.root.resources.handler.client.ClientRequestParseHandler) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) UriBuilderRequest.resolveProxyRequest(io.gravitee.am.gateway.handler.common.vertx.utils.UriBuilderRequest.resolveProxyRequest) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) SOCIAL_AUTHORIZE_URL_CONTEXT_KEY(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginSocialAuthenticationHandler.SOCIAL_AUTHORIZE_URL_CONTEXT_KEY) SOCIAL_PROVIDER_CONTEXT_KEY(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginSocialAuthenticationHandler.SOCIAL_PROVIDER_CONTEXT_KEY) LoginSettings(io.gravitee.am.model.login.LoginSettings) HttpMethod(io.vertx.core.http.HttpMethod) CONTEXT_PATH(io.gravitee.am.gateway.handler.common.vertx.utils.UriBuilderRequest.CONTEXT_PATH) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) IdentityProvider(io.gravitee.am.model.IdentityProvider) Test(org.junit.Test)

Example 8 with IdentityProvider

use of io.gravitee.am.model.IdentityProvider in project gravitee-access-management by gravitee-io.

the class IdentifierFirstLoginEndpointTest method shouldInvokeLoginEndpoint_redirectUsernameMatchesDomainAndSpecificIdp.

@Test
public void shouldInvokeLoginEndpoint_redirectUsernameMatchesDomainAndSpecificIdp() throws Exception {
    router.route(HttpMethod.POST, "/login/identifier").handler(routingContext -> {
        final IdentityProvider idp = new IdentityProvider();
        idp.setId("provider-id");
        idp.setDomainWhitelist(List.of("domain.com"));
        idp.setType("google");
        routingContext.put(SOCIAL_PROVIDER_CONTEXT_KEY, List.of(idp));
        routingContext.put(SOCIAL_AUTHORIZE_URL_CONTEXT_KEY, Map.of(idp.getId(), "https://host/some/provider/oauth/authorize"));
        routingContext.next();
    }).handler(identifierFirstLoginEndpoint::handle);
    when(clientSyncService.findByClientId(appClient.getClientId())).thenReturn(Maybe.just(appClient));
    testRequest(HttpMethod.POST, "/login/identifier?username=username@domain.com&client_id=" + appClient.getClientId() + "&response_type=code&redirect_uri=somewhere.com", null, resp -> {
        String location = resp.headers().get("location");
        assertNotNull(location);
        assertTrue(location.equals("https://host/some/provider/oauth/authorize?login_hint=username%40domain.com"));
    }, HttpStatusCode.FOUND_302, "Found", null);
}
Also used : ErrorHandler(io.gravitee.am.gateway.handler.common.vertx.web.handler.ErrorHandler) Client(io.gravitee.am.model.oidc.Client) TemplateEngine(io.vertx.reactivex.ext.web.common.template.TemplateEngine) Mock(org.mockito.Mock) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) ConstantKeys(io.gravitee.am.common.utils.ConstantKeys) IdentityProvider(io.gravitee.am.model.IdentityProvider) BotDetectionManager(io.gravitee.am.gateway.handler.manager.botdetection.BotDetectionManager) TreeSet(java.util.TreeSet) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RxWebTestBase(io.gravitee.am.gateway.handler.common.vertx.RxWebTestBase) ClientSyncService(io.gravitee.am.gateway.handler.common.client.ClientSyncService) RequestUtils(io.gravitee.am.gateway.handler.common.vertx.utils.RequestUtils) ClientRequestParseHandler(io.gravitee.am.gateway.handler.root.resources.handler.client.ClientRequestParseHandler) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) UriBuilderRequest.resolveProxyRequest(io.gravitee.am.gateway.handler.common.vertx.utils.UriBuilderRequest.resolveProxyRequest) RoutingContext(io.vertx.reactivex.ext.web.RoutingContext) Mockito(org.mockito.Mockito) MultiMap(io.vertx.reactivex.core.MultiMap) List(java.util.List) SOCIAL_AUTHORIZE_URL_CONTEXT_KEY(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginSocialAuthenticationHandler.SOCIAL_AUTHORIZE_URL_CONTEXT_KEY) SOCIAL_PROVIDER_CONTEXT_KEY(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginSocialAuthenticationHandler.SOCIAL_PROVIDER_CONTEXT_KEY) LoginSettings(io.gravitee.am.model.login.LoginSettings) HttpMethod(io.vertx.core.http.HttpMethod) CONTEXT_PATH(io.gravitee.am.gateway.handler.common.vertx.utils.UriBuilderRequest.CONTEXT_PATH) Handler(io.vertx.core.Handler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) IdentityProvider(io.gravitee.am.model.IdentityProvider) Test(org.junit.Test)

Example 9 with IdentityProvider

use of io.gravitee.am.model.IdentityProvider in project gravitee-access-management by gravitee-io.

the class LoginEndpointHandlerTest method shouldInvokeLoginEndpoint_redirectProvider.

@Test
public void shouldInvokeLoginEndpoint_redirectProvider() throws Exception {
    appClient.getLoginSettings().setIdentifierFirstEnabled(false);
    appClient.getLoginSettings().setHideForm(TRUE);
    router.route(HttpMethod.GET, "/login").handler(routingContext -> {
        final IdentityProvider idp = new IdentityProvider();
        idp.setId("provider-id");
        routingContext.put(SOCIAL_PROVIDER_CONTEXT_KEY, List.of(idp));
        routingContext.put(SOCIAL_AUTHORIZE_URL_CONTEXT_KEY, Map.of(idp.getId(), "/some/provider/oauth/authorize"));
        routingContext.next();
    }).handler(new LoginHideFormHandler(domain)).handler(get302AssertMockRoutingContextHandler(loginEndpoint, true, false));
    when(clientSyncService.findByClientId(appClient.getClientId())).thenReturn(Maybe.just(appClient));
    testRequest(HttpMethod.GET, "/login?client_id=" + appClient.getClientId() + "&response_type=code&redirect_uri=somewhere.com", HttpStatusCode.FOUND_302, "Found");
}
Also used : LoginHideFormHandler(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginHideFormHandler) IdentityProvider(io.gravitee.am.model.IdentityProvider) Test(org.junit.Test)

Example 10 with IdentityProvider

use of io.gravitee.am.model.IdentityProvider in project gravitee-access-management by gravitee-io.

the class LoginEndpointHandlerTest method shouldInvokeLoginEndpoint_noRedirectMultipleProviders.

@Test
public void shouldInvokeLoginEndpoint_noRedirectMultipleProviders() throws Exception {
    appClient.getLoginSettings().setIdentifierFirstEnabled(false);
    appClient.getLoginSettings().setHideForm(TRUE);
    router.route(HttpMethod.GET, "/login").handler(routingContext -> {
        final IdentityProvider idp1 = new IdentityProvider();
        idp1.setId("provider-id-1");
        final IdentityProvider idp2 = new IdentityProvider();
        idp2.setId("provider-id-2");
        routingContext.put(SOCIAL_PROVIDER_CONTEXT_KEY, List.of(idp1, idp2));
        routingContext.next();
    }).handler(new LoginHideFormHandler(domain)).handler(get200AssertMockRoutingContextHandler(loginEndpoint, true, false));
    when(clientSyncService.findByClientId(appClient.getClientId())).thenReturn(Maybe.just(appClient));
    testRequest(HttpMethod.GET, "/login?client_id=" + appClient.getClientId() + "&response_type=code&redirect_uri=somewhere.com", HttpStatusCode.OK_200, "OK");
}
Also used : LoginHideFormHandler(io.gravitee.am.gateway.handler.root.resources.handler.login.LoginHideFormHandler) IdentityProvider(io.gravitee.am.model.IdentityProvider) Test(org.junit.Test)

Aggregations

IdentityProvider (io.gravitee.am.model.IdentityProvider)72 Test (org.junit.Test)48 NewIdentityProvider (io.gravitee.am.service.model.NewIdentityProvider)23 Client (io.gravitee.am.model.oidc.Client)20 UpdateIdentityProvider (io.gravitee.am.service.model.UpdateIdentityProvider)16 Maybe (io.reactivex.Maybe)16 DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)15 AuthenticationProvider (io.gravitee.am.identityprovider.api.AuthenticationProvider)14 Domain (io.gravitee.am.model.Domain)14 ApplicationIdentityProvider (io.gravitee.am.model.idp.ApplicationIdentityProvider)13 TestObserver (io.reactivex.observers.TestObserver)12 User (io.gravitee.am.model.User)11 Authentication (io.gravitee.am.identityprovider.api.Authentication)10 List (java.util.List)10 RunWith (org.junit.runner.RunWith)8 Mock (org.mockito.Mock)8 Mockito (org.mockito.Mockito)8 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)8 Map (java.util.Map)7 Completable (io.reactivex.Completable)6