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.soklet;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import com.revetsec.BrowserRequestOriginPolicy;
021import com.revetsec.BrowserRequestOriginResult;
022import com.soklet.Request;
023import org.jspecify.annotations.NonNull;
024
025import javax.annotation.concurrent.ThreadSafe;
026
027import static java.util.Objects.requireNonNull;
028
029/**
030 * Soklet request extraction for Revetsec's browser request-origin policy.
031 * <p>
032 * The helper installs no filter and changes no application state. Soklet exposes materialized header values; if a
033 * trusted HTTP edge collapses physically identical duplicate fields before Soklet receives them, that edge must
034 * reject the duplicates itself.
035 *
036 * @since 1.0.0
037 */
038@ThreadSafe
039public final class SokletBrowserRequestOrigins {
040    private SokletBrowserRequestOrigins() { }
041
042    /**
043     * Checks the request method and all materialized {@code Sec-Fetch-Site} and {@code Origin} values.
044     *
045     * @param policy configured core policy
046     * @param request routed request
047     * @return closed request-origin outcome
048     * @throws NullPointerException if an argument is {@code null}
049     * @since 1.0.0
050     */
051    @CheckReturnValue
052    public static @NonNull BrowserRequestOriginResult browserRequestOriginResultFor(
053            @NonNull BrowserRequestOriginPolicy policy, @NonNull Request request) {
054        requireNonNull(request);
055        return requireNonNull(policy).checkResult(request.getHttpMethod().name(),
056                SokletOAuth.collectHeaderValues(request, "Sec-Fetch-Site"),
057                SokletOAuth.collectHeaderValues(request, "Origin"));
058    }
059}