Java Code Examples for org.eclipse.microprofile.jwt.Claim

The following examples show how to use org.eclipse.microprofile.jwt.Claim. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source Project: smallrye-jwt   Source File: ProviderExtensionSupport.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
            }
        }
    }
}
 
Example 6
Source Project: quarkus   Source File: SmallRyeJwtProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Register the CDI beans that are needed by the MP-JWT extension
 *
 * @param additionalBeans - producer for additional bean items
 */
@BuildStep
void registerAdditionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
        BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
    if (config.enabled) {
        AdditionalBeanBuildItem.Builder unremovable = AdditionalBeanBuildItem.builder().setUnremovable();
        unremovable.addBeanClass(MpJwtValidator.class);
        unremovable.addBeanClass(JWTAuthMechanism.class);
        unremovable.addBeanClass(ClaimValueProducer.class);
        additionalBeans.produce(unremovable.build());
    }
    AdditionalBeanBuildItem.Builder removable = AdditionalBeanBuildItem.builder();
    removable.addBeanClass(JWTAuthContextInfoProvider.class);
    removable.addBeanClass(DefaultJWTParser.class);
    removable.addBeanClass(CommonJwtProducer.class);
    removable.addBeanClass(RawClaimTypeProducer.class);
    removable.addBeanClass(JsonValueProducer.class);
    removable.addBeanClass(JwtPrincipalProducer.class);
    removable.addBeanClass(Claim.class);
    additionalBeans.produce(removable.build());

    reflectiveClasses.produce(new ReflectiveClassBuildItem(true, true, SignatureAlgorithm.class));
    reflectiveClasses.produce(new ReflectiveClassBuildItem(true, true, JwtProviderImpl.class));
}
 
Example 7
Source Project: smallrye-jwt   Source File: OptionalClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces an Optional claim value wrapping a String.
 *
 * @param ip reference to the injection point
 * @return an optional claim value
 */
@Produces
@Claim("")
public Optional<String> getOptionalString(InjectionPoint ip) {
    CDILogging.log.getOptionalString(ip);
    if (currentToken == null) {
        return Optional.empty();
    }
    return Optional.ofNullable((String) JsonUtils.convert(String.class, currentToken.getClaim(getName(ip))));
}
 
Example 8
Source Project: smallrye-jwt   Source File: OptionalClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces an Optional claim value wrapping a Set of Strings.
 *
 * @param ip reference to the injection point
 * @return an optional claim value
 */
@Produces
@Claim("")
public Optional<Set<String>> getOptionalStringSet(InjectionPoint ip) {
    CDILogging.log.getOptionalStringSet(ip);
    if (currentToken == null) {
        return Optional.empty();
    }
    return Optional.ofNullable((Set) JsonUtils.convert(Set.class, currentToken.getClaim(getName(ip))));
}
 
Example 9
Source Project: smallrye-jwt   Source File: OptionalClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces an Optional claim value wrapping a Long.
 *
 * @param ip reference to the injection point
 * @return an optional claim value
 */
@Produces
@Claim("")
public Optional<Long> getOptionalLong(InjectionPoint ip) {
    CDILogging.log.getOptionalLong(ip);
    if (currentToken == null) {
        return Optional.empty();
    }
    return Optional.ofNullable((Long) JsonUtils.convert(Long.class, currentToken.getClaim(getName(ip))));
}
 
Example 10
Source Project: smallrye-jwt   Source File: OptionalClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces an Optional claim value wrapping a Boolean.
 *
 * @param ip reference to the injection point
 * @return an optional claim value
 */
@Produces
@Claim("")
public Optional<Boolean> getOptionalBoolean(InjectionPoint ip) {
    CDILogging.log.getOptionalBoolean(ip);
    if (currentToken == null) {
        return Optional.empty();
    }
    return Optional.ofNullable((Boolean) JsonUtils.convert(Boolean.class, currentToken.getClaim(getName(ip))));
}
 
Example 11
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 12
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces a *raw* Optional value.
 *
 * @param ip reference to the injection point
 * @return an optional claim value
 */
@Produces
@Claim("")
@SuppressWarnings("rawtypes")
public Optional getOptionalValue(InjectionPoint ip) {
    CDILogging.log.getOptionalValue(ip);
    if (currentToken == null) {
        return Optional.empty();
    }
    return currentToken.claim(getName(ip));
}
 
Example 13
Source Project: smallrye-jwt   Source File: RawClaimTypeProducer.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source Project: smallrye-jwt   Source File: ProviderExtensionSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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));
            }
        }
    }
}
 
Example 15
Source Project: smallrye-jwt   Source File: CommonJwtProducer.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source Project: quarkus   Source File: OidcBuildStep.java    License: Apache License 2.0 5 votes vote down vote up
@BuildStep(onlyIf = IsEnabled.class)
AdditionalBeanBuildItem jwtClaimIntegration(Capabilities capabilities) {
    if (!capabilities.isPresent(Capability.JWT)) {
        AdditionalBeanBuildItem.Builder removable = AdditionalBeanBuildItem.builder();
        removable.addBeanClass(CommonJwtProducer.class);
        removable.addBeanClass(RawClaimTypeProducer.class);
        removable.addBeanClass(JsonValueProducer.class);
        removable.addBeanClass(Claim.class);
        return removable.build();
    }
    return null;
}
 
Example 17
Source Project: hammock   Source File: JWTExtension.java    License: Apache License 2.0 5 votes vote down vote up
private static Claim getClaim(Set<Annotation> annotations) {
    for (Annotation a : annotations) {
        if (a instanceof Claim) {
            return (Claim) a;
        }
    }
    return null;
}
 
Example 18
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public JsonString getJsonString(InjectionPoint ip) {
    return getValue(ip);
}
 
Example 19
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public Optional<JsonString> getOptionalJsonString(InjectionPoint ip) {
    return getOptionalValue(ip);
}
 
Example 20
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public JsonNumber getJsonNumber(InjectionPoint ip) {
    return getValue(ip);
}
 
Example 21
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public Optional<JsonNumber> getOptionalJsonNumber(InjectionPoint ip) {
    return getOptionalValue(ip);
}
 
Example 22
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public JsonArray getJsonArray(InjectionPoint ip) {
    return getValue(ip);
}
 
Example 23
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public Optional<JsonArray> getOptionalJsonArray(InjectionPoint ip) {
    return getOptionalValue(ip);
}
 
Example 24
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public JsonObject getJsonObject(InjectionPoint ip) {
    return getValue(ip);
}
 
Example 25
Source Project: smallrye-jwt   Source File: JsonValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
public Optional<JsonObject> getOptionalJsonObject(InjectionPoint ip) {
    return getOptionalValue(ip);
}
 
Example 26
Source Project: smallrye-jwt   Source File: ClaimValueProducer.java    License: Apache License 2.0 4 votes vote down vote up
@Produces
@Claim("")
<T> ClaimValue<T> produceClaim(InjectionPoint ip) {
    return new ClaimValueWrapper<>(ip, util);
}
 
Example 27
Source Project: hammock   Source File: ClaimDefinition.java    License: Apache License 2.0 4 votes vote down vote up
ClaimDefinition(Claim claim, Type returnType, Type typeParameter) {
    this.claim = claim;
    this.returnType = returnType;
    this.typeParameter = typeParameter;
}
 
Example 28
Source Project: hammock   Source File: JWTExtension.java    License: Apache License 2.0 4 votes vote down vote up
public void locateClaims(@Observes ProcessInjectionPoint<?, ?> pip) {
    Claim claim = pip.getInjectionPoint().getAnnotated().getAnnotation(Claim.class);
    if (claim != null) {
        injectionPoints.add(pip.getInjectionPoint());
    }
}
 
Example 29
Source Project: tomee   Source File: ClaimBean.java    License: Apache License 2.0 4 votes vote down vote up
public static String getClaimKey(final Claim claim) {
    return claim.standard() == Claims.UNKNOWN ? claim.value() : claim.standard().name();
}
 
Example 30
Source Project: tomee   Source File: MPJWTCDIExtension.java    License: Apache License 2.0 4 votes vote down vote up
public void collectConfigProducer(@Observes final ProcessInjectionPoint<?, ?> pip) {
    final Claim claim = pip.getInjectionPoint().getAnnotated().getAnnotation(Claim.class);
    if (claim != null) {
        injectionPoints.add(pip.getInjectionPoint());
    }
}