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.saml;
018
019import org.jspecify.annotations.NonNull;
020import javax.annotation.concurrent.Immutable;
021
022/**
023 * The outcome of parsing the SAML HTTP-POST binding. Accepted bytes are still untrusted.
024 *
025 * @since 1.0.0
026 */
027@Immutable
028public sealed interface SamlPostBindingParseResult permits SamlPostBindingParseResult.Parsed,
029        SamlPostBindingParseResult.Rejected {
030    /**
031     * An opaque message whose XML has not yet been parsed or verified.
032     *
033     * @since 1.0.0
034     */
035    @Immutable
036    final class Parsed implements SamlPostBindingParseResult {
037        private final @NonNull SamlPostBindingMessage message;
038        Parsed(@NonNull SamlPostBindingMessage message) { this.message = message; }
039        /**
040         * Returns the opaque untrusted message.
041         *
042         * @return the message
043         * @since 1.0.0
044         */
045        public @NonNull SamlPostBindingMessage getMessage() { return message; }
046        @Override public @NonNull String toString() { return "SamlPostBindingParseResult.Parsed{<untrusted>}"; }
047    }
048
049    /**
050     * A fixed local rejection; no portion of the submitted message is included.
051     *
052     * @since 1.0.0
053     */
054    @Immutable
055    final class Rejected implements SamlPostBindingParseResult {
056        private final @NonNull Reason reason;
057        Rejected(@NonNull Reason reason) { this.reason = reason; }
058        /**
059         * Returns a fixed binding rejection reason.
060         *
061         * @return the reason
062         * @since 1.0.0
063         */
064        public @NonNull Reason getReason() { return reason; }
065        @Override public @NonNull String toString() { return "SamlPostBindingParseResult.Rejected{" + reason + "}"; }
066    }
067
068    /**
069     * Bounded input and framing failures.
070     *
071     * @since 1.0.0
072     */
073    @Immutable
074    enum Reason {
075        /** The form body is empty or exceeds its cap. */ BODY_SIZE,
076        /** The Content-Type fields are missing, repeated or unsupported. */ CONTENT_TYPE,
077        /** The query string conflicts with the POST binding. */ QUERY,
078        /** Required parameters are missing, repeated or unexpected. */ PARAMETERS,
079        /** A form component is not valid UTF-8 or percent encoding. */ FORM_ENCODING,
080        /** RelayState exceeds the binding cap. */ RELAY_STATE_SIZE,
081        /** The SAML payload violates canonical standard Base64 or its whitespace allowance. */ BASE64,
082        /** The encoded payload or decoded XML exceeds its cap, or decoded XML is empty. */ XML_SIZE
083    }
084}