Java Code Examples for org.keycloak.models.ProtocolMapperModel#setName()

The following examples show how to use org.keycloak.models.ProtocolMapperModel#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroupMembershipMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ProtocolMapperModel create(String name,
                                  String tokenClaimName,
                                  boolean consentRequired, String consentText,
                                  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<String, String>();
    config.put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, tokenClaimName);
    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;
}
 
Example 2
Source File: OIDCAttributeMapperHelper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ProtocolMapperModel createClaimMapper(String name,
                              String userAttribute,
                              String tokenClaimName, String claimType,
                              boolean accessToken, boolean idToken, boolean userinfo,
                              String mapperId) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(mapperId);
    mapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<String, String>();
    config.put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute);
    config.put(TOKEN_CLAIM_NAME, tokenClaimName);
    config.put(JSON_TYPE, claimType);
    if (accessToken) config.put(INCLUDE_IN_ACCESS_TOKEN, "true");
    if (idToken) config.put(INCLUDE_IN_ID_TOKEN, "true");
    if (userinfo) config.put(INCLUDE_IN_USERINFO, "true");
    mapper.setConfig(config);
    return mapper;
}
 
Example 3
Source File: AddressMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: AudienceProtocolMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: RoleListMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ProtocolMapperModel create(String name, String samlAttributeName, String nameFormat, String friendlyName, boolean singleAttribute) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(PROVIDER_ID);
    mapper.setProtocol(SamlProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<>();
    config.put(AttributeStatementHelper.SAML_ATTRIBUTE_NAME, samlAttributeName);
    if (friendlyName != null) {
        config.put(AttributeStatementHelper.FRIENDLY_NAME, friendlyName);
    }
    if (nameFormat != null) {
        config.put(AttributeStatementHelper.SAML_ATTRIBUTE_NAMEFORMAT, nameFormat);
    }
    config.put(SINGLE_ROLE_ATTRIBUTE, Boolean.toString(singleAttribute));
    mapper.setConfig(config);

    return mapper;
}
 
Example 6
Source File: HardcodedClaim.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ProtocolMapperModel create(String name,
                                  String hardcodedName,
                                  String hardcodedValue, String claimType,
                                  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<>();
    config.put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, hardcodedName);
    config.put(CLAIM_VALUE, hardcodedValue);
    config.put(OIDCAttributeMapperHelper.JSON_TYPE, claimType);
    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;
}
 
Example 7
Source File: UserSessionNoteMapper.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static ProtocolMapperModel createClaimMapper(String name,
                                                    String userSessionNote,
                                                    String tokenClaimName, String jsonType,
                                                    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<>();
    config.put(ProtocolMapperUtils.USER_SESSION_NOTE, userSessionNote);
    config.put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, tokenClaimName);
    config.put(OIDCAttributeMapperHelper.JSON_TYPE, jsonType);
    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;
}
 
Example 8
Source File: ClientScopeAdapter.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public Set<ProtocolMapperModel> getProtocolMappers() {
    Set<ProtocolMapperModel> mappings = new HashSet<ProtocolMapperModel>();
    for (ProtocolMapperEntity entity : this.entity.getProtocolMappers()) {
        ProtocolMapperModel mapping = new ProtocolMapperModel();
        mapping.setId(entity.getId());
        mapping.setName(entity.getName());
        mapping.setProtocol(entity.getProtocol());
        mapping.setProtocolMapper(entity.getProtocolMapper());
        Map<String, String> config = new HashMap<String, String>();
        if (entity.getConfig() != null) {
            config.putAll(entity.getConfig());
        }
        mapping.setConfig(config);
        mappings.add(mapping);
    }
    return mappings;
}
 
Example 9
Source File: ClientAdapter.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public Set<ProtocolMapperModel> getProtocolMappers() {
    Set<ProtocolMapperModel> mappings = new HashSet<ProtocolMapperModel>();
    for (ProtocolMapperEntity entity : this.entity.getProtocolMappers()) {
        ProtocolMapperModel mapping = new ProtocolMapperModel();
        mapping.setId(entity.getId());
        mapping.setName(entity.getName());
        mapping.setProtocol(entity.getProtocol());
        mapping.setProtocolMapper(entity.getProtocolMapper());
        Map<String, String> config = new HashMap<String, String>();
        if (entity.getConfig() != null) {
            config.putAll(entity.getConfig());
        }
        mapping.setConfig(config);
        mappings.add(mapping);
    }
    return mappings;
}
 
Example 10
Source File: ClientScopeAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected ProtocolMapperModel entityToModel(ProtocolMapperEntity entity) {
    ProtocolMapperModel mapping = new ProtocolMapperModel();
    mapping.setId(entity.getId());
    mapping.setName(entity.getName());
    mapping.setProtocol(entity.getProtocol());
    mapping.setProtocolMapper(entity.getProtocolMapper());
    Map<String, String> config = new HashMap<String, String>();
    if (entity.getConfig() != null) config.putAll(entity.getConfig());
    mapping.setConfig(config);
    return mapping;
}
 
Example 11
Source File: ClientAdapter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected ProtocolMapperModel entityToModel(ProtocolMapperEntity entity) {
    ProtocolMapperModel mapping = new ProtocolMapperModel();
    mapping.setId(entity.getId());
    mapping.setName(entity.getName());
    mapping.setProtocol(entity.getProtocol());
    mapping.setProtocolMapper(entity.getProtocolMapper());
    Map<String, String> config = new HashMap<String, String>();
    if (entity.getConfig() != null) config.putAll(entity.getConfig());
    mapping.setConfig(config);
    return mapping;
}
 
Example 12
Source File: RoleNameMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ProtocolMapperModel create(String name,
                                         String role,
                                         String newName) {
    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<String, String>();
    config.put(ROLE_CONFIG, role);
    config.put(NEW_ROLE_NAME, newName);
    mapper.setConfig(config);
    return mapper;

}
 
Example 13
Source File: RoleNameMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ProtocolMapperModel create(String name,
                                         String role,
                                         String newName) {
    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);
    config.put(NEW_ROLE_NAME, newName);
    mapper.setConfig(config);
    return mapper;

}
 
Example 14
Source File: HardcodedRole.java    From keycloak with Apache License 2.0 5 votes vote down vote up
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;

}
 
Example 15
Source File: UserStorageConsentTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static void setupConsent(KeycloakSession session) {
    RealmModel realm = session.realms().getRealmByName("demo");
    ClientModel product = session.realms().getClientByClientId("product-portal", realm);
    product.setConsentRequired(true);
    ClientScopeModel clientScope = realm.addClientScope("clientScope");
    clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    System.err.println("client scope protocol mappers size: " + clientScope.getProtocolMappers().size());

    for (ProtocolMapperModel mapper : product.getProtocolMappers()) {
        if (mapper.getProtocol().equals(OIDCLoginProtocol.LOGIN_PROTOCOL)) {
            if (mapper.getName().equals(OIDCLoginProtocolFactory.USERNAME)
                    || mapper.getName().equals(OIDCLoginProtocolFactory.EMAIL)
                    || mapper.getName().equals(OIDCLoginProtocolFactory.GIVEN_NAME)
                    ) {
                ProtocolMapperModel copy = new ProtocolMapperModel();
                copy.setName(mapper.getName());
                copy.setProtocol(mapper.getProtocol());
                Map<String, String> config = new HashMap<>();
                config.putAll(mapper.getConfig());
                copy.setConfig(config);
                copy.setProtocolMapper(mapper.getProtocolMapper());
                clientScope.addProtocolMapper(copy);
            }
        }
        product.removeProtocolMapper(mapper);
    }
    product.addClientScope(clientScope, true);
}
 
Example 16
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: AudienceResolveProtocolMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static ProtocolMapperModel createClaimMapper(String name) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(PROVIDER_ID);
    mapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    mapper.setConfig(Collections.emptyMap());
    return mapper;
}
 
Example 18
Source File: MigrateTo9_0_0.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected void addAccountConsoleClient(RealmModel realm) {
    if (realm.getClientByClientId(Constants.ACCOUNT_CONSOLE_CLIENT_ID) == null) {
        ClientModel client = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_CONSOLE_CLIENT_ID);
        client.setName("${client_" + Constants.ACCOUNT_CONSOLE_CLIENT_ID + "}");
        client.setEnabled(true);
        client.setFullScopeAllowed(false);
        client.setPublicClient(true);
        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);
    }
}
 
Example 19
Source File: CASAttributeMapperHelper.java    From keycloak-protocol-cas with Apache License 2.0 5 votes vote down vote up
public static ProtocolMapperModel createClaimMapper(String name,
                                                    String tokenClaimName, String claimType,
                                                    String mapperId) {
    ProtocolMapperModel mapper = new ProtocolMapperModel();
    mapper.setName(name);
    mapper.setProtocolMapper(mapperId);
    mapper.setProtocol(CASLoginProtocol.LOGIN_PROTOCOL);
    Map<String, String> config = new HashMap<String, String>();
    config.put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, tokenClaimName);
    config.put(OIDCAttributeMapperHelper.JSON_TYPE, claimType);
    mapper.setConfig(config);
    return mapper;
}
 
Example 20
Source File: RealmManager.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private void setupAccountManagement(RealmModel realm) {
    ClientModel accountClient = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
    if (accountClient == null) {
        accountClient = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
        accountClient.setName("${client_" + Constants.ACCOUNT_MANAGEMENT_CLIENT_ID + "}");
        accountClient.setEnabled(true);
        accountClient.setAlwaysDisplayInConsole(false);
        accountClient.setFullScopeAllowed(false);

        accountClient.setRootUrl(Constants.AUTH_BASE_URL_PROP);
        String baseUrl = "/realms/" + realm.getName() + "/account/";
        accountClient.setBaseUrl(baseUrl);
        accountClient.addRedirectUri(baseUrl + "*");

        accountClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);

        for (String role : AccountRoles.ALL) {
            accountClient.addDefaultRole(role);
            RoleModel roleModel = accountClient.getRole(role);
            roleModel.setDescription("${role_" + role + "}");
        }
        RoleModel manageAccountLinks = accountClient.addRole(AccountRoles.MANAGE_ACCOUNT_LINKS);
        manageAccountLinks.setDescription("${role_" + AccountRoles.MANAGE_ACCOUNT_LINKS + "}");
        RoleModel manageAccount = accountClient.getRole(AccountRoles.MANAGE_ACCOUNT);
        manageAccount.addCompositeRole(manageAccountLinks);
        RoleModel viewAppRole = accountClient.addRole(AccountRoles.VIEW_APPLICATIONS);
        viewAppRole.setDescription("${role_" + AccountRoles.VIEW_APPLICATIONS + "}");
        RoleModel viewConsentRole = accountClient.addRole(AccountRoles.VIEW_CONSENT);
        viewConsentRole.setDescription("${role_" + AccountRoles.VIEW_CONSENT + "}");
        RoleModel manageConsentRole = accountClient.addRole(AccountRoles.MANAGE_CONSENT);
        manageConsentRole.setDescription("${role_" + AccountRoles.MANAGE_CONSENT + "}");
        manageConsentRole.addCompositeRole(viewConsentRole);

        ClientModel accountConsoleClient = realm.getClientByClientId(Constants.ACCOUNT_CONSOLE_CLIENT_ID);
        if (accountConsoleClient == null) {
            accountConsoleClient = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_CONSOLE_CLIENT_ID);
            accountConsoleClient.setName("${client_" + Constants.ACCOUNT_CONSOLE_CLIENT_ID + "}");
            accountConsoleClient.setEnabled(true);
            accountConsoleClient.setAlwaysDisplayInConsole(false);
            accountConsoleClient.setFullScopeAllowed(false);
            accountConsoleClient.setPublicClient(true);
            accountConsoleClient.setDirectAccessGrantsEnabled(false);

            accountConsoleClient.setRootUrl(Constants.AUTH_BASE_URL_PROP);
            accountConsoleClient.setBaseUrl(baseUrl);
            accountConsoleClient.addRedirectUri(baseUrl + "*");

            accountConsoleClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);

            accountConsoleClient.addScopeMapping(accountClient.getRole(AccountRoles.MANAGE_ACCOUNT));

            ProtocolMapperModel audienceMapper = new ProtocolMapperModel();
            audienceMapper.setName(OIDCLoginProtocolFactory.AUDIENCE_RESOLVE);
            audienceMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
            audienceMapper.setProtocolMapper(AudienceResolveProtocolMapper.PROVIDER_ID);

            accountConsoleClient.addProtocolMapper(audienceMapper);

            accountConsoleClient.setAttribute(OIDCConfigAttributes.PKCE_CODE_CHALLENGE_METHOD, "S256");
        }
    }
}