Search in sources :

Example 6 with Claim

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

the class ProviderExtensionSupport method processClaimProviderInjections.

/**
 * Collect the types of all {@linkplain Provider} injection points annotated with {@linkplain Claim}.
 *
 * @param pip - the injection point event information
 */
void processClaimProviderInjections(@Observes ProcessInjectionPoint<?, ? extends Provider> pip) {
    CDILogging.log.pip(pip.getInjectionPoint());
    final InjectionPoint ip = pip.getInjectionPoint();
    if (ip.getAnnotated().isAnnotationPresent(Claim.class)) {
        Claim claim = ip.getAnnotated().getAnnotation(Claim.class);
        if (claim.value().length() == 0 && claim.standard() == Claims.UNKNOWN) {
            pip.addDefinitionError(CDIMessages.msg.claimHasNoNameOrValidStandardEnumSetting(ip));
        }
        boolean usesEnum = claim.standard() != Claims.UNKNOWN;
        final String claimName = usesEnum ? claim.standard().name() : claim.value();
        CDILogging.log.checkingProviderClaim(claimName, ip);
        Type matchType = ip.getType();
        // The T from the Provider<T> injection site
        Type actualType = ((ParameterizedType) matchType).getActualTypeArguments()[0];
        // Don't add Optional or JsonValue as this is handled specially
        if (isOptional(actualType)) {
            // Validate that this is not an Optional<JsonValue>
            Type innerType = ((ParameterizedType) actualType).getActualTypeArguments()[0];
            if (!isJson(innerType)) {
                providerOptionalTypes.add(actualType);
                providerQualifiers.add(claim);
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Claim(org.eclipse.microprofile.jwt.Claim)

Example 7 with Claim

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

the class ProviderExtensionSupport 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 ProviderBeanAttributes
 */
public void addTypeToClaimProducer(@Observes ProcessBeanAttributes pba) {
    if (!providerOptionalTypes.isEmpty() && pba.getAnnotated().isAnnotationPresent(Claim.class)) {
        Claim claim = pba.getAnnotated().getAnnotation(Claim.class);
        if (claim.value().length() == 0 && claim.standard() == Claims.UNKNOWN) {
            CDILogging.log.addTypeToClaimProducer(pba.getAnnotated());
            BeanAttributes delegate = pba.getBeanAttributes();
            if (delegate.getTypes().contains(Optional.class)) {
                pba.setBeanAttributes(new ProviderBeanAttributes(delegate, providerOptionalTypes, providerQualifiers));
            }
        }
    }
}
Also used : BeanAttributes(javax.enterprise.inject.spi.BeanAttributes) ProcessBeanAttributes(javax.enterprise.inject.spi.ProcessBeanAttributes) Claim(org.eclipse.microprofile.jwt.Claim)

Example 8 with Claim

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

the class RawClaimTypeProducer method getClaimAsSet.

@Produces
@Claim("")
Set<String> getClaimAsSet(InjectionPoint ip) {
    CDILogging.log.getClaimAsSet(ip);
    if (currentToken == null) {
        return null;
    }
    String name = getName(ip);
    return (Set<String>) JsonUtils.convert(Set.class, currentToken.getClaim(name));
}
Also used : Set(java.util.Set) JsonString(javax.json.JsonString) Produces(javax.enterprise.inject.Produces) Claim(org.eclipse.microprofile.jwt.Claim)

Example 9 with Claim

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

the class RawClaimTypeProducer method getClaimAsBoolean.

@Produces
@Claim("")
Boolean getClaimAsBoolean(InjectionPoint ip) {
    CDILogging.log.getClaimAsBoolean(ip);
    if (currentToken == null) {
        return null;
    }
    String name = getName(ip);
    Optional<Object> optValue = currentToken.claim(name);
    Boolean returnValue = null;
    if (optValue.isPresent()) {
        Object value = optValue.get();
        if (value instanceof JsonValue) {
            final JsonValue.ValueType valueType = ((JsonValue) value).getValueType();
            if (valueType.equals(JsonValue.ValueType.TRUE)) {
                returnValue = true;
            } else if (valueType.equals(JsonValue.ValueType.FALSE)) {
                returnValue = false;
            }
        } else {
            returnValue = Boolean.valueOf(value.toString());
        }
    }
    return returnValue;
}
Also used : JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) Produces(javax.enterprise.inject.Produces) Claim(org.eclipse.microprofile.jwt.Claim)

Example 10 with Claim

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

the class RawClaimTypeProducer method getName.

static 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 : JsonString(javax.json.JsonString) Annotation(java.lang.annotation.Annotation) 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