Search in sources :

Example 1 with ProtocolMapperModel

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

the class AddressMapper method createAddressMapper.

public static ProtocolMapperModel createAddressMapper(boolean idToken, boolean accessToken, boolean userInfo) {
    Map<String, String> config;
    ProtocolMapperModel address = new ProtocolMapperModel();
    address.setName("address");
    address.setProtocolMapper(PROVIDER_ID);
    address.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    config = new HashMap<>();
    config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, Boolean.toString(accessToken));
    config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, Boolean.toString(idToken));
    config.put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, Boolean.toString(userInfo));
    config.put(getModelPropertyName(STREET), STREET);
    config.put(getModelPropertyName(AddressClaimSet.LOCALITY), AddressClaimSet.LOCALITY);
    config.put(getModelPropertyName(AddressClaimSet.REGION), AddressClaimSet.REGION);
    config.put(getModelPropertyName(AddressClaimSet.POSTAL_CODE), AddressClaimSet.POSTAL_CODE);
    config.put(getModelPropertyName(AddressClaimSet.COUNTRY), AddressClaimSet.COUNTRY);
    config.put(getModelPropertyName(AddressClaimSet.FORMATTED), AddressClaimSet.FORMATTED);
    address.setConfig(config);
    return address;
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 2 with ProtocolMapperModel

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

the class AudienceProtocolMapper method createClaimMapper.

public static ProtocolMapperModel createClaimMapper(String name, String includedClientAudience, String includedCustomAudience, boolean accessToken, boolean idToken) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(PROVIDER_ID);
    mapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<>();
    if (includedClientAudience != null) {
        config.put(INCLUDED_CLIENT_AUDIENCE, includedClientAudience);
    }
    if (includedCustomAudience != null) {
        config.put(INCLUDED_CUSTOM_AUDIENCE, includedCustomAudience);
    }
    if (accessToken)
        config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "true");
    if (idToken)
        config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "true");
    mapper.setConfig(config);
    return mapper;
}
Also used : HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 3 with ProtocolMapperModel

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

the class ClaimsParameterTokenMapper method putClaims.

private void putClaims(String tokenType, String claims, IDToken token, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
    JsonNode requestParams = null;
    try {
        requestParams = JsonSerialization.readValue(claims, JsonNode.class);
    } catch (IOException e) {
        return;
    }
    if (!requestParams.has(tokenType))
        return;
    JsonNode tokenNode = requestParams.findValue(tokenType);
    OIDCWellKnownProvider.DEFAULT_CLAIMS_SUPPORTED.stream().filter(i -> tokenNode.has(i)).filter(i -> tokenNode.findValue(i).has("essential")).filter(i -> tokenNode.findValue(i).findValue("essential").isBoolean()).filter(i -> tokenNode.findValue(i).findValue("essential").asBoolean()).forEach(i -> {
        // "name", "given_name", "family_name", "preferred_username", "email" need to be set explicitly using existing mapper.
        if (i.equals(IDToken.NAME)) {
            FullNameMapper fullNameMapper = new FullNameMapper();
            fullNameMapper.setClaim(token, mappingModel, userSession);
        } else if (i.equals(IDToken.GIVEN_NAME)) {
            UserPropertyMapper userPropertyMapper = new UserPropertyMapper();
            userPropertyMapper.setClaim(token, UserPropertyMapper.createClaimMapper("requested firstName", "firstName", IDToken.GIVEN_NAME, "String", false, true), userSession);
        } else if (i.equals(IDToken.FAMILY_NAME)) {
            UserPropertyMapper userPropertyMapper = new UserPropertyMapper();
            userPropertyMapper.setClaim(token, UserPropertyMapper.createClaimMapper("requested lastName", "lastName", IDToken.FAMILY_NAME, "String", false, true), userSession);
        } else if (i.equals(IDToken.PREFERRED_USERNAME)) {
            UserPropertyMapper userPropertyMapper = new UserPropertyMapper();
            userPropertyMapper.setClaim(token, UserPropertyMapper.createClaimMapper("requested username", "username", IDToken.PREFERRED_USERNAME, "String", false, true), userSession);
        } else if (i.equals(IDToken.EMAIL)) {
            UserPropertyMapper userPropertyMapper = new UserPropertyMapper();
            userPropertyMapper.setClaim(token, UserPropertyMapper.createClaimMapper("requested email", "email", IDToken.EMAIL, "String", false, true), userSession);
        }
    });
}
Also used : ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) KeycloakSession(org.keycloak.models.KeycloakSession) IOException(java.io.IOException) HashMap(java.util.HashMap) UserSessionModel(org.keycloak.models.UserSessionModel) IDToken(org.keycloak.representations.IDToken) OIDCWellKnownProvider(org.keycloak.protocol.oidc.OIDCWellKnownProvider) ArrayList(java.util.ArrayList) JsonSerialization(org.keycloak.util.JsonSerialization) List(java.util.List) TokenUtil(org.keycloak.util.TokenUtil) ClientSessionContext(org.keycloak.models.ClientSessionContext) Map(java.util.Map) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 4 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(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<>();
    config.put(ROLE_CONFIG, role);
    mapper.setConfig(config);
    return mapper;
}
Also used : HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 5 with ProtocolMapperModel

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

the class ProtocolMappersResource method update.

/**
 * Update the mapper
 *
 * @param id Mapper id
 * @param rep
 */
@PUT
@NoCache
@Path("models/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void update(@PathParam("id") String id, ProtocolMapperRepresentation rep) {
    managePermission.require();
    ProtocolMapperModel model = client.getProtocolMapperById(id);
    if (model == null)
        throw new NotFoundException("Model not found");
    model = RepresentationToModel.toModel(rep);
    validateModel(model);
    client.updateProtocolMapper(model);
    adminEvent.operation(OperationType.UPDATE).resourcePath(session.getContext().getUri()).representation(rep).success();
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache) PUT(javax.ws.rs.PUT)

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