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

io.helidon.security.jwt.CommonValidator Maven / Gradle / Ivy

There is a newer version: 4.1.6
Show newest version
/*
 * Copyright (c) 2024 Oracle and/or its affiliates.
 *
 * 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.helidon.security.jwt;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

abstract class CommonValidator implements ClaimValidator {

    private final JwtScope scope;
    private final Set claims;

    CommonValidator(BaseBuilder builder) {
        this.scope = builder.scope;
        this.claims = Set.copyOf(builder.claims);
    }

    @Override
    public JwtScope jwtScope() {
        return scope;
    }

    @Override
    public Set claims() {
        return claims;
    }

    abstract static class BaseBuilder, T>
            implements io.helidon.common.Builder, T> {

        private JwtScope scope = JwtScope.PAYLOAD;
        private Set claims = new HashSet<>();

        BaseBuilder() {
        }

        /**
         * The scope of JWT.
         * Default value is {@link JwtScope#PAYLOAD}.
         *
         * @param scope jwt scope
         * @return updated builder instance
         */
        B scope(JwtScope scope) {
            this.scope = Objects.requireNonNull(scope);
            return me();
        }

        /**
         * Add JWT claim this validator is bound to.
         *
         * @param claim claim name
         * @return updated builder instance
         */
        B addClaim(String claim) {
            Objects.requireNonNull(claim);
            this.claims.add(claim);
            return me();
        }

        /**
         * Add JWT claim this validator is bound to.
         *
         * @param claims bound claims
         * @return updated builder instance
         */
        B claims(Set claims) {
            Objects.requireNonNull(claims);
            this.claims = new HashSet<>(claims);
            return me();
        }

        /**
         * Clear all set claims.
         *
         * @return updated builder instance
         */
        B clearClaims() {
            this.claims.clear();
            return me();
        }

        /**
         * Currently set {@link JwtScope} scope value.
         *
         * @return scope value
         */
        JwtScope scope() {
            return scope;
        }

        @SuppressWarnings("unchecked")
        protected B me() {
            return (B) this;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy