Search in sources :

Example 16 with IdentityProviderModel

use of org.keycloak.models.IdentityProviderModel in project keycloak by keycloak.

the class IdentityProviderResource method getMapperTypes.

/**
 * Get mapper types for identity provider
 */
@GET
@Path("mapper-types")
@NoCache
public Map<String, IdentityProviderMapperTypeRepresentation> getMapperTypes() {
    this.auth.realm().requireViewIdentityProviders();
    if (identityProviderModel == null) {
        throw new javax.ws.rs.NotFoundException();
    }
    KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
    return sessionFactory.getProviderFactoriesStream(IdentityProviderMapper.class).map(IdentityProviderMapper.class::cast).map(mapper -> Arrays.stream(mapper.getCompatibleProviders()).filter(type -> Objects.equals(IdentityProviderMapper.ANY_PROVIDER, type) || Objects.equals(identityProviderModel.getProviderId(), type)).map(type -> {
        IdentityProviderMapperTypeRepresentation rep = new IdentityProviderMapperTypeRepresentation();
        rep.setId(mapper.getId());
        rep.setCategory(mapper.getDisplayCategory());
        rep.setName(mapper.getDisplayType());
        rep.setHelpText(mapper.getHelpText());
        rep.setProperties(mapper.getConfigProperties().stream().map(ModelToRepresentation::toRepresentation).collect(Collectors.toList()));
        return rep;
    }).findFirst().orElse(null)).filter(Objects::nonNull).collect(Collectors.toMap(IdentityProviderMapperTypeRepresentation::getId, Function.identity()));
}
Also used : OperationType(org.keycloak.events.admin.OperationType) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) PathParam(javax.ws.rs.PathParam) Arrays(java.util.Arrays) ResourceType(org.keycloak.events.admin.ResourceType) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Produces(javax.ws.rs.Produces) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) GET(javax.ws.rs.GET) Logger(org.jboss.logging.Logger) Path(javax.ws.rs.Path) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) Function(java.util.function.Function) RepresentationToModel(org.keycloak.models.utils.RepresentationToModel) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) MediaType(javax.ws.rs.core.MediaType) UserModel(org.keycloak.models.UserModel) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) AdminPermissionManagement(org.keycloak.services.resources.admin.permissions.AdminPermissionManagement) StripSecretsUtils(org.keycloak.models.utils.StripSecretsUtils) IdentityProvider(org.keycloak.broker.provider.IdentityProvider) IdentityProviderMapper(org.keycloak.broker.provider.IdentityProviderMapper) DELETE(javax.ws.rs.DELETE) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) AdminPermissions(org.keycloak.services.resources.admin.permissions.AdminPermissions) RealmModel(org.keycloak.models.RealmModel) POST(javax.ws.rs.POST) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ManagementPermissionReference(org.keycloak.representations.idm.ManagementPermissionReference) Objects(java.util.Objects) SocialIdentityProvider(org.keycloak.broker.social.SocialIdentityProvider) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Stream(java.util.stream.Stream) NoCache(org.jboss.resteasy.annotations.cache.NoCache) Response(javax.ws.rs.core.Response) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) PUT(javax.ws.rs.PUT) ErrorResponse(org.keycloak.services.ErrorResponse) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) NotFoundException(javax.ws.rs.NotFoundException) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 17 with IdentityProviderModel

use of org.keycloak.models.IdentityProviderModel in project keycloak by keycloak.

the class BrokerRunOnServerUtil method configureAutoLinkFlow.

static RunOnServer configureAutoLinkFlow(String idpAlias) {
    return (session -> {
        RealmModel appRealm = session.getContext().getRealm();
        AuthenticationFlowModel newFlow = new AuthenticationFlowModel();
        newFlow.setAlias("AutoLink");
        newFlow.setDescription("AutoLink");
        newFlow.setProviderId("basic-flow");
        newFlow.setBuiltIn(false);
        newFlow.setTopLevel(true);
        newFlow = appRealm.addAuthenticationFlow(newFlow);
        AuthenticationExecutionModel execution = new AuthenticationExecutionModel();
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setAuthenticatorFlow(false);
        execution.setAuthenticator("idp-create-user-if-unique");
        execution.setPriority(1);
        execution.setParentFlow(newFlow.getId());
        execution = appRealm.addAuthenticatorExecution(execution);
        AuthenticationExecutionModel execution2 = new AuthenticationExecutionModel();
        execution2.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution2.setAuthenticatorFlow(false);
        execution2.setAuthenticator("idp-auto-link");
        execution2.setPriority(2);
        execution2.setParentFlow(newFlow.getId());
        execution2 = appRealm.addAuthenticatorExecution(execution2);
        IdentityProviderModel idp = appRealm.getIdentityProviderByAlias(idpAlias);
        idp.setFirstBrokerLoginFlowId(newFlow.getId());
        appRealm.updateIdentityProvider(idp);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) IdentityProviderModel(org.keycloak.models.IdentityProviderModel)

Example 18 with IdentityProviderModel

use of org.keycloak.models.IdentityProviderModel in project keycloak by keycloak.

the class FlowUtil method usesInIdentityProvider.

public FlowUtil usesInIdentityProvider(String idpAlias) {
    // Setup new FirstBrokerLogin flow to identity provider
    IdentityProviderModel idp = realm.getIdentityProviderByAlias(idpAlias);
    idp.setFirstBrokerLoginFlowId(currentFlow.getId());
    realm.updateIdentityProvider(idp);
    return this;
}
Also used : IdentityProviderModel(org.keycloak.models.IdentityProviderModel)

Example 19 with IdentityProviderModel

use of org.keycloak.models.IdentityProviderModel in project keycloak by keycloak.

the class FreeMarkerLoginFormsProvider method createCommonAttributes.

/**
 * Create common attributes used in all templates.
 *
 * @param theme actual Theme used (provided by <code>getTheme()</code>)
 * @param locale actual locale
 * @param messagesBundle actual message bundle (provided by <code>handleThemeResources()</code>)
 * @param baseUriBuilder actual base uri builder (provided by <code>prepareBaseUriBuilder()</code>)
 * @param page in case if common page is rendered, is null if called from <code>createForm()</code>
 */
protected void createCommonAttributes(Theme theme, Locale locale, Properties messagesBundle, UriBuilder baseUriBuilder, LoginFormsPages page) {
    URI baseUri = baseUriBuilder.build();
    if (accessCode != null) {
        baseUriBuilder.queryParam(LoginActionsService.SESSION_CODE, accessCode);
    }
    URI baseUriWithCodeAndClientId = baseUriBuilder.build();
    if (client != null) {
        attributes.put("client", new ClientBean(session, client));
    }
    if (realm != null) {
        attributes.put("realm", new RealmBean(realm));
        List<IdentityProviderModel> identityProviders = LoginFormsUtil.filterIdentityProviders(realm.getIdentityProvidersStream(), session, context);
        attributes.put("social", new IdentityProviderBean(realm, session, identityProviders, baseUriWithCodeAndClientId));
        attributes.put("url", new UrlBean(realm, theme, baseUri, this.actionUri));
        attributes.put("requiredActionUrl", new RequiredActionUrlFormatterMethod(realm, baseUri));
        attributes.put("auth", new AuthenticationContextBean(context, page));
        attributes.put(Constants.EXECUTION, execution);
        if (realm.isInternationalizationEnabled()) {
            UriBuilder b;
            if (page != null) {
                switch(page) {
                    case LOGIN:
                    case LOGIN_USERNAME:
                    case X509_CONFIRM:
                        b = UriBuilder.fromUri(Urls.realmLoginPage(baseUri, realm.getName()));
                        break;
                    case REGISTER:
                        b = UriBuilder.fromUri(Urls.realmRegisterPage(baseUri, realm.getName()));
                        break;
                    default:
                        b = UriBuilder.fromUri(baseUri).path(uriInfo.getPath());
                        break;
                }
            } else {
                b = UriBuilder.fromUri(baseUri).path(uriInfo.getPath());
            }
            if (execution != null) {
                b.queryParam(Constants.EXECUTION, execution);
            }
            if (authenticationSession != null && authenticationSession.getAuthNote(Constants.KEY) != null) {
                b.queryParam(Constants.KEY, authenticationSession.getAuthNote(Constants.KEY));
            }
            attributes.put("locale", new LocaleBean(realm, locale, b, messagesBundle));
        }
    }
    if (realm != null && user != null && session != null) {
        attributes.put("authenticatorConfigured", new AuthenticatorConfiguredMethod(realm, user, session));
    }
    if (authenticationSession != null && authenticationSession.getClientNote(Constants.KC_ACTION_EXECUTING) != null) {
        attributes.put("isAppInitiatedAction", true);
    }
}
Also used : RequiredActionUrlFormatterMethod(org.keycloak.forms.login.freemarker.model.RequiredActionUrlFormatterMethod) IdentityProviderBean(org.keycloak.forms.login.freemarker.model.IdentityProviderBean) ClientBean(org.keycloak.forms.login.freemarker.model.ClientBean) UrlBean(org.keycloak.forms.login.freemarker.model.UrlBean) AuthenticationContextBean(org.keycloak.forms.login.freemarker.model.AuthenticationContextBean) RealmBean(org.keycloak.forms.login.freemarker.model.RealmBean) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) LocaleBean(org.keycloak.theme.beans.LocaleBean)

Example 20 with IdentityProviderModel

use of org.keycloak.models.IdentityProviderModel in project keycloak by keycloak.

the class IdentityProvidersResource method create.

/**
 * Create a new identity provider
 *
 * @param representation JSON body
 * @return
 */
@POST
@Path("instances")
@Consumes(MediaType.APPLICATION_JSON)
public Response create(IdentityProviderRepresentation representation) {
    this.auth.realm().requireManageIdentityProviders();
    ReservedCharValidator.validate(representation.getAlias());
    try {
        IdentityProviderModel identityProvider = RepresentationToModel.toModel(realm, representation, session);
        this.realm.addIdentityProvider(identityProvider);
        representation.setInternalId(identityProvider.getInternalId());
        adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), identityProvider.getAlias()).representation(StripSecretsUtils.strip(representation)).success();
        return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(representation.getAlias()).build()).build();
    } catch (IllegalArgumentException e) {
        String message = e.getMessage();
        if (message == null) {
            message = "Invalid request";
        }
        return ErrorResponse.error(message, BAD_REQUEST);
    } catch (ModelDuplicateException e) {
        return ErrorResponse.exists("Identity Provider " + representation.getAlias() + " already exists");
    }
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

IdentityProviderModel (org.keycloak.models.IdentityProviderModel)32 RealmModel (org.keycloak.models.RealmModel)10 Response (javax.ws.rs.core.Response)8 IdentityProvider (org.keycloak.broker.provider.IdentityProvider)8 KeycloakSession (org.keycloak.models.KeycloakSession)6 UserModel (org.keycloak.models.UserModel)6 NotFoundException (javax.ws.rs.NotFoundException)5 Path (javax.ws.rs.Path)5 Test (org.junit.Test)5 AuthenticationSessionModel (org.keycloak.sessions.AuthenticationSessionModel)5 IOException (java.io.IOException)4 Map (java.util.Map)4 OAuthErrorException (org.keycloak.OAuthErrorException)4 IdentityBrokerException (org.keycloak.broker.provider.IdentityBrokerException)4 IdentityProviderFactory (org.keycloak.broker.provider.IdentityProviderFactory)4 SocialIdentityProvider (org.keycloak.broker.social.SocialIdentityProvider)4 ClientModel (org.keycloak.models.ClientModel)4 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)4 ErrorResponse (org.keycloak.services.ErrorResponse)4 URI (java.net.URI)3