Search in sources :

Example 46 with ProtocolMapperModel

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

the class OIDCLoginProtocolFactory method createUserAttributeMapper.

private static void createUserAttributeMapper(String name, String attrName, String claimName, String type) {
    ProtocolMapperModel model = UserAttributeMapper.createClaimMapper(name, attrName, claimName, type, true, true, false);
    builtins.put(name, model);
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 47 with ProtocolMapperModel

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

the class MigrateTo9_0_0 method addAccountConsoleClient.

protected void addAccountConsoleClient(RealmModel realm) {
    if (realm.getClientByClientId(Constants.ACCOUNT_CONSOLE_CLIENT_ID) == null) {
        ClientModel client = KeycloakModelUtils.createPublicClient(realm, Constants.ACCOUNT_CONSOLE_CLIENT_ID);
        client.setName("${client_" + Constants.ACCOUNT_CONSOLE_CLIENT_ID + "}");
        client.setEnabled(true);
        client.setFullScopeAllowed(false);
        client.setDirectAccessGrantsEnabled(false);
        client.setRootUrl(Constants.AUTH_BASE_URL_PROP);
        String baseUrl = "/realms/" + realm.getName() + "/account/";
        client.setBaseUrl(baseUrl);
        client.addRedirectUri(baseUrl + "*");
        client.setProtocol("openid-connect");
        RoleModel role = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).getRole(AccountRoles.MANAGE_ACCOUNT);
        if (role != null)
            client.addScopeMapping(role);
        ProtocolMapperModel audienceMapper = new ProtocolMapperModel();
        audienceMapper.setName("audience resolve");
        audienceMapper.setProtocol("openid-connect");
        audienceMapper.setProtocolMapper("oidc-audience-resolve-mapper");
        client.addProtocolMapper(audienceMapper);
    }
}
Also used : ClientModel(org.keycloak.models.ClientModel) RoleModel(org.keycloak.models.RoleModel) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 48 with ProtocolMapperModel

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

the class HardcodedRole method create.

public static ProtocolMapperModel create(String name, String role) {
    String mapperId = PROVIDER_ID;
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(mapperId);
    mapper.setProtocol(SamlProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<>();
    config.put(ROLE_ATTRIBUTE, role);
    mapper.setConfig(config);
    return mapper;
}
Also used : HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 49 with ProtocolMapperModel

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

the class RoleListMapper method mapRoles.

@Override
public void mapRoles(AttributeStatementType roleAttributeStatement, ProtocolMapperModel mappingModel, KeycloakSession session, UserSessionModel userSession, ClientSessionContext clientSessionCtx) {
    String single = mappingModel.getConfig().get(SINGLE_ROLE_ATTRIBUTE);
    boolean singleAttribute = Boolean.parseBoolean(single);
    List<SamlProtocol.ProtocolMapperProcessor<SAMLRoleNameMapper>> roleNameMappers = new LinkedList<>();
    AtomicReference<AttributeType> singleAttributeType = new AtomicReference<>(null);
    ProtocolMapperUtils.getSortedProtocolMappers(session, clientSessionCtx).forEach(entry -> {
        ProtocolMapperModel mapping = entry.getKey();
        ProtocolMapper mapper = entry.getValue();
        if (mapper instanceof SAMLRoleNameMapper) {
            roleNameMappers.add(new SamlProtocol.ProtocolMapperProcessor<>((SAMLRoleNameMapper) mapper, mapping));
        }
        if (mapper instanceof HardcodedRole) {
            AttributeType attributeType;
            if (singleAttribute) {
                if (singleAttributeType.get() == null) {
                    singleAttributeType.set(AttributeStatementHelper.createAttributeType(mappingModel));
                    roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(singleAttributeType.get()));
                }
                attributeType = singleAttributeType.get();
            } else {
                attributeType = AttributeStatementHelper.createAttributeType(mappingModel);
                roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attributeType));
            }
            attributeType.addAttributeValue(mapping.getConfig().get(HardcodedRole.ROLE_ATTRIBUTE));
        }
    });
    List<String> allRoleNames = clientSessionCtx.getRolesStream().map(roleModel -> roleNameMappers.stream().map(entry -> entry.mapper.mapName(entry.model, roleModel)).filter(Objects::nonNull).findFirst().orElse(roleModel.getName())).collect(Collectors.toList());
    for (String roleName : allRoleNames) {
        AttributeType attributeType;
        if (singleAttribute) {
            if (singleAttributeType.get() == null) {
                singleAttributeType.set(AttributeStatementHelper.createAttributeType(mappingModel));
                roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(singleAttributeType.get()));
            }
            attributeType = singleAttributeType.get();
        } else {
            attributeType = AttributeStatementHelper.createAttributeType(mappingModel);
            roleAttributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attributeType));
        }
        attributeType.addAttributeValue(roleName);
    }
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) KeycloakSession(org.keycloak.models.KeycloakSession) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) HashMap(java.util.HashMap) UserSessionModel(org.keycloak.models.UserSessionModel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) ArrayList(java.util.ArrayList) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Objects(java.util.Objects) List(java.util.List) ClientSessionContext(org.keycloak.models.ClientSessionContext) Map(java.util.Map) ProtocolMapperUtils(org.keycloak.protocol.ProtocolMapperUtils) ProtocolMapper(org.keycloak.protocol.ProtocolMapper) LinkedList(java.util.LinkedList) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtocolMapper(org.keycloak.protocol.ProtocolMapper) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) LinkedList(java.util.LinkedList) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) Objects(java.util.Objects)

Example 50 with ProtocolMapperModel

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

the class AttributeStatementHelper method createAttributeMapper.

public static ProtocolMapperModel createAttributeMapper(String name, String userAttribute, String samlAttributeName, String nameFormat, String friendlyName, String mapperId) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(mapperId);
    mapper.setProtocol(SamlProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<>();
    if (userAttribute != null)
        config.put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute);
    config.put(SAML_ATTRIBUTE_NAME, samlAttributeName);
    if (friendlyName != null) {
        config.put(FRIENDLY_NAME, friendlyName);
    }
    if (nameFormat != null) {
        config.put(SAML_ATTRIBUTE_NAMEFORMAT, nameFormat);
    }
    mapper.setConfig(config);
    return mapper;
}
Also used : HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

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