001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     François Maturel
018 */
019
020package org.nuxeo.ecm.platform.ui.web.keycloak;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.lang.reflect.Constructor;
025import java.lang.reflect.InvocationTargetException;
026import java.lang.reflect.Method;
027
028import com.fasterxml.jackson.databind.ObjectMapper;
029
030import org.keycloak.adapters.KeycloakDeployment;
031import org.keycloak.adapters.KeycloakDeploymentBuilder;
032import org.keycloak.representations.adapters.config.AdapterConfig;
033import org.keycloak.util.SystemPropertiesJsonParserFactory;
034
035/**
036 * This class is developed to overcome a Jackson version problem between Nuxeo and Keycloak.<br>
037 * Nuxeo uses Jackson version 1.8.x where Keycloak uses 1.9.x<br>
038 * Sadly the {@link ObjectMapper#setSerializationInclusion} method is not in 1.8.x<br>
039 * Then {@link KeycloakNuxeoDeployment} is the same class as {@link KeycloakDeploymentBuilder}, rewriting static method
040 * {@link KeycloakDeploymentBuilder#loadAdapterConfig} to avoid the use of
041 * {@link ObjectMapper#setSerializationInclusion}
042 *
043 * @since 7.4
044 */
045public class KeycloakNuxeoDeployment {
046
047    /**
048     * Invokes KeycloakDeploymentBuilder.internalBuild with reflection to avoid rewriting source code
049     *
050     * @param is the configuration file {@link InputStream}
051     * @return the {@link KeycloakDeployment} corresponding to the configuration file
052     */
053    public static KeycloakDeployment build(InputStream is) {
054        AdapterConfig adapterConfig = loadAdapterConfig(is);
055
056        try {
057            Constructor<KeycloakDeploymentBuilder> constructor = KeycloakDeploymentBuilder.class.getDeclaredConstructor();
058            constructor.setAccessible(true);
059            KeycloakDeploymentBuilder builder = constructor.newInstance();
060
061            Method method = KeycloakDeploymentBuilder.class.getDeclaredMethod("internalBuild", AdapterConfig.class);
062            method.setAccessible(true);
063            return (KeycloakDeployment) method.invoke(builder, adapterConfig);
064        } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
065            throw new RuntimeException(e);
066        }
067    }
068
069    public static AdapterConfig loadAdapterConfig(InputStream is) {
070        ObjectMapper mapper = new ObjectMapper(new SystemPropertiesJsonParserFactory());
071        AdapterConfig adapterConfig;
072        try {
073            adapterConfig = mapper.readValue(is, AdapterConfig.class);
074        } catch (IOException e) {
075            throw new RuntimeException(e);
076        }
077        return adapterConfig;
078    }
079
080}