forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
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 quarkusio#10004 from jmartisk/master-issue-10001
Enable programmatic security in SmallRye GraphQL endpoints
- Loading branch information
Showing
8 changed files
with
121 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
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
79 changes: 79 additions & 0 deletions
79
...graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/SecurityTest.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,79 @@ | ||
package io.quarkus.smallrye.graphql.deployment; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import org.eclipse.microprofile.graphql.GraphQLApi; | ||
import org.eclipse.microprofile.graphql.Query; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
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.http.Header; | ||
|
||
public class SecurityTest extends AbstractGraphQLTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClass(SecuredApi.class) | ||
.addAsResource("application-secured.properties", "application.properties") | ||
.addAsResource("users.properties") | ||
.addAsResource("roles.properties") | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); | ||
|
||
@Test | ||
public void testAuthenticatedUser() { | ||
String query = getPayload("{ foo }"); | ||
RestAssured.given() | ||
.header(new Header("Authorization", "Basic ZGF2aWQ6cXdlcnR5MTIz")) | ||
.body(query) | ||
.contentType(MEDIATYPE_JSON) | ||
.post("/graphql/") | ||
.then() | ||
.assertThat() | ||
.body("errors", nullValue()) | ||
.body("data.foo", equalTo("foo")); | ||
} | ||
|
||
@Test | ||
public void testUnauthorizedRole() { | ||
String query = getPayload("{ bar }"); | ||
RestAssured.given() | ||
.header(new Header("Authorization", "Basic ZGF2aWQ6cXdlcnR5MTIz")) | ||
.body(query) | ||
.contentType(MEDIATYPE_JSON) | ||
.post("/graphql") | ||
.then() | ||
.assertThat() | ||
.body("errors", notNullValue()) | ||
.body("data.bar", nullValue()); | ||
} | ||
|
||
@GraphQLApi | ||
@ApplicationScoped | ||
public static class SecuredApi { | ||
|
||
@Query | ||
@RolesAllowed("fooRole") | ||
public String foo() { | ||
return "foo"; | ||
} | ||
|
||
@Query | ||
@RolesAllowed("barRole") | ||
public String bar() { | ||
return "bar"; | ||
} | ||
|
||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
extensions/smallrye-graphql/deployment/src/test/resources/application-secured.properties
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,5 @@ | ||
quarkus.security.users.file.enabled=true | ||
quarkus.security.users.file.plain-text=true | ||
quarkus.security.users.file.users=users.properties | ||
quarkus.security.users.file.roles=roles.properties | ||
quarkus.http.auth.basic=true |
1 change: 1 addition & 0 deletions
1
extensions/smallrye-graphql/deployment/src/test/resources/roles.properties
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 @@ | ||
david=fooRole |
1 change: 1 addition & 0 deletions
1
extensions/smallrye-graphql/deployment/src/test/resources/users.properties
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 @@ | ||
david=qwerty123 |
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