Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the conversion of JsonObject to custom types #476

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions implementation/jwt-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<groupId>io.smallrye</groupId>
<artifactId>smallrye-jwt-common</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.converters</groupId>
<artifactId>smallrye-converters</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
package io.smallrye.jwt.auth.principal;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.json.JsonObject;

import org.eclipse.microprofile.jwt.Claims;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.MalformedClaimException;

import io.smallrye.converters.api.Converter;
import io.smallrye.jwt.JsonUtils;

/**
Expand All @@ -34,6 +39,7 @@
*/
public class DefaultJWTCallerPrincipal extends JWTCallerPrincipal {
private final JwtClaims claimsSet;
private Map<Class<?>, Converter<?>> converters = Collections.emptyMap();

/**
* Create the DefaultJWTCallerPrincipal from the parsed JWT token and the extracted principal name
Expand All @@ -52,6 +58,11 @@ public DefaultJWTCallerPrincipal(String tokenType, JwtClaims claimsSet) {
this(getRawToken(claimsSet), tokenType, claimsSet);
}

public DefaultJWTCallerPrincipal(String tokenType, JwtClaims claimsSet, Map<Class<?>, Converter<?>> converters) {
this(getRawToken(claimsSet), tokenType, claimsSet);
this.converters = converters;
}

public DefaultJWTCallerPrincipal(JwtClaims claimsSet) {
this("JWT", claimsSet);
}
Expand Down Expand Up @@ -166,9 +177,15 @@ protected Set<String> filterCustomClaimNames(Collection<String> claimNames) {

protected void replaceClaimValueWithJsonValue(String name) {
try {
final Object object = claimsSet.getClaimValue(name, Object.class);
Object object = claimsSet.getClaimValue(name, Object.class);
if (!(object instanceof String)) {
claimsSet.setClaim(name, JsonUtils.wrapValue(object));

object = JsonUtils.wrapValue(object);
if (object instanceof JsonObject && converters.containsKey(JsonObject.class)) {
object = converters.get(JsonObject.class).convert(object.toString());
}

claimsSet.setClaim(name, object);
}
} catch (MalformedClaimException e) {
PrincipalLogging.log.replaceClaimValueWithJsonFailure(name, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.jose4j.jwt.consumer.JwtContext;

/**
* A default implementation of the abstract JWTCallerPrincipalFactory that uses the Keycloak token parsing classes.
* A default implementation of the abstract JWTCallerPrincipalFactory.
*/
public class DefaultJWTCallerPrincipalFactory extends JWTCallerPrincipalFactory {

Expand All @@ -30,7 +30,7 @@ public JWTCallerPrincipal parse(final String token, final JWTAuthContextInfo aut

JwtContext jwtContext = parser.parse(token, authContextInfo);
String type = jwtContext.getJoseObjects().get(0).getHeader("typ");
return new DefaultJWTCallerPrincipal(type, jwtContext.getJwtClaims());
return new DefaultJWTCallerPrincipal(type, jwtContext.getJwtClaims(), authContextInfo.getConverters());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import java.security.interfaces.RSAPublicKey;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.crypto.SecretKey;

import io.smallrye.converters.api.Converter;
import io.smallrye.jwt.KeyFormat;
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm;
import io.smallrye.jwt.algorithm.SignatureAlgorithm;
Expand Down Expand Up @@ -64,6 +66,7 @@ public class JWTAuthContextInfo {
private Set<String> requiredClaims;
private boolean relaxVerificationKeyValidation = true;
private boolean verifyCertificateThumbprint;
private Map<Class<?>, Converter<?>> converters = Collections.emptyMap();

public JWTAuthContextInfo() {
}
Expand Down Expand Up @@ -121,6 +124,7 @@ public JWTAuthContextInfo(JWTAuthContextInfo orig) {
this.requiredClaims = orig.requiredClaims;
this.relaxVerificationKeyValidation = orig.relaxVerificationKeyValidation;
this.verifyCertificateThumbprint = orig.verifyCertificateThumbprint;
this.converters = orig.converters;
}

@Deprecated
Expand Down Expand Up @@ -406,6 +410,7 @@ public String toString() {
", groupsSeparator='" + groupsSeparator + '\'' +
", relaxVerificationKeyValidation=" + relaxVerificationKeyValidation +
", verifyCertificateThumbprint=" + verifyCertificateThumbprint +
", converters=" + converters +
'}';
}

Expand All @@ -424,4 +429,12 @@ public boolean isVerifyCertificateThumbprint() {
public void setVerifyCertificateThumbprint(boolean verifyCertificateThumbprint) {
this.verifyCertificateThumbprint = verifyCertificateThumbprint;
}

public Map<Class<?>, Converter<?>> getConverters() {
return converters;
}

public void setConverters(Map<Class<?>, Converter<?>> converters) {
this.converters = converters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@
package io.smallrye.jwt.config;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.jwt.config.Names;

import io.smallrye.converters.api.Converter;
import io.smallrye.jwt.KeyFormat;
import io.smallrye.jwt.SmallryeJwtUtils;
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm;
Expand All @@ -46,6 +52,9 @@ public class JWTAuthContextInfoProvider {
private static final String NONE = "NONE";
private static final String DEFAULT_GROUPS_SEPARATOR = " ";

@Inject
Instance<Converter<?>> converters;

/**
* Create JWTAuthContextInfoProvider with the public key and issuer
*
Expand Down Expand Up @@ -526,9 +535,25 @@ Optional<JWTAuthContextInfo> getOptionalContextInfo() {
contextInfo.setRequiredClaims(requiredClaims.orElse(null));
contextInfo.setRelaxVerificationKeyValidation(relaxVerificationKeyValidation);
contextInfo.setVerifyCertificateThumbprint(verifyCertificateThumbprint);

Map<Class<?>, Converter<?>> converterMap = createConverterMap();
contextInfo.setConverters(converterMap);
return Optional.of(contextInfo);
}

private Map<Class<?>, Converter<?>> createConverterMap() {
if (converters != null && converters.isResolvable()) {
Map<Class<?>, Converter<?>> map = new HashMap<>();
for (Iterator<Converter<?>> it = converters.iterator(); it.hasNext();) {
Converter<?> converter = it.next();
map.put(String.class, converter);
}
return map;
} else {
return Collections.emptyMap();
}
}

@Produces
@ApplicationScoped
public JWTAuthContextInfo getContextInfo() {
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<version.jose4j>0.7.8</version.jose4j>
<version.resteasy>4.6.0.Final</version.resteasy>
<version.mokito>3.11.1</version.mokito>
<version.smallrye.converters>1.0.0</version.smallrye.converters>
</properties>

<licenses>
Expand Down Expand Up @@ -115,6 +116,12 @@
<version>${version.jose4j}</version>
</dependency>

<dependency>
<groupId>io.smallrye.converters</groupId>
<artifactId>smallrye-converters</artifactId>
<version>${version.smallrye.converters}</version>
</dependency>

<!-- Dependencies provided by the project -->
<dependency>
<groupId>io.smallrye</groupId>
Expand Down