Search in sources :

Example 16 with Claim

use of org.eclipse.microprofile.jwt.Claim in project Payara by payara.

the class JwtAuthCdiExtension method checkInjectIntoRightScope.

public <T> void checkInjectIntoRightScope(@Observes ProcessInjectionTarget<T> eventIn, BeanManager beanManager) {
    // JDK8 u60 workaround
    ProcessInjectionTarget<T> event = eventIn;
    for (InjectionPoint injectionPoint : event.getInjectionTarget().getInjectionPoints()) {
        Claim claim = hasClaim(injectionPoint);
        if (claim != null) {
            // MP-JWT 1.0 7.1.3.
            Bean<?> bean = injectionPoint.getBean();
            Class<?> scope = bean != null ? injectionPoint.getBean().getScope() : null;
            if (scope != null && scope.equals(SessionScoped.class)) {
                throw new DeploymentException("Can't inject using qualifier " + Claim.class + " in a target with scope " + scope);
            }
            if (!claim.value().equals("") && claim.standard() != UNKNOWN && !claim.value().equals(claim.standard().name())) {
                throw new DeploymentException("Claim value " + claim.value() + " should be equal to claim standard " + claim.standard().name() + " or one of those should be left at their default value");
            }
        }
    }
}
Also used : SessionScoped(javax.enterprise.context.SessionScoped) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) DeploymentException(javax.enterprise.inject.spi.DeploymentException) Claim(org.eclipse.microprofile.jwt.Claim)

Example 17 with Claim

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

the class MPJWTExtension method addTypeToClaimProducer.

/**
 * Replace the general producer method BeanAttributes with one bound to the collected injection site
 * types to properly reflect all of the type locations the producer method applies to.
 *
 * @param pba the ProcessBeanAttributes
 * @see ClaimProviderBeanAttributes
 */
public void addTypeToClaimProducer(@Observes ProcessBeanAttributes pba) {
    if (pba.getAnnotated().isAnnotationPresent(Claim.class)) {
        Claim claim = pba.getAnnotated().getAnnotation(Claim.class);
        if (claim.value().length() == 0 && claim.standard() == Claims.UNKNOWN) {
            log.debugf("addTypeToClaimProducer: %s\n", pba.getAnnotated());
            BeanAttributes delegate = pba.getBeanAttributes();
            String name = delegate.getName();
            if (delegate.getTypes().contains(Optional.class)) {
                if (providerOptionalTypes.size() == 0) {
                    providerOptionalTypes.add(Optional.class);
                }
                pba.setBeanAttributes(new ClaimProviderBeanAttributes(delegate, providerOptionalTypes, providerQualifiers));
            // This is
            } else if (name != null && name.startsWith("RawClaimTypeProducer#")) {
                if (rawTypes.size() == 0) {
                    rawTypes.add(Object.class);
                }
                pba.setBeanAttributes(new ClaimProviderBeanAttributes(delegate, rawTypes, rawTypeQualifiers));
                log.debugf("Setup RawClaimTypeProducer BeanAttributes");
            }
        }
    }
}
Also used : BeanAttributes(javax.enterprise.inject.spi.BeanAttributes) ProcessBeanAttributes(javax.enterprise.inject.spi.ProcessBeanAttributes) Claim(org.eclipse.microprofile.jwt.Claim)

Example 18 with Claim

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

the class ClaimValueProducer method produce.

@Produces
@Claim("")
ClaimValue<T> produce(InjectionPoint ip) {
    String name = getName(ip);
    ClaimValue<Optional<T>> cv = MPJWTProducer.generalClaimValueProducer(name);
    ClaimValue<T> returnValue = (ClaimValue<T>) cv;
    Optional<T> value = cv.getValue();
    // Pull out the ClaimValue<T> T type,
    Type matchType = ip.getType();
    Type actualType = Object.class;
    boolean isOptional = false;
    if (matchType instanceof ParameterizedType) {
        actualType = ((ParameterizedType) matchType).getActualTypeArguments()[0];
        isOptional = matchType.getTypeName().equals(Optional.class.getTypeName());
        if (isOptional) {
            actualType = ((ParameterizedType) matchType).getActualTypeArguments()[0];
        }
    }
    if (!actualType.getTypeName().startsWith(Optional.class.getTypeName())) {
        T nestedValue = value.orElse(null);
        ClaimValueWrapper<T> wrapper = new ClaimValueWrapper<>(cv.getName());
        wrapper.setValue(nestedValue);
        returnValue = wrapper;
    }
    return returnValue;
}
Also used : Optional(java.util.Optional) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ClaimValue(org.eclipse.microprofile.jwt.ClaimValue) Produces(javax.enterprise.inject.Produces) Claim(org.eclipse.microprofile.jwt.Claim)

Example 19 with Claim

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

the class ClaimValueProducer method getName.

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 20 with Claim

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

the class RawClaimTypeProducer method getClaimAsDouble.

@Produces
@Claim("")
Double getClaimAsDouble(InjectionPoint ip) {
    CDILogging.log.getClaimAsDouble(ip);
    if (currentToken == null) {
        return null;
    }
    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    Double returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonNumber) {
            JsonNumber jsonValue = (JsonNumber) value;
            returnValue = jsonValue.doubleValue();
        } else {
            returnValue = Double.parseDouble(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)

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