-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
700 additions
and
22 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
77 changes: 77 additions & 0 deletions
77
...ime/src/main/java/io/quarkus/elytron/security/runtime/ElytronTrustedIdentityProvider.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,77 @@ | ||
package io.quarkus.elytron.security.runtime; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
import java.util.function.Supplier; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.wildfly.security.auth.server.RealmIdentity; | ||
import org.wildfly.security.auth.server.RealmUnavailableException; | ||
import org.wildfly.security.auth.server.SecurityDomain; | ||
import org.wildfly.security.auth.server.ServerAuthenticationContext; | ||
import org.wildfly.security.credential.PasswordCredential; | ||
|
||
import io.quarkus.security.AuthenticationFailedException; | ||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.IdentityProvider; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.runtime.QuarkusSecurityIdentity; | ||
import io.quarkus.vertx.http.runtime.security.TrustedAuthenticationRequest; | ||
|
||
/** | ||
* | ||
* | ||
*/ | ||
@ApplicationScoped | ||
public class ElytronTrustedIdentityProvider implements IdentityProvider<TrustedAuthenticationRequest> { | ||
|
||
private static Logger log = Logger.getLogger(ElytronTrustedIdentityProvider.class); | ||
|
||
@Inject | ||
SecurityDomain domain; | ||
|
||
@Override | ||
public Class<TrustedAuthenticationRequest> getRequestType() { | ||
return TrustedAuthenticationRequest.class; | ||
} | ||
|
||
@Override | ||
public CompletionStage<SecurityIdentity> authenticate(TrustedAuthenticationRequest request, | ||
AuthenticationRequestContext context) { | ||
return context.runBlocking(new Supplier<SecurityIdentity>() { | ||
@Override | ||
public SecurityIdentity get() { | ||
org.wildfly.security.auth.server.SecurityIdentity result; | ||
try { | ||
RealmIdentity id = domain.getIdentity(request.getPrincipal()); | ||
if (!id.exists()) { | ||
return null; | ||
} | ||
PasswordCredential cred = id.getCredential(PasswordCredential.class); | ||
ServerAuthenticationContext ac = domain.createNewAuthenticationContext(); | ||
ac.setAuthenticationName(request.getPrincipal()); | ||
ac.addPrivateCredential(cred); | ||
ac.authorize(); | ||
result = ac.getAuthorizedIdentity(); | ||
|
||
if (result == null) { | ||
throw new AuthenticationFailedException(); | ||
} | ||
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(); | ||
builder.setPrincipal(result.getPrincipal()); | ||
for (String i : result.getRoles()) { | ||
builder.addRole(i); | ||
} | ||
return builder.build(); | ||
} catch (RealmUnavailableException e) { | ||
throw new RuntimeException(e); | ||
} catch (SecurityException e) { | ||
log.debug("Authentication failed", e); | ||
throw new AuthenticationFailedException(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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
125 changes: 125 additions & 0 deletions
125
.../vertx-http/deployment/src/test/java/io/quarkus/vertx/http/security/FormAuthTestCase.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,125 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
import java.util.function.Supplier; | ||
|
||
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.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.filter.cookie.CookieFilter; | ||
|
||
public class FormAuthTestCase { | ||
|
||
private static final String APP_PROPS = "" + | ||
"quarkus.http.auth.form.enabled=true\n" + | ||
"quarkus.http.auth.form.login-page=login\n" + | ||
"quarkus.http.auth.form.error-page=error\n" + | ||
"quarkus.http.auth.form.landing-page=landing\n" + | ||
"quarkus.http.auth.policy.r1.roles-allowed=admin\n" + | ||
"quarkus.http.auth.permission.roles1.paths=/admin\n" + | ||
"quarkus.http.auth.permission.roles1.policy=r1\n"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<JavaArchive>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestTrustedIdentityProvider.class, PathHandler.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles() | ||
.add("admin", "admin", "admin"); | ||
} | ||
|
||
@Test | ||
public void testFormBasedAuthSuccess() { | ||
CookieFilter cookies = new CookieFilter(); | ||
RestAssured | ||
.given() | ||
.filter(cookies) | ||
.redirects().follow(false) | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(302) | ||
.header("location", containsString("/login")) | ||
.cookie("quarkus-redirect-location", containsString("/admin")); | ||
|
||
RestAssured | ||
.given() | ||
.filter(cookies) | ||
.redirects().follow(false) | ||
.when() | ||
.formParam("j_username", "admin") | ||
.formParam("j_password", "admin") | ||
.post("/j_security_check") | ||
.then() | ||
.assertThat() | ||
.statusCode(302) | ||
.header("location", containsString("/admin")) | ||
.cookie("quarkus-credential", notNullValue()); | ||
|
||
RestAssured | ||
.given() | ||
.filter(cookies) | ||
.redirects().follow(false) | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.body(equalTo("admin:/admin")); | ||
|
||
} | ||
|
||
@Test | ||
public void testFormBasedAuthSuccessLandingPage() { | ||
CookieFilter cookies = new CookieFilter(); | ||
RestAssured | ||
.given() | ||
.filter(cookies) | ||
.redirects().follow(false) | ||
.when() | ||
.formParam("j_username", "admin") | ||
.formParam("j_password", "admin") | ||
.post("/j_security_check") | ||
.then() | ||
.assertThat() | ||
.statusCode(302) | ||
.header("location", containsString("/landing")) | ||
.cookie("quarkus-credential", notNullValue()); | ||
|
||
} | ||
|
||
@Test | ||
public void testFormAuthFailure() { | ||
CookieFilter cookies = new CookieFilter(); | ||
RestAssured | ||
.given() | ||
.filter(cookies) | ||
.redirects().follow(false) | ||
.when() | ||
.formParam("j_username", "admin") | ||
.formParam("j_password", "wrongpassword") | ||
.post("/j_security_check") | ||
.then() | ||
.assertThat() | ||
.statusCode(302) | ||
.header("location", containsString("/error")); | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../deployment/src/test/java/io/quarkus/vertx/http/security/TestTrustedIdentityProvider.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,33 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.IdentityProvider; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.runtime.QuarkusPrincipal; | ||
import io.quarkus.security.runtime.QuarkusSecurityIdentity; | ||
import io.quarkus.vertx.http.runtime.security.TrustedAuthenticationRequest; | ||
|
||
@Singleton | ||
public class TestTrustedIdentityProvider implements IdentityProvider<TrustedAuthenticationRequest> { | ||
@Override | ||
public Class<TrustedAuthenticationRequest> getRequestType() { | ||
return TrustedAuthenticationRequest.class; | ||
} | ||
|
||
@Override | ||
public CompletionStage<SecurityIdentity> authenticate(TrustedAuthenticationRequest request, | ||
AuthenticationRequestContext context) { | ||
TestIdentityController.TestIdentity ident = TestIdentityController.idenitities.get(request.getPrincipal()); | ||
if (ident == null) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
return CompletableFuture | ||
.completedFuture(QuarkusSecurityIdentity.builder().setPrincipal(new QuarkusPrincipal(request.getPrincipal())) | ||
.addRoles(ident.roles).build()); | ||
} | ||
} |
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.