Skip to content

Commit 6d64746

Browse files
author
Dennis Labordus
committed
Merge branch 'userinfo-call' into add-comments
2 parents ec984fc + d72f8aa commit 6d64746

File tree

8 files changed

+115
-8
lines changed

8 files changed

+115
-8
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ You can then execute your native executable with: `./app/target/code-with-quarku
7272
If you want to learn more about building native executables, please consult https://quarkus.io/guides/maven-tooling.html
7373
.
7474

75+
## Environment variables
76+
77+
Below environment variable(s) can be used to configure the connection to BaseX, if BaseX Server is used.
78+
79+
| Environment variable | Java Property | Description | Example |
80+
| -------------------------------- | ------------------------- | --------------------------------------------- | ---------------- |
81+
| BASEX_HOST | basex.host | Name of the Host where BaseX runs. | localhost |
82+
| BASEX_PORT | basex.port | Port on the Host on which BaseX runs. | 1984 |
83+
| BASEX_USERNAME | basex.username | Username under which the application logs in. | admin |
84+
| BASEX_PASSWORD | basex.password | Password of the username used above. | admin |
85+
86+
Below environment variable(s) can be used to configure which claims are used to fill the UserInfo response.
87+
88+
| Environment variable | Java Property | Description | Example |
89+
| -------------------------------- | ------------------------------ | --------------------------------------------- | ---------------- |
90+
| USERINFO_NAME_CLAIMNAME | compas.userinfo.name.claimname | The Name of the user logged in. | name |
91+
7592
## Security
7693

7794
To use most of the endpoints the users needs to be authenticated using JWT in the authorization header. There are 4

app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ SPDX-License-Identifier: Apache-2.0
104104
</dependency>
105105
<dependency>
106106
<groupId>io.quarkus</groupId>
107-
<artifactId>quarkus-test-security</artifactId>
107+
<artifactId>quarkus-test-security-jwt</artifactId>
108108
<scope>test</scope>
109109
</dependency>
110110
<dependency>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.rest;
5+
6+
import io.smallrye.config.ConfigMapping;
7+
import io.smallrye.config.WithName;
8+
9+
@ConfigMapping(prefix = "compas.userinfo")
10+
public interface UserInfoProperties {
11+
@WithName("name.claimname")
12+
String name();
13+
}

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResource.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package org.lfenergy.compas.scl.data.rest.v1;
55

66
import io.quarkus.security.Authenticated;
7-
import io.quarkus.security.identity.SecurityIdentity;
7+
import org.eclipse.microprofile.jwt.JsonWebToken;
88
import org.lfenergy.compas.scl.data.model.SclType;
9+
import org.lfenergy.compas.scl.data.rest.UserInfoProperties;
910
import org.lfenergy.compas.scl.data.rest.v1.model.Type;
1011
import org.lfenergy.compas.scl.data.rest.v1.model.TypeListResponse;
12+
import org.lfenergy.compas.scl.data.rest.v1.model.UserInfoResponse;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

@@ -31,16 +33,19 @@ public class CompasCommonResource {
3133
private static final Logger LOGGER = LoggerFactory.getLogger(CompasCommonResource.class);
3234

3335
@Inject
34-
SecurityIdentity securityIdentity;
36+
JsonWebToken jsonWebToken;
37+
38+
@Inject
39+
UserInfoProperties userInfoProperties;
3540

3641
@GET
3742
@Path("/type/list")
3843
@Produces(MediaType.APPLICATION_XML)
3944
public TypeListResponse list(@HeaderParam("Authorization") String authHeader) {
40-
LOGGER.debug("Authorization Header '{}'", authHeader);
45+
LOGGER.trace("Authorization Header '{}'", authHeader);
4146

4247
// Retrieve the roles the logged in user has.
43-
var roles = securityIdentity.getRoles();
48+
var roles = jsonWebToken.getGroups();
4449

4550
var response = new TypeListResponse();
4651
response.setTypes(
@@ -52,4 +57,15 @@ public TypeListResponse list(@HeaderParam("Authorization") String authHeader) {
5257
.collect(Collectors.toList()));
5358
return response;
5459
}
60+
61+
@GET
62+
@Path("/userinfo")
63+
@Produces(MediaType.APPLICATION_XML)
64+
public UserInfoResponse getUserInfo(@HeaderParam("Authorization") String authHeader) {
65+
LOGGER.trace("Authorization Header '{}'", authHeader);
66+
67+
var response = new UserInfoResponse();
68+
response.setName(jsonWebToken.getClaim(userInfoProperties.name()));
69+
return response;
70+
}
5571
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.rest.v1.model;
5+
6+
import javax.xml.bind.annotation.XmlAccessType;
7+
import javax.xml.bind.annotation.XmlAccessorType;
8+
import javax.xml.bind.annotation.XmlElement;
9+
import javax.xml.bind.annotation.XmlRootElement;
10+
11+
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.SCL_DATA_SERVICE_V1_NS_URI;
12+
13+
@XmlRootElement(name = "UserInfoResponse", namespace = SCL_DATA_SERVICE_V1_NS_URI)
14+
@XmlAccessorType(XmlAccessType.FIELD)
15+
public class UserInfoResponse {
16+
@XmlElement(name = "Name", namespace = SCL_DATA_SERVICE_V1_NS_URI)
17+
private String name;
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
}

app/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
compas.userinfo.name.claimname = ${USERINFO_NAME_CLAIMNAME:name}
6+
57
quarkus.http.cors = false
68
quarkus.http.root-path = /compas-scl-data-service/
79
quarkus.http.limits.max-body-size = 150M

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasCommonResourceTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
import io.quarkus.test.common.http.TestHTTPEndpoint;
77
import io.quarkus.test.junit.QuarkusTest;
88
import io.quarkus.test.security.TestSecurity;
9+
import io.quarkus.test.security.jwt.Claim;
10+
import io.quarkus.test.security.jwt.JwtSecurity;
911
import org.junit.jupiter.api.Test;
1012

1113
import static io.restassured.RestAssured.given;
1214
import static org.junit.jupiter.api.Assertions.assertEquals;
1315
import static org.lfenergy.compas.scl.data.rest.Constants.READ_ROLE;
16+
import static org.lfenergy.compas.scl.data.rest.Constants.UPDATE_ROLE;
1417

1518
@QuarkusTest
1619
@TestHTTPEndpoint(CompasCommonResource.class)
1720
class CompasCommonResourceTest {
1821
@Test
19-
@TestSecurity(user = "test-user", roles = {"IID_" + READ_ROLE, "SCD_" + READ_ROLE})
22+
@TestSecurity(user = "test-user", roles = {"IID_" + READ_ROLE, "SCD_" + READ_ROLE, "SED_" + UPDATE_ROLE})
2023
void list_WhenCalledWithMultipleReadRights_ThenMultipleItemResponseRetrieved() {
2124
var response = given()
2225
.when().get("/type/list")
@@ -44,7 +47,7 @@ void list_WhenCalledWithOneReadRights_ThenOneItemResponseRetrieved() {
4447
.response();
4548

4649
var xmlPath = response.xmlPath();
47-
// User has read rights for 2 types, so these types are returned.
50+
// User has read rights for one type, so this type is returned.
4851
var sclTypes = xmlPath.getList("TypeListResponse.Type.Code");
4952
assertEquals(1, sclTypes.size());
5053
assertEquals("SCD", sclTypes.get(0));
@@ -61,8 +64,27 @@ void list_WhenCalledWithNoReadRights_ThenNoItemResponseRetrieved() {
6164
.response();
6265

6366
var xmlPath = response.xmlPath();
64-
// User has read rights for 2 types, so these types are returned.
67+
// User has read rights for no types, so empty list is returned.
6568
var sclTypes = xmlPath.getList("TypeListResponse.Type.Code");
6669
assertEquals(0, sclTypes.size());
6770
}
71+
72+
@Test
73+
@TestSecurity(user = "test-user")
74+
@JwtSecurity(claims = {
75+
// Default the claim "name" is configured, so we will set this claim for the test.
76+
@Claim(key = "name", value = "Test User")
77+
})
78+
void getUserInfo_WhenCalled_ThenUserInfoResponseRetrieved() {
79+
var response = given()
80+
.when().get("/userinfo")
81+
.then()
82+
.statusCode(200)
83+
.extract()
84+
.response();
85+
86+
var xmlPath = response.xmlPath();
87+
var name = xmlPath.get("UserInfoResponse.Name");
88+
assertEquals("Test User", name);
89+
}
6890
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-FileCopyrightText: 2021 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.data.rest.v1.model;
5+
6+
class UserInfoResponseTest extends AbstractPojoTester {
7+
@Override
8+
protected Class<?> getClassToBeTested() {
9+
return UserInfoResponse.class;
10+
}
11+
}

0 commit comments

Comments
 (0)