Skip to content
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

Standardize the examples #980

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions security-keycloak-authorization-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>

<!-- Test -->
<!-- Test dependencies -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
survivant marked this conversation as resolved.
Show resolved Hide resolved
* Copyright 2019 Red Hat, Inc, and individual contributors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
survivant marked this conversation as resolved.
Show resolved Hide resolved
package org.acme.security.keycloak.authorization;

import javax.ws.rs.GET;
Expand All @@ -10,7 +25,7 @@ public class AdminResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
public String manage() {
public String admin() {
return "granted";
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2019 Red Hat, Inc, and individual contributors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.acme.security.keycloak.authorization;

import javax.inject.Inject;
Expand All @@ -12,21 +27,21 @@
public class UserResource {

@Inject
SecurityIdentity keycloakSecurityContext;
SecurityIdentity identity;

@GET
@Path("/me")
@Produces(MediaType.APPLICATION_JSON)
public User me() {
return new User(keycloakSecurityContext);
return new User(identity);
}

public static class User {

private final String userName;

User(SecurityIdentity securityContext) {
this.userName = securityContext.getPrincipal().getName();
User(SecurityIdentity identity) {
this.userName = identity.getPrincipal().getName();
}

public String getUserName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import io.quarkus.test.junit.QuarkusIntegrationTest;

/**
* @author <a href="mailto:[email protected]">Pedro Igor</a>
*/
@QuarkusIntegrationTest
public class NativePolicyEnforcerIT extends PolicyEnforcerTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public void testAccessUserResource() {
.when().get("/api/users/me")
.then()
.statusCode(200);

RestAssured.given().auth().oauth2(getAccessToken("admin"))
.when().get("/api/users/me")
.then()
.statusCode(200);

RestAssured.given().auth().oauth2(getAccessToken("jdoe"))
.when().get("/api/users/me")
.then()
Expand Down
5 changes: 5 additions & 0 deletions security-openid-connect-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
</dependencyManagement>

<dependencies>
<dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@survivant Why is this module added here ? There is a dedicated keycloak-authorization-quickstart

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sberyozkin maybe it's not necessary. I though it was used by KeycloakTestClient. So the sample : security-keycloak-authorization-quickstart need that module to check in Keycloak if a URL is authorized for the user logged.

and the sample : security-openid-connect-quickstart will received a token with the roles in it and use that to validate if the user is authorized with the annotation @RolesAllowed so the dependency to keycloak autorisation is not needed ?

did I understand correctly the difference between the two samples ?

<groupId>io.quarkus</groupId>
<artifactId>quarkus-keycloak-authorization</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
Expand Down Expand Up @@ -104,6 +108,7 @@
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
<quarkus.native.enable-https-url-handler>true</quarkus.native.enable-https-url-handler>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@

import io.quarkus.security.Authenticated;

/**
* @author <a href="mailto:[email protected]">Pedro Igor</a>
*/
@Path("/api/admin")
@Authenticated
public class AdminResource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,42 @@ public class BearerTokenAuthenticationTest {

KeycloakTestClient keycloakClient = new KeycloakTestClient();

static {
RestAssured.useRelaxedHTTPSValidation();
}

@Test
public void testAdminAccess() {
public void testUserAccess() {
RestAssured.given().auth().oauth2(getAccessToken("alice"))
.when().get("/api/users/me")
.then()
.statusCode(200);

RestAssured.given().auth().oauth2(getAccessToken("admin"))
.when().get("/api/admin")
.when().get("/api/users/me")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@survivant Hmm but this drops a check that alice (who only has a user role) can not access admin-level resources

.then()
.statusCode(200);

RestAssured.given().auth().oauth2(getAccessToken("alice"))
.when().get("/api/admin")
RestAssured.given().auth().oauth2(getAccessToken("jdoe"))
.when().get("/api/users/me")
.then()
.statusCode(403);
.statusCode(200);
}

@Test
public void testUserAccess() {

public void testAdminAccess() {
RestAssured.given().auth().oauth2(getAccessToken("alice"))
.when().get("/api/users/me")
.when().get("/api/admin")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly for admin

.then()
.statusCode(200);
.statusCode(403);

RestAssured.given().auth().oauth2(getAccessToken("jdoe"))
.when().get("/api/admin")
.then()
.statusCode(403);

RestAssured.given().auth().oauth2(getAccessToken("admin"))
.when().get("/api/users/me")
.when().get("/api/admin")
.then()
.statusCode(200);
}
Expand Down