All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.am.gateway.handler.common.utils.ThymeleafDataHelper Maven / Gradle / Ivy

/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.am.gateway.handler.common.utils;

import io.gravitee.am.gateway.handler.common.vertx.core.http.VertxHttpServerRequest;
import io.gravitee.am.gateway.handler.context.EvaluableRequest;
import io.gravitee.am.model.Domain;
import io.gravitee.am.model.User;
import io.gravitee.am.model.oidc.Client;
import io.gravitee.am.model.safe.ClientProperties;
import io.gravitee.am.model.safe.DomainProperties;
import io.gravitee.am.model.safe.UserProperties;
import io.vertx.rxjava3.core.MultiMap;
import io.vertx.rxjava3.ext.web.RoutingContext;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.jsoup.internal.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.gravitee.am.common.utils.ConstantKeys.CLIENT_CONTEXT_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.DOMAIN_CONTEXT_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.ERROR_CODE_PARAM_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.ERROR_DESCRIPTION_PARAM_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.ERROR_HASH;
import static io.gravitee.am.common.utils.ConstantKeys.ERROR_PARAM_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.PARAM_CONTEXT_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.REQUEST_CONTEXT_KEY;
import static io.gravitee.am.common.utils.ConstantKeys.SERVER_ERROR;
import static io.gravitee.am.common.utils.ConstantKeys.USER_CONTEXT_KEY;

/**
 * @author Eric LELEU (eric.leleu at graviteesource.com)
 * @author GraviteeSource Team
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ThymeleafDataHelper {

    public static Map generateData(RoutingContext context, Domain domain, Client client) {
        final Map data = new HashMap<>(context.data());
        if (domain != null) {
            data.put(DOMAIN_CONTEXT_KEY, new DomainProperties(domain));
        }
        if (client != null) {
            data.put(CLIENT_CONTEXT_KEY, new ClientProperties(client));
        }
        getUser(context).ifPresent(userProperties -> data.put(USER_CONTEXT_KEY, userProperties));

        // Put evaluable request and param entry to get simple access to request object and query parameters
        // we use putIfAbsent because the Endpoint may have initialized these
        // entries before the call of this generateData method
        EvaluableRequest evaluableRequest = new EvaluableRequest(new VertxHttpServerRequest(context.request().getDelegate(), true));
        data.putIfAbsent(REQUEST_CONTEXT_KEY, evaluableRequest);
        final Map parameters = Optional.ofNullable((Map) data.get(PARAM_CONTEXT_KEY)).orElse(new HashMap<>());
        for (var entry : evaluableRequest.getParams().toSingleValueMap().entrySet()) {
            parameters.putIfAbsent(entry.getKey(), entry.getValue());
        }
        data.put(PARAM_CONTEXT_KEY, parameters);
        if (context.session() != null && context.getDelegate() != null) {
            MultiMap queryParams = context.queryParams();
            String errorHash = context.session().get(ERROR_HASH);
            String error = queryParams.get(ERROR_PARAM_KEY);
            String errorCode = queryParams.get(ERROR_CODE_PARAM_KEY);
            String errorDescription = queryParams.get(ERROR_DESCRIPTION_PARAM_KEY);
            if (errorHash != null) {
                StringBuilder errorSB = new StringBuilder();
                if (error != null) {
                    data.put(ERROR_PARAM_KEY, queryParams.get(ERROR_PARAM_KEY));
                    errorSB.append(queryParams.get(ERROR_PARAM_KEY));
                }
                if (errorCode != null) {
                    data.put(ERROR_CODE_PARAM_KEY, queryParams.get(ERROR_CODE_PARAM_KEY));
                }
                if (errorDescription != null) {
                    data.put(ERROR_DESCRIPTION_PARAM_KEY, queryParams.get(ERROR_DESCRIPTION_PARAM_KEY));
                    errorSB.append("$");
                    errorSB.append(queryParams.get(ERROR_DESCRIPTION_PARAM_KEY));
                }
                if (!StringUtil.isBlank(errorSB.toString()) && !HashUtil.compare(errorHash, errorSB.toString())) {
                    setServerError(data);
                }
            } else if (error != null || errorCode != null || errorDescription != null) {
                setServerError(data);
            }
        }


        return data;
    }

    private static void setServerError(Map data) {
        data.put(ERROR_PARAM_KEY, SERVER_ERROR);
        data.put(ERROR_CODE_PARAM_KEY, null);
        data.put(ERROR_DESCRIPTION_PARAM_KEY, "Unknown error occurred");
    }

    private static Optional getUser(RoutingContext context) {
        Object user = context.get(USER_CONTEXT_KEY); // context may contain User or UserProperties according to the execution path
        Optional mayHaveUser = Optional.empty();
        User authUser;
        if (user instanceof User) {
            authUser = (User) user;
            mayHaveUser = Optional.of(new UserProperties(authUser, false));
        } else if (context.user() != null) {
            authUser = ((io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User) context.user().getDelegate()).getUser();
            mayHaveUser = Optional.of(new UserProperties(authUser, false));
        }
        return mayHaveUser;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy