-
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
c59afb6
commit 12a961f
Showing
7 changed files
with
165 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; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
package io.quarkus.oidc.runtime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonObject; | ||
|
||
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.oidc.OidcTenantConfig; | ||
import io.quarkus.oidc.TokenCustomizer; | ||
import io.smallrye.jwt.build.Jwt; | ||
|
||
public class OidcProviderTest { | ||
|
||
@SuppressWarnings("resource") | ||
@Test | ||
public void testCustomizer() 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); | ||
} | ||
} |