-
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.
Support token propagation during identity augmentation
- Loading branch information
1 parent
2da3206
commit 4eac45b
Showing
17 changed files
with
435 additions
and
4 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
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
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
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-auth=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(); | ||
} |
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
Oops, something went wrong.