001/*
002 * Copyright 2026 Revetware LLC.
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
017package com.revetsec.oidc;
018
019import com.revetsec.internal.encoding.FormUrlEncoding;
020import com.revetsec.internal.encoding.EncodingException;
021import org.jspecify.annotations.NonNull;
022import javax.annotation.concurrent.Immutable;
023import java.net.URI;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.Map;
027
028/**
029 * A prepared browser request, not provider-session termination or authenticated identity. GET and form POST
030 * getters deliberately emit the retained ID-token credential; never log them. Local session termination and
031 * POST/CSRF authorization precede preparation. On the store path deliver only after pending save reports SAVED.
032 * No server request to the logout endpoint or automatic retry occurs. The 128 KiB representation ceiling is not a
033 * promise that every browser/provider accepts maximal GET URLs.
034 * @author <a href="https://www.revetkn.com">Mark Allen</a>
035 * @since 1.0.0
036 */
037@Immutable
038public final class OidcLogoutRequest {
039        private final @NonNull URI redirectUri, action;
040        private final @NonNull Map<@NonNull String, @NonNull String> parameters;
041        private final @NonNull String body;
042        private final @NonNull PendingOidcLogout pending;
043        OidcLogoutRequest(@NonNull URI endpoint, @NonNull String clientId, @NonNull URI callback, @NonNull String compact, @NonNull String state, @NonNull PendingOidcLogout pending) {
044                Map<String, String> values = new LinkedHashMap<>(); values.put("id_token_hint", compact); values.put("client_id", clientId); values.put("post_logout_redirect_uri", callback.toASCIIString()); values.put("state", state);
045                try {
046                        StringBuilder form = new StringBuilder();
047                        for (Map.Entry<String, String> field : values.entrySet()) {
048                                String name = FormUrlEncoding.encode(field.getKey()), value = FormUrlEncoding.encode(field.getValue());
049                                if ((long) form.length() + name.length() + value.length() + 2 > OidcLogoutSupport.REQUEST_LIMIT) throw OidcLogoutSupport.failure(OidcLogoutException.Reason.INPUT_LIMIT);
050                                if (!form.isEmpty()) form.append('&'); form.append(name).append('=').append(value);
051                        }
052                        this.body = form.toString(); String base = endpoint.toASCIIString();
053                        if ((long) base.length() + this.body.length() + 1 > OidcLogoutSupport.REQUEST_LIMIT) throw OidcLogoutSupport.failure(OidcLogoutException.Reason.INPUT_LIMIT);
054                        this.redirectUri = URI.create(base + (endpoint.getRawQuery() == null ? "?" : endpoint.getRawQuery().isEmpty() ? "" : "&") + this.body);
055                } catch (EncodingException invalid) { throw OidcLogoutSupport.failure(OidcLogoutException.Reason.INPUT_LIMIT); }
056                this.action = endpoint; this.parameters = Collections.unmodifiableMap(new LinkedHashMap<>(values)); this.pending = pending;
057        }
058        /** Explicitly emits a credential-bearing GET target.
059         * @return browser redirect URI; never log it
060         * @since 1.0.0
061         */
062        public @NonNull URI getRedirectUri() { return this.redirectUri; }
063        /** Returns the trusted form action, preserving allowed provider query parameters.
064         * @return form POST action
065         * @since 1.0.0
066         */
067        public @NonNull URI getFormAction() { return this.action; }
068        /** Explicitly emits immutable credential-bearing form parameters.
069         * @return form parameters; never log them
070         * @since 1.0.0
071         */
072        public @NonNull Map<@NonNull String, @NonNull String> getFormParameters() { return this.parameters; }
073        /** Explicitly emits the UTF8 application/x-www-form-urlencoded credential-bearing form body.
074         * @return browser POST body; never log it
075         * @since 1.0.0
076         */
077        public @NonNull String toFormBody() { return this.body; }
078        /** Returns the independent correlation record to save or seal before browser delivery.
079         * @return pending logout state, not authentication proof
080         * @since 1.0.0
081         */
082        public @NonNull PendingOidcLogout getPending() { return this.pending; }
083        /** Redacts all credential and state fields.
084         * @return redacted description
085         * @since 1.0.0
086         */
087        @Override public @NonNull String toString() { return "OidcLogoutRequest{credential=<redacted>, correlation=<redacted>}"; }
088}