-
Notifications
You must be signed in to change notification settings - Fork 3k
Provide fluent API to set up SecurityIdentityAugmentors and IdentityProviders such as Security JPA programmatically
#51279
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
Draft
michalvavrik
wants to merge
1
commit into
quarkusio:main
Choose a base branch
from
michalvavrik:feature/fluent-api-for-identity-provider-and-augmentor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+929
−17
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
...nsions/security-jpa-common/runtime/src/main/java/io/quarkus/security/jpa/SecurityJpa.java
This file contains hidden or 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,28 @@ | ||
| package io.quarkus.security.jpa; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| import io.quarkus.arc.Arc; | ||
| import io.quarkus.security.identity.IdentityProvider; | ||
|
|
||
| /** | ||
| * A CDI beans used to retrieve generated Quarkus Security JPA beans. | ||
| */ | ||
| public interface SecurityJpa { | ||
|
|
||
| /** | ||
| * @return Quarkus Security JPA {@link IdentityProvider}s | ||
| */ | ||
| Collection<IdentityProvider<?>> getIdentityProviders(); | ||
|
|
||
| /** | ||
| * Looks up the {@link SecurityJpa} CDI bean and returns the Quarkus Security JPA {@link IdentityProvider}s. | ||
| * | ||
| * @return Quarkus Security JPA {@link IdentityProvider}s | ||
| */ | ||
| static Collection<IdentityProvider<?>> jpa() { | ||
| try (var securityJpaInstance = Arc.requireContainer().instance(SecurityJpa.class)) { | ||
| return securityJpaInstance.get().getIdentityProviders(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
157 changes: 157 additions & 0 deletions
157
...ment/src/test/java/io/quarkus/security/jpa/reactive/SecurityJpaReactiveFluentApiTest.java
This file contains hidden or 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,157 @@ | ||
| package io.quarkus.security.jpa.reactive; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Observes; | ||
|
|
||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
| import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.quarkus.security.identity.AuthenticationRequestContext; | ||
| import io.quarkus.security.identity.IdentityProvider; | ||
| import io.quarkus.security.identity.SecurityIdentity; | ||
| import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest; | ||
| import io.quarkus.security.jpa.SecurityJpa; | ||
| import io.quarkus.test.QuarkusUnitTest; | ||
| import io.quarkus.vertx.http.security.Form; | ||
| import io.quarkus.vertx.http.security.HttpSecurity; | ||
| import io.restassured.RestAssured; | ||
| import io.restassured.filter.cookie.CookieFilter; | ||
| import io.smallrye.mutiny.Uni; | ||
|
|
||
| public class SecurityJpaReactiveFluentApiTest { | ||
|
|
||
| @RegisterExtension | ||
| static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
| .create(JavaArchive.class) | ||
| .addClasses(TestApplication.class, MinimalUserEntity.class, SecurityJpaConfiguration.class, | ||
| SingleRoleSecuredResource.class, FailingIdentityProvider.class) | ||
| .addAsResource("minimal-config/import.sql", "import.sql") | ||
| .addAsResource(new StringAsset(""" | ||
| quarkus.datasource.db-kind=postgresql | ||
| quarkus.datasource.username=${postgres.reactive.username} | ||
| quarkus.datasource.password=${postgres.reactive.password} | ||
| quarkus.datasource.reactive=true | ||
| quarkus.datasource.reactive.url=${postgres.reactive.url} | ||
| quarkus.hibernate-orm.sql-load-script=import.sql | ||
| quarkus.hibernate-orm.schema-management.strategy=drop-and-create | ||
| """), "application.properties")); | ||
|
|
||
| @Test | ||
| void testFormBasedAuthentication() { | ||
| RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
| CookieFilter cookies = new CookieFilter(); | ||
| RestAssured | ||
| .given() | ||
| .filter(cookies) | ||
| .redirects().follow(false) | ||
| .when() | ||
| .get("/jaxrs-secured/user-secured") | ||
| .then() | ||
| .assertThat() | ||
| .statusCode(302) | ||
| .header("location", containsString("/login")) | ||
| .cookie("quarkus-redirect-location", containsString("/user-secured")); | ||
|
|
||
| // test with a non-existent user | ||
| RestAssured | ||
| .given() | ||
| .filter(cookies) | ||
| .redirects().follow(false) | ||
| .when() | ||
| .formParam("j_username", "dummy") | ||
| .formParam("j_password", "dummy") | ||
| .post("/j_security_check") | ||
| .then() | ||
| .assertThat() | ||
| .statusCode(302); | ||
|
|
||
| RestAssured | ||
| .given() | ||
| .filter(cookies) | ||
| .redirects().follow(false) | ||
| .when() | ||
| .formParam("j_username", "user") | ||
| .formParam("j_password", "user") | ||
| .post("/j_security_check") | ||
| .then() | ||
| .assertThat() | ||
| .statusCode(302) | ||
| .header("location", containsString("/user-secured")) | ||
| .cookie("laitnederc-sukrauq", notNullValue()); | ||
|
|
||
| RestAssured | ||
| .given() | ||
| .filter(cookies) | ||
| .redirects().follow(false) | ||
| .when() | ||
| .get("/jaxrs-secured/user-secured") | ||
| .then() | ||
| .assertThat() | ||
| .statusCode(200) | ||
| .body(equalTo("A secured message")); | ||
| } | ||
|
|
||
| @Test | ||
| void testBasicAuthentication() { | ||
| RestAssured | ||
| .given() | ||
| .auth().preemptive().basic("user", "wrong-password") | ||
| .get("/jaxrs-secured/user-secured") | ||
| .then() | ||
| .statusCode(401); | ||
| RestAssured | ||
| .given() | ||
| .auth().preemptive().basic("user", "user") | ||
| .get("/jaxrs-secured/user-secured") | ||
| .then() | ||
| .statusCode(200) | ||
| .body(equalTo("A secured message")); | ||
| } | ||
|
|
||
| public static class SecurityJpaConfiguration { | ||
|
|
||
| void configure(@Observes HttpSecurity httpSecurity) { | ||
| var form = Form.builder() | ||
| .loginPage("login") | ||
| .errorPage("error") | ||
| .landingPage("landing") | ||
| .cookieName("laitnederc-sukrauq") | ||
| .newCookieInterval(Duration.ofSeconds(5)) | ||
| .timeout(Duration.ofSeconds(5)) | ||
| .encryptionKey("CHANGEIT-CHANGEIT-CHANGEIT-CHANGEIT-CHANGEIT") | ||
| .build(); | ||
| var jpa = SecurityJpa.jpa(); | ||
| httpSecurity.mechanism(form, jpa).basic(jpa); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @ApplicationScoped | ||
| public static class FailingIdentityProvider implements IdentityProvider<UsernamePasswordAuthenticationRequest> { | ||
|
|
||
| @Override | ||
| public Class<UsernamePasswordAuthenticationRequest> getRequestType() { | ||
| return UsernamePasswordAuthenticationRequest.class; | ||
| } | ||
|
|
||
| @Override | ||
| public Uni<SecurityIdentity> authenticate(UsernamePasswordAuthenticationRequest authenticationRequest, | ||
| AuthenticationRequestContext authenticationRequestContext) { | ||
| throw new IllegalStateException("This provider must never be invoked as we selected the JPA provider"); | ||
| } | ||
|
|
||
| @Override | ||
| public int priority() { | ||
| return Integer.MAX_VALUE; | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...ntime/src/main/java/io/quarkus/security/jpa/reactive/runtime/SecurityJpaReactiveImpl.java
This file contains hidden or 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,32 @@ | ||
| package io.quarkus.security.jpa.reactive.runtime; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| import io.quarkus.security.identity.IdentityProvider; | ||
| import io.quarkus.security.jpa.SecurityJpa; | ||
|
|
||
| public final class SecurityJpaReactiveImpl implements SecurityJpa { | ||
|
|
||
| @Inject | ||
| Instance<JpaReactiveIdentityProvider> jpaReactiveIdentityProvider; | ||
|
|
||
| @Inject | ||
| Instance<JpaReactiveTrustedIdentityProvider> jpaReactiveTrustedIdentityProvider; | ||
|
|
||
| @Override | ||
| public Collection<IdentityProvider<?>> getIdentityProviders() { | ||
| final Collection<IdentityProvider<?>> result = new ArrayList<>(); | ||
| if (jpaReactiveIdentityProvider.isResolvable()) { | ||
| result.add(jpaReactiveIdentityProvider.get()); | ||
| } | ||
| if (jpaReactiveTrustedIdentityProvider.isResolvable()) { | ||
| result.add(jpaReactiveTrustedIdentityProvider.get()); | ||
| } | ||
| return Collections.unmodifiableCollection(result); | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.