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;
}
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;
}
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;
}
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());
}
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;
}));
}
}
Aggregations