Search in sources :

Example 6 with AfterBeanDiscovery

use of javax.enterprise.inject.spi.AfterBeanDiscovery in project tomee by apache.

the class MPJWTCDIExtension method registerClaimProducer.

public void registerClaimProducer(@Observes final AfterBeanDiscovery abd, final BeanManager bm) {
    final Set<Type> types = injectionPoints.stream().filter(NOT_PROVIDERS).filter(NOT_INSTANCES).map(ip -> REPLACED_TYPES.getOrDefault(ip.getType(), ip.getType())).collect(Collectors.<Type>toSet());
    final Set<Type> providerTypes = injectionPoints.stream().filter(NOT_PROVIDERS.negate()).map(ip -> ((ParameterizedType) ip.getType()).getActualTypeArguments()[0]).collect(Collectors.<Type>toSet());
    final Set<Type> instanceTypes = injectionPoints.stream().filter(NOT_INSTANCES.negate()).map(ip -> ((ParameterizedType) ip.getType()).getActualTypeArguments()[0]).collect(Collectors.<Type>toSet());
    types.addAll(providerTypes);
    types.addAll(instanceTypes);
    types.stream().map(type -> new ClaimBean<>(bm, type)).forEach((Consumer<ClaimBean>) abd::addBean);
    abd.addBean().id(MPJWTCDIExtension.class.getName() + "#" + JsonWebToken.class.getName()).beanClass(JsonWebToken.class).types(JsonWebToken.class, Object.class).qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE).scope(Dependent.class).createWith(ctx -> {
        final Principal principal = getContextualReference(Principal.class, bm);
        if (JsonWebToken.class.isInstance(principal)) {
            return JsonWebToken.class.cast(principal);
        }
        return null;
    });
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Provider(javax.inject.Provider) BValInterceptor(org.apache.bval.cdi.BValInterceptor) HashMap(java.util.HashMap) CreationalContext(javax.enterprise.context.spi.CreationalContext) HashSet(java.util.HashSet) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) JWTAuthConfigurationProperties(org.apache.tomee.microprofile.jwt.config.JWTAuthConfigurationProperties) Observes(javax.enterprise.event.Observes) Map(java.util.Map) Any(javax.enterprise.inject.Any) Instance(javax.enterprise.inject.Instance) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) Default(javax.enterprise.inject.Default) MPJWTInitializer(org.apache.tomee.microprofile.jwt.MPJWTInitializer) Extension(javax.enterprise.inject.spi.Extension) Predicate(java.util.function.Predicate) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) Claim(org.eclipse.microprofile.jwt.Claim) Set(java.util.Set) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Principal(java.security.Principal) ParameterizedType(java.lang.reflect.ParameterizedType) Dependent(javax.enterprise.context.Dependent) Type(java.lang.reflect.Type) MPJWTFilter(org.apache.tomee.microprofile.jwt.MPJWTFilter) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Bean(javax.enterprise.inject.spi.Bean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) BeanManager(javax.enterprise.inject.spi.BeanManager) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Dependent(javax.enterprise.context.Dependent) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Principal(java.security.Principal)

Example 7 with AfterBeanDiscovery

use of javax.enterprise.inject.spi.AfterBeanDiscovery in project Payara by payara.

the class CdiInitEventHandler method installAuthenticationMechanism.

public static void installAuthenticationMechanism(AfterBeanDiscovery afterBeanDiscovery) {
    afterBeanDiscovery.addBean(new CdiProducer<IdentityStore>().scope(ApplicationScoped.class).beanClass(IdentityStore.class).types(Object.class, IdentityStore.class, SignedJWTIdentityStore.class).addToId("store " + LoginConfig.class).create(e -> new SignedJWTIdentityStore()));
    afterBeanDiscovery.addBean(new CdiProducer<HttpAuthenticationMechanism>().scope(ApplicationScoped.class).beanClass(HttpAuthenticationMechanism.class).types(Object.class, HttpAuthenticationMechanism.class, JWTAuthenticationMechanism.class).addToId("mechanism " + LoginConfig.class).create(e -> new JWTAuthenticationMechanism()));
    // MP-JWT 1.0 7.1.1. Injection of JsonWebToken
    afterBeanDiscovery.addBean(new CdiProducer<JsonWebToken>().scope(RequestScoped.class).beanClass(JsonWebToken.class).types(Object.class, JsonWebToken.class).addToId("token " + LoginConfig.class).create(e -> getJsonWebToken()));
    // MP-JWT 1.0 7.1.2
    for (JWTInjectableType injectableType : computeTypes()) {
        // Add a new Bean<T>/Dynamic producer for each type that 7.1.2 asks us to support.
        afterBeanDiscovery.addBean(new CdiProducer<Object>().scope(Dependent.class).beanClass(CdiInitEventHandler.class).types(injectableType.getFullType()).qualifiers(new ClaimAnnotationLiteral()).addToId("claim for " + injectableType.getFullType()).create(creationalContext -> {
            // Get the qualifier from the injection point
            Claim claim = getQualifier(getCurrentInjectionPoint(CdiUtils.getBeanManager(), creationalContext), Claim.class);
            String claimName = getClaimName(claim);
            Function<String, Object> claimValueSupplier = (String claimNameParam) -> {
                return loadClaimObject(injectableType, claimNameParam);
            };
            Object claimObj;
            if (injectableType.isClaimValue()) {
                // If the target type has a ClaimValue in it, wrap the converted value
                // into a ClaimValue, e.g. ClaimValue<Long> or ClaimValue<Optional<Long>>
                claimObj = new ClaimValueImpl<>(claimName, claimValueSupplier);
            } else {
                // otherwise simply return the value
                claimObj = claimValueSupplier.apply(claimName);
            }
            return claimObj;
        }));
    }
}
Also used : Arrays(java.util.Arrays) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) ClaimValue(org.eclipse.microprofile.jwt.ClaimValue) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) ClaimValueImpl(fish.payara.microprofile.jwtauth.jwt.ClaimValueImpl) CdiProducer(org.glassfish.soteria.cdi.CdiProducer) Function(java.util.function.Function) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) HashSet(java.util.HashSet) CreationalContext(javax.enterprise.context.spi.CreationalContext) JsonValue(javax.json.JsonValue) JsonStructure(javax.json.JsonStructure) SecurityContext(javax.security.enterprise.SecurityContext) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) JsonNumber(javax.json.JsonNumber) JWTAuthenticationMechanism(fish.payara.microprofile.jwtauth.eesecurity.JWTAuthenticationMechanism) ClaimAnnotationLiteral(fish.payara.microprofile.jwtauth.jwt.ClaimAnnotationLiteral) HttpAuthenticationMechanism(javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism) Collectors.toSet(java.util.stream.Collectors.toSet) JsonObject(javax.json.JsonObject) JsonArray(javax.json.JsonArray) Set(java.util.Set) Claim(org.eclipse.microprofile.jwt.Claim) JsonWebTokenImpl(fish.payara.microprofile.jwtauth.jwt.JsonWebTokenImpl) JsonString(javax.json.JsonString) Dependent(javax.enterprise.context.Dependent) RequestScoped(javax.enterprise.context.RequestScoped) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JWTInjectableType(fish.payara.microprofile.jwtauth.jwt.JWTInjectableType) Collections(java.util.Collections) Bean(javax.enterprise.inject.spi.Bean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) CdiUtils(org.glassfish.soteria.cdi.CdiUtils) BeanManager(javax.enterprise.inject.spi.BeanManager) HttpAuthenticationMechanism(javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism) RequestScoped(javax.enterprise.context.RequestScoped) ClaimAnnotationLiteral(fish.payara.microprofile.jwtauth.jwt.ClaimAnnotationLiteral) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) JsonString(javax.json.JsonString) ClaimValueImpl(fish.payara.microprofile.jwtauth.jwt.ClaimValueImpl) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JWTInjectableType(fish.payara.microprofile.jwtauth.jwt.JWTInjectableType) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Function(java.util.function.Function) CdiProducer(org.glassfish.soteria.cdi.CdiProducer) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) JsonObject(javax.json.JsonObject) JWTAuthenticationMechanism(fish.payara.microprofile.jwtauth.eesecurity.JWTAuthenticationMechanism) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) Claim(org.eclipse.microprofile.jwt.Claim)

Example 8 with AfterBeanDiscovery

use of javax.enterprise.inject.spi.AfterBeanDiscovery in project Payara by payara.

the class JwtAuthCdiExtension method installMechanismIfNeeded.

public void installMechanismIfNeeded(@Observes AfterBeanDiscovery eventIn, BeanManager beanManager) {
    // JDK8 u60 workaround
    AfterBeanDiscovery afterBeanDiscovery = eventIn;
    if (addJWTAuthenticationMechanism) {
        validateConfigValue();
        CdiInitEventHandler.installAuthenticationMechanism(afterBeanDiscovery);
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery)

Example 9 with AfterBeanDiscovery

use of javax.enterprise.inject.spi.AfterBeanDiscovery in project wildfly-swarm by wildfly-swarm.

the class SocketBindingGroupExtension method afterBeanDiscovery.

@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    List<SimpleKey> configuredGroups = this.configView.simpleSubkeys(ROOT);
    for (SimpleKey groupName : configuredGroups) {
        Set<Bean<?>> groups = beanManager.getBeans(SocketBindingGroup.class, AnyLiteral.INSTANCE);
        AtomicBoolean producerRequired = new AtomicBoolean(false);
        if (groups.stream().noneMatch(e -> e.getQualifiers().stream().anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(groupName)))) {
            SocketBindingGroup group = new SocketBindingGroup(groupName.name(), null, "0");
            applyConfiguration(group);
            if (producerRequired.get()) {
                CommonBean<SocketBindingGroup> interfaceBean = CommonBeanBuilder.newBuilder(SocketBindingGroup.class).beanClass(SocketBindingGroupExtension.class).scope(ApplicationScoped.class).addQualifier(AnyLiteral.INSTANCE).addQualifier(new NamedLiteral(group.name())).createSupplier(() -> group).addType(SocketBindingGroup.class).addType(Object.class).build();
                abd.addBean(interfaceBean);
            }
        }
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnyLiteral(org.jboss.weld.literal.AnyLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) List(java.util.List) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) Observes(javax.enterprise.event.Observes) NamedLiteral(org.jboss.weld.literal.NamedLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Named(javax.inject.Named) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) CommonBeanBuilder(org.wildfly.swarm.spi.api.cdi.CommonBeanBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Named(javax.inject.Named) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) NamedLiteral(org.jboss.weld.literal.NamedLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)9 BeanManager (javax.enterprise.inject.spi.BeanManager)5 Set (java.util.Set)4 Bean (javax.enterprise.inject.spi.Bean)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 RequestScoped (javax.enterprise.context.RequestScoped)3 Observes (javax.enterprise.event.Observes)3 BeforeBeanDiscovery (javax.enterprise.inject.spi.BeforeBeanDiscovery)3 Extension (javax.enterprise.inject.spi.Extension)3 ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)3 ProcessInjectionPoint (javax.enterprise.inject.spi.ProcessInjectionPoint)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Dependent (javax.enterprise.context.Dependent)2 CreationalContext (javax.enterprise.context.spi.CreationalContext)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)2 Claim (org.eclipse.microprofile.jwt.Claim)2 JsonWebToken (org.eclipse.microprofile.jwt.JsonWebToken)2