Search in sources :

Example 16 with ProtocolMapperModel

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

the class TestingResourceProvider method generateAudienceClientScope.

/**
 * Generate new client scope for specified service client. The "Frontend" clients, who will use this client scope, will be able to
 * send their access token to authenticate against specified service client
 *
 * @param clientId Client ID of service client (typically bearer-only client)
 * @return ID of the newly generated clientScope
 */
@Path("generate-audience-client-scope")
@POST
@NoCache
public String generateAudienceClientScope(@QueryParam("realm") final String realmName, @QueryParam("clientId") final String clientId) {
    try {
        RealmModel realm = getRealmByName(realmName);
        ClientModel serviceClient = realm.getClientByClientId(clientId);
        if (serviceClient == null) {
            throw new NotFoundException("Referenced service client doesn't exist");
        }
        ClientScopeModel clientScopeModel = realm.addClientScope(clientId);
        clientScopeModel.setProtocol(serviceClient.getProtocol() == null ? OIDCLoginProtocol.LOGIN_PROTOCOL : serviceClient.getProtocol());
        clientScopeModel.setDisplayOnConsentScreen(true);
        clientScopeModel.setConsentScreenText(clientId);
        clientScopeModel.setIncludeInTokenScope(true);
        // Add audience protocol mapper
        ProtocolMapperModel audienceMapper = AudienceProtocolMapper.createClaimMapper("Audience for " + clientId, clientId, null, true, false);
        clientScopeModel.addProtocolMapper(audienceMapper);
        return clientScopeModel.getId();
    } catch (ModelDuplicateException e) {
        throw new BadRequestException("Client Scope " + clientId + " already exists");
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) ClientScopeModel(org.keycloak.models.ClientScopeModel) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 17 with ProtocolMapperModel

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

the class RepresentationToModel method updateClientProtocolMappers.

public static void updateClientProtocolMappers(ClientRepresentation rep, ClientModel resource) {
    if (rep.getProtocolMappers() != null) {
        Map<String, ProtocolMapperModel> existingProtocolMappers = resource.getProtocolMappersStream().collect(Collectors.toMap(mapper -> generateProtocolNameKey(mapper.getProtocol(), mapper.getName()), Function.identity()));
        for (ProtocolMapperRepresentation protocolMapperRepresentation : rep.getProtocolMappers()) {
            String protocolNameKey = generateProtocolNameKey(protocolMapperRepresentation.getProtocol(), protocolMapperRepresentation.getName());
            ProtocolMapperModel existingMapper = existingProtocolMappers.get(protocolNameKey);
            if (existingMapper != null) {
                ProtocolMapperModel updatedProtocolMapperModel = toModel(protocolMapperRepresentation);
                updatedProtocolMapperModel.setId(existingMapper.getId());
                resource.updateProtocolMapper(updatedProtocolMapperModel);
                existingProtocolMappers.remove(protocolNameKey);
            } else {
                resource.addProtocolMapper(toModel(protocolMapperRepresentation));
            }
        }
        for (Map.Entry<String, ProtocolMapperModel> entryToDelete : existingProtocolMappers.entrySet()) {
            resource.removeProtocolMapper(entryToDelete.getValue());
        }
    }
}
Also used : Arrays(java.util.Arrays) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) AuthenticationExecutionExportRepresentation(org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation) LDAPConstants(org.keycloak.models.LDAPConstants) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) OAuth2DeviceConfig(org.keycloak.models.OAuth2DeviceConfig) Map(java.util.Map) SocialLinkRepresentation(org.keycloak.representations.idm.SocialLinkRepresentation) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) UserCredentialModel(org.keycloak.models.UserCredentialModel) PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Set(java.util.Set) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) PolicyStore(org.keycloak.authorization.store.PolicyStore) ResourceStore(org.keycloak.authorization.store.ResourceStore) ClientTemplateRepresentation(org.keycloak.representations.idm.ClientTemplateRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) SocialIdentityProvider(org.keycloak.broker.social.SocialIdentityProvider) ResourceServerRepresentation(org.keycloak.representations.idm.authorization.ResourceServerRepresentation) SslRequired(org.keycloak.common.enums.SslRequired) PermissionTicketStore(org.keycloak.authorization.store.PermissionTicketStore) OAuth2Constants(org.keycloak.OAuth2Constants) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ClaimRepresentation(org.keycloak.representations.idm.ClaimRepresentation) Constants(org.keycloak.models.Constants) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) MigrationProvider(org.keycloak.migration.MigrationProvider) ArrayList(java.util.ArrayList) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) OTPSecretData(org.keycloak.models.credential.dto.OTPSecretData) UserModel(org.keycloak.models.UserModel) UserFederationProviderRepresentation(org.keycloak.representations.idm.UserFederationProviderRepresentation) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) MigrationUtils(org.keycloak.migration.migrators.MigrationUtils) ValidationUtil(org.keycloak.validation.ValidationUtil) PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) CredentialModel(org.keycloak.credential.CredentialModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) WebAuthnPolicy(org.keycloak.models.WebAuthnPolicy) KeycloakSession(org.keycloak.models.KeycloakSession) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) IOException(java.io.IOException) ResourceServerStore(org.keycloak.authorization.store.ResourceServerStore) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) ListIterator(java.util.ListIterator) MigrateTo8_0_0(org.keycloak.migration.migrators.MigrateTo8_0_0) PasswordPolicyNotMetException(org.keycloak.policy.PasswordPolicyNotMetException) Config(org.keycloak.Config) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClaimMask(org.keycloak.models.ClaimMask) ComponentModel(org.keycloak.component.ComponentModel) BrowserSecurityHeaders(org.keycloak.models.BrowserSecurityHeaders) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) PolicyEnforcementMode(org.keycloak.representations.idm.authorization.PolicyEnforcementMode) UriUtils(org.keycloak.common.util.UriUtils) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) AuthorizationProviderFactory(org.keycloak.authorization.AuthorizationProviderFactory) RoleModel(org.keycloak.models.RoleModel) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) ScopeStore(org.keycloak.authorization.store.ScopeStore) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Entry(java.util.Map.Entry) RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) ClientModel(org.keycloak.models.ClientModel) Scope(org.keycloak.authorization.model.Scope) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Profile(org.keycloak.common.Profile) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) ScopeMappingRepresentation(org.keycloak.representations.idm.ScopeMappingRepresentation) Logger(org.jboss.logging.Logger) StoreFactory(org.keycloak.authorization.store.StoreFactory) HashMap(java.util.HashMap) OAuthClientRepresentation(org.keycloak.representations.idm.OAuthClientRepresentation) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) HashSet(java.util.HashSet) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentExportRepresentation(org.keycloak.representations.idm.ComponentExportRepresentation) UserFederationMapperRepresentation(org.keycloak.representations.idm.UserFederationMapperRepresentation) UserConsentModel(org.keycloak.models.UserConsentModel) KeyProvider(org.keycloak.keys.KeyProvider) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) OTPPolicy(org.keycloak.models.OTPPolicy) GroupModel(org.keycloak.models.GroupModel) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) LinkedList(java.util.LinkedList) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) IdentityProvider(org.keycloak.broker.provider.IdentityProvider) ScopeContainerModel(org.keycloak.models.ScopeContainerModel) ResourceServer(org.keycloak.authorization.model.ResourceServer) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) RolesRepresentation(org.keycloak.representations.idm.RolesRepresentation) OTPCredentialData(org.keycloak.models.credential.dto.OTPCredentialData) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Iterator(java.util.Iterator) CibaConfig(org.keycloak.models.CibaConfig) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ApplicationRepresentation(org.keycloak.representations.idm.ApplicationRepresentation) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) DecisionStrategy(org.keycloak.representations.idm.authorization.DecisionStrategy) JsonSerialization(org.keycloak.util.JsonSerialization) Policy(org.keycloak.authorization.model.Policy) AuthenticatorConfigRepresentation(org.keycloak.representations.idm.AuthenticatorConfigRepresentation) ModelException(org.keycloak.models.ModelException) ParConfig(org.keycloak.models.ParConfig) PasswordPolicy(org.keycloak.models.PasswordPolicy) Resource(org.keycloak.authorization.model.Resource) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 18 with ProtocolMapperModel

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

the class RepresentationToModel method toModel.

public static ProtocolMapperModel toModel(ProtocolMapperRepresentation rep) {
    ProtocolMapperModel model = new ProtocolMapperModel();
    model.setId(rep.getId());
    model.setName(rep.getName());
    model.setProtocol(rep.getProtocol());
    model.setProtocolMapper(rep.getProtocolMapper());
    model.setConfig(removeEmptyString(rep.getConfig()));
    return model;
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 19 with ProtocolMapperModel

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

the class ProtocolMapperUtils method getSortedProtocolMappers.

public static Stream<Entry<ProtocolMapperModel, ProtocolMapper>> getSortedProtocolMappers(KeycloakSession session, ClientSessionContext ctx) {
    KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
    return ctx.getProtocolMappersStream().flatMap(mapperModel -> {
        ProtocolMapper mapper = (ProtocolMapper) sessionFactory.getProviderFactory(ProtocolMapper.class, mapperModel.getProtocolMapper());
        if (mapper == null)
            return null;
        Map<ProtocolMapperModel, ProtocolMapper> protocolMapperMap = new HashMap<>();
        protocolMapperMap.put(mapperModel, mapper);
        return protocolMapperMap.entrySet().stream();
    }).filter(Objects::nonNull).sorted(Comparator.comparing(ProtocolMapperUtils::compare));
}
Also used : HashMap(java.util.HashMap) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 20 with ProtocolMapperModel

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

the class OpenshiftSAClientAdapter method createDefaultProtocolMappers.

private static Set<ProtocolMapperModel> createDefaultProtocolMappers() {
    Set<ProtocolMapperModel> mappers = new HashSet<>();
    ProtocolMapperModel mapper = OIDCAttributeMapperHelper.createClaimMapper("username", "username", "preferred_username", "string", true, true, UserPropertyMapper.PROVIDER_ID);
    mapper.setId(KeycloakModelUtils.generateId());
    mappers.add(mapper);
    return mappers;
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) HashSet(java.util.HashSet)

Aggregations

ProtocolMapperModel (org.keycloak.models.ProtocolMapperModel)51 HashMap (java.util.HashMap)22 ClientModel (org.keycloak.models.ClientModel)7 Path (javax.ws.rs.Path)6 NoCache (org.jboss.resteasy.annotations.cache.NoCache)6 LinkedList (java.util.LinkedList)5 List (java.util.List)5 ProviderConfigProperty (org.keycloak.provider.ProviderConfigProperty)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 NotFoundException (javax.ws.rs.NotFoundException)4 ClientScopeModel (org.keycloak.models.ClientScopeModel)4 KeycloakSession (org.keycloak.models.KeycloakSession)4 RealmModel (org.keycloak.models.RealmModel)4 RoleModel (org.keycloak.models.RoleModel)4 UserModel (org.keycloak.models.UserModel)4 IDToken (org.keycloak.representations.IDToken)4 ProtocolMapperRepresentation (org.keycloak.representations.idm.ProtocolMapperRepresentation)4 IOException (java.io.IOException)3 MigrationProvider (org.keycloak.migration.MigrationProvider)3