'Keycloak add extra claims from database / external source with custom protocol mapper not woking

//////////////////////////////////////////////////////////// Not showing in keycloak Create custom mappaer type list how to resolve and identify provlem
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    public class CustomOIDCProtocolMapper extends AbstractOIDCProtocolMapper implements OIDCAccessTokenMapper, OIDCIDTokenMapper, UserInfoTokenMapper {
    public static final String PROVIDER_ID = "oidc-customprotocolmapper";
    private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
    /**
         * Maybe you want to have config fields for your Mapper
         */
        /*
        static {
            ProviderConfigProperty property;
            property = new ProviderConfigProperty();
            property.setName(ProtocolMapperUtils.USER_ATTRIBUTE);
            property.setLabel(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_LABEL);
            property.setHelpText(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_HELP_TEXT);
            property.setType(ProviderConfigProperty.STRING_TYPE);
            configProperties.add(property);
    property = new ProviderConfigProperty();
            property.setName(ProtocolMapperUtils.MULTIVALUED);
            property.setLabel(ProtocolMapperUtils.MULTIVALUED_LABEL);
            property.setHelpText(ProtocolMapperUtils.MULTIVALUED_HELP_TEXT);
            property.setType(ProviderConfigProperty.BOOLEAN_TYPE);
            configProperties.add(property);
        }
         */
        @Override
        public List<ProviderConfigProperty> getConfigProperties() {
            return configProperties;
        }
    @Override
        public String getDisplayCategory() {
            return TOKEN_MAPPER_CATEGORY;
        }
    @Override
        public String getDisplayType() {
            return "Custom Claim Mapper";
        }
    @Override
        public String getId() {
            return PROVIDER_ID;
        }
    @Override
        public String getHelpText() {
            return "some help text";
        }
    public AccessToken transformAccessToken(AccessToken token, ProtocolMapperModel mappingModel, KeycloakSession keycloakSession,
                                                UserSessionModel userSession, ClientSessionContext clientSessionCtx) {
    token.getOtherClaims().put("custom_claim_name", "custom_claim_value");
    setClaim(token, mappingModel, userSession, keycloakSession, clientSessionCtx);
            return token;
        }
    public static ProtocolMapperModel create(String name,
                                                 boolean accessToken, boolean idToken, boolean userInfo) {
            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.INCLUDE_IN_ACCESS_TOKEN, "true");
            config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "true");
            mapper.setConfig(config);
            return mapper;
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source