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

io.trino.server.security.oauth2.NonceCookie Maven / Gradle / Ivy

There is a newer version: 468
Show newest version
/*
 * 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.trino.server.security.oauth2;

import jakarta.ws.rs.core.Cookie;
import jakarta.ws.rs.core.NewCookie;

import java.time.Instant;
import java.util.Date;
import java.util.Optional;

import static io.trino.server.security.oauth2.OAuth2CallbackResource.CALLBACK_ENDPOINT;
import static jakarta.ws.rs.core.Cookie.DEFAULT_VERSION;
import static jakarta.ws.rs.core.NewCookie.DEFAULT_MAX_AGE;
import static java.util.function.Predicate.not;

public final class NonceCookie
{
    // prefix according to: https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05#section-4.1.3.1
    public static final String NONCE_COOKIE = "__Secure-Trino-Nonce";

    private NonceCookie() {}

    public static NewCookie create(String nonce, Instant tokenExpiration)
    {
        return new NewCookie(
                NONCE_COOKIE,
                nonce,
                CALLBACK_ENDPOINT,
                null,
                DEFAULT_VERSION,
                null,
                DEFAULT_MAX_AGE,
                Date.from(tokenExpiration),
                true,
                true);
    }

    public static Optional read(Cookie cookie)
    {
        return Optional.ofNullable(cookie)
                .map(Cookie::getValue)
                .filter(not(String::isBlank));
    }

    public static NewCookie delete()
    {
        return new NewCookie(
                NONCE_COOKIE,
                "delete",
                CALLBACK_ENDPOINT,
                null,
                DEFAULT_VERSION,
                null,
                0,
                null,
                true,
                true);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy