-
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.
Merge pull request #34933 from michalvavrik/feature/oidc-token-propag…
…ation-during-auth Support OIDC token propagation during SecurityIdentity augmentation
- Loading branch information
Showing
27 changed files
with
770 additions
and
4 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...nt/runtime/src/main/java/io/quarkus/oidc/token/propagation/TokenPropagationConstants.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,21 @@ | ||
package io.quarkus.oidc.token.propagation; | ||
|
||
public final class TokenPropagationConstants { | ||
|
||
TokenPropagationConstants() { | ||
} | ||
|
||
/** | ||
* System property key that is resolved to true if OIDC auth mechanism should put | ||
* `TokenCredential` into Vert.x duplicated context. | ||
*/ | ||
public static final String OIDC_PROPAGATE_TOKEN_CREDENTIAL = "io.quarkus.oidc.runtime." + | ||
"AbstractOidcAuthenticationMechanism.PROPAGATE_TOKEN_CREDENTIAL_WITH_DUPLICATED_CTX"; | ||
/** | ||
* System property key that is resolved to true if JWT auth mechanism should put | ||
* `TokenCredential` into Vert.x duplicated context. | ||
*/ | ||
public static final String JWT_PROPAGATE_TOKEN_CREDENTIAL = "io.quarkus.smallrye.jwt.runtime." + | ||
"auth.JWTAuthMechanism.PROPAGATE_TOKEN_CREDENTIAL_WITH_DUPLICATED_CTX"; | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
...n/propagation/reactive/OidcTokenPropagationWithSecurityIdentityAugmentorLazyAuthTest.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,55 @@ | ||
package io.quarkus.oidc.token.propagation.reactive; | ||
|
||
import static io.quarkus.oidc.token.propagation.reactive.RolesSecurityIdentityAugmentor.SUPPORTED_USER; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.util.Set; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.oidc.server.OidcWiremockTestResource; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTestResource(OidcWiremockTestResource.class) | ||
public class OidcTokenPropagationWithSecurityIdentityAugmentorLazyAuthTest { | ||
|
||
private static Class<?>[] testClasses = { | ||
FrontendResource.class, | ||
ProtectedResource.class, | ||
AccessTokenPropagationService.class, | ||
RolesResource.class, | ||
RolesService.class, | ||
RolesSecurityIdentityAugmentor.class | ||
}; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(testClasses) | ||
.addAsResource("application.properties") | ||
.addAsResource( | ||
new StringAsset("quarkus.oidc-token-propagation-reactive.enabled-during-authentication=true\n" + | ||
"quarkus.rest-client.\"roles\".uri=http://localhost:8081/roles\n" + | ||
"quarkus.http.auth.proactive=false\n"), | ||
"META-INF/microprofile-config.properties")); | ||
|
||
@Test | ||
public void testGetUserNameWithTokenPropagation() { | ||
// request only succeeds if SecurityIdentityAugmentor managed to acquire 'tester' role for user 'alice' | ||
// and that is only possible if access token is propagated during augmentation | ||
RestAssured.given().auth().oauth2(getBearerAccessToken()) | ||
.when().get("/frontend/token-propagation-with-augmentor") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo("Token issued to alice has been exchanged, new user name: bob")); | ||
} | ||
|
||
public String getBearerAccessToken() { | ||
return OidcWiremockTestResource.getAccessToken(SUPPORTED_USER, Set.of("admin")); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...idc/token/propagation/reactive/OidcTokenPropagationWithSecurityIdentityAugmentorTest.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,54 @@ | ||
package io.quarkus.oidc.token.propagation.reactive; | ||
|
||
import static io.quarkus.oidc.token.propagation.reactive.RolesSecurityIdentityAugmentor.SUPPORTED_USER; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import java.util.Set; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.oidc.server.OidcWiremockTestResource; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTestResource(OidcWiremockTestResource.class) | ||
public class OidcTokenPropagationWithSecurityIdentityAugmentorTest { | ||
|
||
private static Class<?>[] testClasses = { | ||
FrontendResource.class, | ||
ProtectedResource.class, | ||
AccessTokenPropagationService.class, | ||
RolesResource.class, | ||
RolesService.class, | ||
RolesSecurityIdentityAugmentor.class | ||
}; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(testClasses) | ||
.addAsResource("application.properties") | ||
.addAsResource( | ||
new StringAsset("quarkus.oidc-token-propagation-reactive.enabled-during-authentication=true\n" + | ||
"quarkus.rest-client.\"roles\".uri=http://localhost:8081/roles\n"), | ||
"META-INF/microprofile-config.properties")); | ||
|
||
@Test | ||
public void testGetUserNameWithTokenPropagation() { | ||
// request only succeeds if SecurityIdentityAugmentor managed to acquire 'tester' role for user 'alice' | ||
// and that is only possible if access token is propagated during augmentation | ||
RestAssured.given().auth().oauth2(getBearerAccessToken()) | ||
.when().get("/frontend/token-propagation-with-augmentor") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo("Token issued to alice has been exchanged, new user name: bob")); | ||
} | ||
|
||
public String getBearerAccessToken() { | ||
return OidcWiremockTestResource.getAccessToken(SUPPORTED_USER, Set.of("admin")); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...ve/deployment/src/test/java/io/quarkus/oidc/token/propagation/reactive/RolesResource.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,26 @@ | ||
package io.quarkus.oidc.token.propagation.reactive; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.jwt.JsonWebToken; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.security.ForbiddenException; | ||
|
||
@Path("/roles") | ||
@Authenticated | ||
public class RolesResource { | ||
|
||
@Inject | ||
JsonWebToken jwt; | ||
|
||
@GET | ||
public String get() { | ||
if ("bob".equals(jwt.getName())) { | ||
return "tester"; | ||
} | ||
throw new ForbiddenException("Only user 'bob' is allowed to request roles"); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../test/java/io/quarkus/oidc/token/propagation/reactive/RolesSecurityIdentityAugmentor.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,34 @@ | ||
package io.quarkus.oidc.token.propagation.reactive; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.SecurityIdentityAugmentor; | ||
import io.quarkus.security.runtime.QuarkusSecurityIdentity; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@ApplicationScoped | ||
public class RolesSecurityIdentityAugmentor implements SecurityIdentityAugmentor { | ||
|
||
static final String SUPPORTED_USER = "alice"; | ||
|
||
@Inject | ||
@RestClient | ||
RolesService rolesService; | ||
|
||
@Override | ||
public Uni<SecurityIdentity> augment(SecurityIdentity securityIdentity, | ||
AuthenticationRequestContext authenticationRequestContext) { | ||
if (securityIdentity != null && securityIdentity.getPrincipal() != null | ||
&& SUPPORTED_USER.equals(securityIdentity.getPrincipal().getName())) { | ||
return rolesService | ||
.getRole() | ||
.map(role -> QuarkusSecurityIdentity.builder(securityIdentity).addRole(role).build()); | ||
} | ||
return Uni.createFrom().item(securityIdentity); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ive/deployment/src/test/java/io/quarkus/oidc/token/propagation/reactive/RolesService.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,18 @@ | ||
package io.quarkus.oidc.token.propagation.reactive; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
import io.quarkus.oidc.token.propagation.AccessToken; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@RegisterRestClient(configKey = "roles") | ||
@AccessToken | ||
@Path("/") | ||
public interface RolesService { | ||
|
||
@GET | ||
Uni<String> getRole(); | ||
} |
Oops, something went wrong.