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

io.helidon.microprofile.jwt.auth.ClaimProducer Maven / Gradle / Ivy

There is a newer version: 4.1.4
Show newest version
/*
 * Copyright (c) 2018, 2023 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.microprofile.jwt.auth;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.Set;

import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.inject.Provider;
import org.eclipse.microprofile.jwt.Claim;
import org.eclipse.microprofile.jwt.Claims;

/**
 * Class ClaimProducer.
 */
class ClaimProducer implements Bean {
    private static final Annotation QUALIFIER = new Claim() {
        @Override
        public String value() {
            return "";
        }

        @Override
        public Claims standard() {
            return Claims.UNKNOWN;
        }

        @Override
        public Class annotationType() {
            return Claim.class;
        }
    };

    private final JwtAuthCdiExtension.MpClaimQualifier qualifier;
    private final Type type;
    private final Class scope;

    ClaimProducer(JwtAuthCdiExtension.MpClaimQualifier q, Type type, Class scope) {
        this.qualifier = q;
        this.scope = scope;
        Type actualType = type;
        if (type instanceof ParameterizedType) {
            ParameterizedType paramType = (ParameterizedType) type;
            if (Provider.class.equals(paramType.getRawType())
                    || Instance.class.equals(paramType.getRawType())) {
                actualType = paramType.getActualTypeArguments()[0];
            }
        }

        this.type = actualType;
    }

    static Object getClaimValue(String claimName,
                                JsonWebTokenImpl webToken,
                                JwtAuthCdiExtension.MpClaimQualifier q) {
        return getParametrizedClaimValue(claimName,
                webToken,
                q);
    }

    @SuppressWarnings("unchecked")
    static Object getParametrizedClaimValue(String claimName,
                                            JsonWebTokenImpl webToken,
                                            JwtAuthCdiExtension.MpClaimQualifier claimLiteral) {

        if (null == webToken) {
            // not in MP-JWT scope
            return null;
        }

        Object result;
        if (claimLiteral.claimValue()) {
            if (claimLiteral.optional()) {
                result = webToken.getClaim(claimName, claimLiteral.typeArg2());
            } else {
                result = webToken.getClaim(claimName, claimLiteral.typeArg());
            }
        } else if (claimLiteral.optional()) {
            result = webToken.getClaim(claimName, claimLiteral.typeArg());
        } else {
            result = webToken.getClaim(claimName, claimLiteral.rawType());
        }

        if (claimLiteral.optional()) result = Optional.ofNullable(result);
        if (claimLiteral.claimValue()) result = new ClaimValueWrapper<>(claimName, result);
        return result;
    }

    @Override
    public Object create(CreationalContext context) {
        return getClaimValue(context);
    }

    private Object getClaimValue(CreationalContext context) {
        JsonWebTokenImpl token = CDI.current().select(JsonWebTokenImpl.class, new Impl() {
            @Override
            public Class annotationType() {
                return Impl.class;
            }
        }).get();
        return getClaimValue(qualifier.name(), token, qualifier);
    }

    @Override
    public Class getBeanClass() {
        return ClaimProducer.class;
    }

    @Override
    public Set getInjectionPoints() {
        return Set.of();
    }

    @Override
    public void destroy(Object instance, CreationalContext creationalContext) {

    }

    @Override
    public Set getTypes() {
        return Set.of(type);
    }

    @Override
    public Set getQualifiers() {
        return Set.of(qualifier, QUALIFIER);
    }

    @Override
    public Class getScope() {
        return scope;
    }

    @Override
    public String getName() {
        return qualifier.id();
    }

    @Override
    public Set> getStereotypes() {
        return Set.of();
    }

    @Override
    public boolean isAlternative() {
        return false;
    }

    @Override
    public String toString() {
        return "ClaimProducer{"
                + "qualifier=" + qualifier
                + '}';
    }
}