-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to customize OIDC verification
- Loading branch information
1 parent
ba59762
commit e0bff19
Showing
9 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
extensions/oidc/runtime/src/main/java/io/quarkus/oidc/TokenCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkus.oidc; | ||
|
||
import jakarta.json.JsonObject; | ||
|
||
/** | ||
* TokenCustomizer can be used to change token headers for the token verification to succeed | ||
*/ | ||
public interface TokenCustomizer { | ||
/** | ||
* Customize token headers | ||
* | ||
* @param headers the token headers | ||
* @return modified headers, null can be returned to indicate no modification has taken place | ||
*/ | ||
JsonObject customizeHeaders(JsonObject headers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/TokenCustomizerFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.oidc.runtime; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
import io.quarkus.oidc.OIDCException; | ||
import io.quarkus.oidc.TokenCustomizer; | ||
|
||
public class TokenCustomizerFinder { | ||
|
||
public static TokenCustomizer find(String name) { | ||
ArcContainer container = Arc.container(); | ||
TokenCustomizer tokenCustomizer = null; | ||
if (container != null) { | ||
tokenCustomizer = name != null | ||
? (TokenCustomizer) container.instance(name).get() | ||
: container.instance(TokenCustomizer.class).get(); | ||
} | ||
if (tokenCustomizer == null && name != null) { | ||
throw new OIDCException("Unable to find TokenCustomizer " + name); | ||
} | ||
|
||
return tokenCustomizer; | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
extensions/oidc/runtime/src/test/java/io/quarkus/oidc/runtime/OidcProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package io.quarkus.oidc.runtime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.Base64; | ||
|
||
import org.jose4j.jwk.RsaJsonWebKey; | ||
import org.jose4j.jwk.RsaJwkGenerator; | ||
import org.jose4j.jwt.consumer.InvalidJwtException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.deployment.util.FileUtil; | ||
import io.quarkus.oidc.OidcTenantConfig; | ||
import io.quarkus.oidc.TokenCustomizer; | ||
import io.smallrye.jwt.build.Jwt; | ||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
|
||
public class OidcProviderTest { | ||
|
||
@SuppressWarnings("resource") | ||
@Test | ||
public void testAzureTokenCustomizer() throws Exception { | ||
|
||
final String token = readFile("token.txt"); | ||
final String jwkSetString = readFile("jwks.json"); | ||
JsonWebKeySet jwkSet = new JsonWebKeySet(jwkSetString); | ||
OidcTenantConfig oidcConfig = new OidcTenantConfig(); | ||
oidcConfig.token.setLifespanGrace(Integer.MAX_VALUE); | ||
|
||
OidcProvider provider = new OidcProvider(null, oidcConfig, jwkSet, null, null); | ||
try { | ||
provider.verifyJwtToken(token); | ||
fail("InvalidJwtException expected"); | ||
} catch (InvalidJwtException ex) { | ||
// continue | ||
} | ||
|
||
provider = new OidcProvider(null, oidcConfig, jwkSet, new TokenCustomizer() { | ||
|
||
@Override | ||
public JsonObject customizeHeaders(JsonObject headers) { | ||
try { | ||
String nonce = headers.getString("nonce"); | ||
byte[] nonceSha256 = OidcUtils.getSha256Digest(nonce.getBytes(StandardCharsets.UTF_8)); | ||
byte[] newNonceBytes = Base64.getUrlEncoder().withoutPadding().encode(nonceSha256); | ||
return Json.createObjectBuilder(headers) | ||
.add("nonce", new String(newNonceBytes, StandardCharsets.UTF_8)).build(); | ||
} catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
}, null); | ||
|
||
TokenVerificationResult result = provider.verifyJwtToken(token); | ||
assertEquals("https://sts.windows.net/e7861267-92c5-4a03-bdb2-2d3e491e7831/", | ||
result.localVerificationResult.getString("iss")); | ||
} | ||
|
||
private String readFile(String filePath) throws Exception { | ||
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath)) { | ||
byte[] content = FileUtil.readFileContents(is); | ||
return new String(content, StandardCharsets.UTF_8); | ||
} | ||
} | ||
|
||
@SuppressWarnings("resource") | ||
@Test | ||
public void testAlgorithmCustomizer() throws Exception { | ||
|
||
RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048); | ||
rsaJsonWebKey.setKeyId("k1"); | ||
|
||
final String token = Jwt.issuer("http://keycloak/ream").jws().keyId("k1").sign(rsaJsonWebKey.getPrivateKey()); | ||
final String newToken = replaceAlgorithm(token, "ES256"); | ||
JsonWebKeySet jwkSet = new JsonWebKeySet("{\"keys\": [" + rsaJsonWebKey.toJson() + "]}"); | ||
OidcTenantConfig oidcConfig = new OidcTenantConfig(); | ||
|
||
OidcProvider provider = new OidcProvider(null, oidcConfig, jwkSet, null, null); | ||
try { | ||
provider.verifyJwtToken(newToken); | ||
fail("InvalidJwtException expected"); | ||
} catch (InvalidJwtException ex) { | ||
// continue | ||
} | ||
|
||
provider = new OidcProvider(null, oidcConfig, jwkSet, new TokenCustomizer() { | ||
|
||
@Override | ||
public JsonObject customizeHeaders(JsonObject headers) { | ||
return Json.createObjectBuilder(headers).add("alg", "RS256").build(); | ||
} | ||
|
||
}, null); | ||
TokenVerificationResult result = provider.verifyJwtToken(newToken); | ||
assertEquals("http://keycloak/ream", result.localVerificationResult.getString("iss")); | ||
} | ||
|
||
private static String replaceAlgorithm(String token, String algorithm) { | ||
io.vertx.core.json.JsonObject headers = OidcUtils.decodeJwtHeaders(token); | ||
headers.put("alg", algorithm); | ||
String newHeaders = new String( | ||
Base64.getUrlEncoder().withoutPadding().encode(headers.toString().getBytes()), | ||
StandardCharsets.UTF_8); | ||
int dotIndex = token.indexOf('.'); | ||
return newHeaders + token.substring(dotIndex); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"keys": [ | ||
{ | ||
"kty":"RSA", | ||
"use":"sig", | ||
"kid":"-KI3Q9nNR7bRofxmeZoXqbHZGew", | ||
"x5t":"-KI3Q9nNR7bRofxmeZoXqbHZGew", | ||
"n":"tJL6Wr2JUsxLyNezPQh1J6zn6wSoDAhgRYSDkaMuEHy75VikiB8wg25WuR96gdMpookdlRvh7SnRvtjQN9b5m4zJCMpSRcJ5DuXl4mcd7Cg3Zp1C5-JmMq8J7m7OS9HpUQbA1yhtCHqP7XA4UnQI28J-Tn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mu8DSyY22dG/K6ZDx5qNFg8pehdOUYY24oMamd4J2u2lUgkCKGBZMQgBZFwk+q7H86B/byGuTDEizLjGPTY/sMms1FAX55xBydxrADAer/pKrOF1v7Dq9C1Z9QVcm5D9G4DcenyWUdMyK43NXbVQLPxLOng51KO9icp2j4U7pwHP"], | ||
"issuer":"https://login.microsoftonline.com/e7861267-92c5-4a03-bdb2-2d3e491e7831/v2.0" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
eyJ0eXAiOiJKV1QiLCJub25jZSI6ImM0LXg2c0RzQ2FXeVV2a25leEozekpOVXdfRU56U3FmSjg2dnJrcUs5NTQiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTAwMDAtYzAwMC0wMDAwMDAwMDAwMDAiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC9lNzg2MTI2Ny05MmM1LTRhMDMtYmRiMi0yZDNlNDkxZTc4MzEvIiwiaWF0IjoxNjgzOTE2MTY1LCJuYmYiOjE2ODM5MTYxNjUsImV4cCI6MTY4MzkyMDgyNywiYWNjdCI6MCwiYWNyIjoiMSIsImFpbyI6IkFUUUF5LzhUQUFBQXJmc0hnb0NoL2o3Rkw0cGdMRGk0QktwTUNFRWZrWlBnQStGR3ltZUNqazIvbnV0clg3NGVuSVdQekRBbmw1ankiLCJhbXIiOlsicHdkIl0sImFwcF9kaXNwbGF5bmFtZSI6InRlc3QiLCJhcHBpZCI6IjRmYmEzODc5LWI1ZmItNDYzMy05ZGUzLTEyZGQ4ZTI2ZmE2NyIsImFwcGlkYWNyIjoiM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