Search in sources :

Example 11 with Claim

use of org.eclipse.microprofile.jwt.Claim in project smallrye-jwt by smallrye.

the class RawClaimTypeProducer method getClaimAsLong.

@Produces
@Claim("")
Long getClaimAsLong(InjectionPoint ip) {
    CDILogging.log.getClaimAsLong(ip);
    if (currentToken == null) {
        return null;
    }
    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    Long returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonNumber) {
            JsonNumber jsonValue = (JsonNumber) value;
            returnValue = jsonValue.longValue();
        } else {
            returnValue = Long.parseLong(value.toString());
        }
    }
    return returnValue;
}
Also used : JsonNumber(javax.json.JsonNumber) JsonString(javax.json.JsonString) Produces(javax.enterprise.inject.Produces) Claim(org.eclipse.microprofile.jwt.Claim)

Example 12 with Claim

use of org.eclipse.microprofile.jwt.Claim in project smallrye-jwt by smallrye.

the class RawClaimTypeProducer method getClaimAsString.

@Produces
@Claim("")
String getClaimAsString(InjectionPoint ip) {
    CDILogging.log.getClaimAsString(ip);
    if (currentToken == null) {
        return null;
    }
    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    String returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonString) {
            JsonString jsonValue = (JsonString) value;
            returnValue = jsonValue.getString();
        } else {
            returnValue = value.toString();
        }
    }
    return returnValue;
}
Also used : JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Produces(javax.enterprise.inject.Produces) Claim(org.eclipse.microprofile.jwt.Claim)

Example 13 with Claim

use of org.eclipse.microprofile.jwt.Claim in project smallrye-jwt by smallrye.

the class CommonJwtProducer method getName.

public String getName(InjectionPoint ip) {
    String name = null;
    for (Annotation ann : ip.getQualifiers()) {
        if (ann instanceof Claim) {
            Claim claim = (Claim) ann;
            name = claim.standard() == Claims.UNKNOWN ? claim.value() : claim.standard().name();
        }
    }
    return name;
}
Also used : Annotation(java.lang.annotation.Annotation) Claim(org.eclipse.microprofile.jwt.Claim)

Example 14 with Claim

use of org.eclipse.microprofile.jwt.Claim in project tomee by apache.

the class ClaimBean method create.

@Override
public T create(final CreationalContext<T> context) {
    logger.finest("Creating CDI Bean for type " + types.iterator().next());
    final InjectionPoint ip = (InjectionPoint) bm.getInjectableReference(new ClaimInjectionPoint(this), context);
    if (ip == null) {
        throw new IllegalStateException("Could not retrieve InjectionPoint for type " + types.iterator().next());
    }
    final Annotated annotated = ip.getAnnotated();
    final Claim claim = annotated.getAnnotation(Claim.class);
    final String key = getClaimKey(claim);
    logger.finest(String.format("Found Claim injection with name=%s and for %s", key, ip.toString()));
    if (ParameterizedType.class.isInstance(annotated.getBaseType())) {
        final ParameterizedType paramType = ParameterizedType.class.cast(annotated.getBaseType());
        final Type rawType = paramType.getRawType();
        if (Class.class.isInstance(rawType) && paramType.getActualTypeArguments().length == 1) {
            final Class<?> rawTypeClass = ((Class<?>) rawType);
            // handle Provider<T>
            if (rawTypeClass.isAssignableFrom(Provider.class)) {
                final Type providerType = paramType.getActualTypeArguments()[0];
                if (ParameterizedType.class.isInstance(providerType) && isOptional(ParameterizedType.class.cast(providerType))) {
                    return (T) Optional.ofNullable(getClaimValue(key));
                }
                return getClaimValue(key);
            }
            // handle Instance<T>
            if (rawTypeClass.isAssignableFrom(Instance.class)) {
                final Type instanceType = paramType.getActualTypeArguments()[0];
                if (ParameterizedType.class.isInstance(instanceType) && isOptional(ParameterizedType.class.cast(instanceType))) {
                    return (T) Optional.ofNullable(getClaimValue(key));
                }
                return getClaimValue(key);
            }
            // handle ClaimValue<T>
            if (rawTypeClass.isAssignableFrom(ClaimValue.class)) {
                final Type claimValueType = paramType.getActualTypeArguments()[0];
                final ClaimValueWrapper claimValueWrapper = new ClaimValueWrapper(key);
                if (ParameterizedType.class.isInstance(claimValueType) && isOptional(ParameterizedType.class.cast(claimValueType))) {
                    claimValueWrapper.setValue(() -> {
                        final T claimValue = ClaimBean.this.getClaimValue(key);
                        return Optional.ofNullable(claimValue);
                    });
                } else if (ParameterizedType.class.isInstance(claimValueType) && isSet(ParameterizedType.class.cast(claimValueType))) {
                    claimValueWrapper.setValue(() -> {
                        final T claimValue = ClaimBean.this.getClaimValue(key);
                        return claimValue;
                    });
                } else if (ParameterizedType.class.isInstance(claimValueType) && isList(ParameterizedType.class.cast(claimValueType))) {
                    claimValueWrapper.setValue(() -> {
                        final T claimValue = ClaimBean.this.getClaimValue(key);
                        return claimValue;
                    });
                } else if (Class.class.isInstance(claimValueType)) {
                    claimValueWrapper.setValue(() -> {
                        final T claimValue = ClaimBean.this.getClaimValue(key);
                        return claimValue;
                    });
                } else {
                    throw new IllegalArgumentException("Unsupported ClaimValue type " + claimValueType.toString());
                }
                return (T) claimValueWrapper;
            }
            // handle Optional<T>
            if (rawTypeClass.isAssignableFrom(Optional.class)) {
                return getClaimValue(key);
            }
            // handle Set<T>
            if (rawTypeClass.isAssignableFrom(Set.class)) {
                return getClaimValue(key);
            }
            // handle List<T>
            if (rawTypeClass.isAssignableFrom(List.class)) {
                return getClaimValue(key);
            }
        }
    } else if (annotated.getBaseType().getTypeName().startsWith("javax.json.Json")) {
        // handle JsonValue<T> (number, string, etc)
        return (T) toJson(key);
    } else if (propertyEditorRegistry.findConverter((Class<?>) ip.getType()) != null) {
        final Class<?> type = (Class<?>) ip.getType();
        try {
            final Object claimObject = getClaimValue(key);
            if (claimObject == null) {
                return null;
            }
            return (T) propertyEditorRegistry.getValue(type, String.valueOf(claimObject));
        } catch (final Exception e) {
            logger.log(Level.WARNING, String.format("Cannot convert claim %s into type %s", key, type), e);
        }
    } else {
        // handle Raw types
        return getClaimValue(key);
    }
    throw new IllegalStateException("Unhandled Claim type " + annotated.getBaseType());
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) ParameterizedType(java.lang.reflect.ParameterizedType) Annotated(javax.enterprise.inject.spi.Annotated) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonObject(javax.json.JsonObject) Claim(org.eclipse.microprofile.jwt.Claim)

Example 15 with Claim

use of org.eclipse.microprofile.jwt.Claim 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)

Aggregations

Claim (org.eclipse.microprofile.jwt.Claim)21 JsonString (javax.json.JsonString)8 Annotation (java.lang.annotation.Annotation)7 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)7 Produces (javax.enterprise.inject.Produces)6 ParameterizedType (java.lang.reflect.ParameterizedType)5 Type (java.lang.reflect.Type)5 DeploymentException (javax.enterprise.inject.spi.DeploymentException)4 ProcessInjectionPoint (javax.enterprise.inject.spi.ProcessInjectionPoint)3 JsonNumber (javax.json.JsonNumber)3 Optional (java.util.Optional)2 Set (java.util.Set)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 SessionScoped (javax.enterprise.context.SessionScoped)2 BeanAttributes (javax.enterprise.inject.spi.BeanAttributes)2 ProcessBeanAttributes (javax.enterprise.inject.spi.ProcessBeanAttributes)2 JsonObject (javax.json.JsonObject)2 ClaimValue (org.eclipse.microprofile.jwt.ClaimValue)2 JWTAuthenticationMechanism (fish.payara.microprofile.jwtauth.eesecurity.JWTAuthenticationMechanism)1 SignedJWTIdentityStore (fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore)1