Skip to content

Commit b6204d4

Browse files
committed
chore(tooling): create a HTTP Service to test HTTP IdentityProvider
related-to AM-4688
1 parent 2315645 commit b6204d4

File tree

7 files changed

+347
-0
lines changed

7 files changed

+347
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Build Docker image
2+
3+
```
4+
mvn package com.google.cloud.tools:jib-maven-plugin:dockerBuild -Dimage=<image name>
5+
```
6+
7+
# Run Image
8+
9+
The image is configured to bind on 0.0.0.0 and listen the port 8080.
10+
11+
# Request
12+
13+
Authenticate user using password (in clear text)
14+
15+
```
16+
curl -vv -X POST http://localhost:8080/login -d '{"username":"user01", "password":"Test1234567!" }' -H'Content-Type: application/json'
17+
18+
{
19+
"username" : "user01",
20+
"preferred_username" : "alice",
21+
"given_name" : "Wonder",
22+
"first_name" : "Alice",
23+
"email" : "[email protected]"
24+
}
25+
```
26+
27+
Get profile by username without password
28+
29+
```
30+
curl -vv -X POST http://localhost:8080/username -d '{"username":"user01"}' -H'Content-Type: application/json'
31+
32+
{
33+
"username" : "user01",
34+
"preferred_username" : "alice",
35+
"given_name" : "Wonder",
36+
"first_name" : "Alice",
37+
"email" : "[email protected]"
38+
}
39+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) 2015 The Gravitee team (http://gravitee.io)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>io.gravitee.am</groupId>
25+
<artifactId>gravitee-am-tooling</artifactId>
26+
<version>4.7.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>gravitee-am-tooling-http-provider</artifactId>
30+
<name>Gravitee IO - Access Management - Tooling - HTTP Provider</name>
31+
32+
<properties>
33+
<jib-maven-plugin.version>3.4.2</jib-maven-plugin.version>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.vertx</groupId>
39+
<artifactId>vertx-web</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>commons-cli</groupId>
43+
<artifactId>commons-cli</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.nimbusds</groupId>
47+
<artifactId>nimbus-jose-jwt</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>ch.qos.logback</groupId>
51+
<artifactId>logback-classic</artifactId>
52+
<scope>compile</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>ch.qos.logback</groupId>
56+
<artifactId>logback-core</artifactId>
57+
<scope>compile</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
<scope>compile</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>com.google.cloud.tools</groupId>
70+
<artifactId>jib-maven-plugin</artifactId>
71+
<version>${jib-maven-plugin.version}</version>
72+
<configuration>
73+
<to>
74+
<image>localhost:5000/gravitee-am-fapi-resource</image>
75+
</to>
76+
<from>
77+
<image>eclipse-temurin:17</image>
78+
</from>
79+
<container>
80+
<jvmFlags>
81+
<jvmFlag>-Xmx64m</jvmFlag>
82+
</jvmFlags>
83+
<mainClass>io.gravitee.am.tooling.http.HttpProviderApi</mainClass>
84+
<ports>
85+
<port>8080</port>
86+
</ports>
87+
<format>OCI</format>
88+
</container>
89+
</configuration>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.gravitee.am.tooling.http;
17+
18+
import io.vertx.core.Vertx;
19+
import io.vertx.core.http.HttpServer;
20+
import io.vertx.core.http.HttpServerOptions;
21+
import io.vertx.ext.web.Router;
22+
import io.vertx.ext.web.handler.BodyHandler;
23+
import io.vertx.ext.web.handler.StaticHandler;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.util.Optional;
28+
29+
import static io.vertx.core.http.HttpMethod.POST;
30+
31+
/**
32+
* @author Eric LELEU (eric.leleu at graviteesource.com)
33+
*/
34+
public class HttpProviderApi {
35+
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProviderApi.class);
36+
37+
public static final String CONF_HOST = "host";
38+
public static final String CONF_PORT = "port";
39+
public static final String CONF_TRUST_STORE_PATH = "trustStorePath";
40+
public static final String CONF_TRUST_STORE_TYPE = "trustStoreType";
41+
public static final String CONF_TRUST_STORE_PASSWORD = "trustStorePassword";
42+
public static final String CONF_KEY_STORE_PATH = "keyStorePath";
43+
public static final String CONF_KEY_STORE_TYPE = "keyStoreType";
44+
public static final String CONF_KEY_STORE_PASSWORD = "keyStorePassword";
45+
public static final String CONF_CERT_HEADER = "certificateHeader";
46+
47+
public static void main(String[] args) throws Exception {
48+
HttpServerOptions options = buildHttpOptions();
49+
50+
Vertx vertx = Vertx.vertx();
51+
HttpServer server = vertx.createHttpServer(options);
52+
53+
Router router = Router.router(vertx);
54+
router.route()
55+
.handler(StaticHandler.create());
56+
57+
router.route("/login")
58+
.method(POST)
59+
.consumes("application/json")
60+
.produces("application/json")
61+
.handler(BodyHandler.create())
62+
.handler(new LoginHandler(true));
63+
64+
router.route("/username")
65+
.method(POST)
66+
.consumes("application/json")
67+
.produces("application/json")
68+
.handler(BodyHandler.create())
69+
.handler(new LoginHandler(false));
70+
71+
server.requestHandler(router).listen();
72+
LOGGER.info("Server listening on port {}", options.getPort());
73+
}
74+
75+
private static HttpServerOptions buildHttpOptions() {
76+
Integer httpProviderPort = Optional.ofNullable(System.getenv("HTTP_PROVIDER_PORT")).map(Integer::parseInt).orElse(8080);
77+
HttpServerOptions options = new HttpServerOptions();
78+
options.setPort(httpProviderPort);
79+
options.setHost("0.0.0.0");
80+
options.setUseAlpn(false);
81+
return options;
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.gravitee.am.tooling.http;
17+
18+
import io.vertx.core.Handler;
19+
import io.vertx.core.json.JsonObject;
20+
import io.vertx.ext.web.RoutingContext;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import java.util.MissingResourceException;
25+
import java.util.Optional;
26+
import java.util.ResourceBundle;
27+
28+
29+
public class LoginHandler implements Handler<RoutingContext> {
30+
private static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);
31+
32+
private final boolean comparePassword;
33+
34+
public LoginHandler(boolean comparePassword) {
35+
this.comparePassword = comparePassword;
36+
}
37+
38+
@Override
39+
public void handle(RoutingContext routingContext) {
40+
try {
41+
final var usersBundle = ResourceBundle.getBundle("users");
42+
43+
final var payload = routingContext.body().asJsonObject();
44+
final var username = payload.getString("username");
45+
final var passwordRef = usersBundle.getString(username +".password");
46+
47+
if (passwordRef == null || "".equals(passwordRef.trim()) || (comparePassword && !passwordRef.equals(payload.getString("password")))) {
48+
routingContext.response()
49+
.putHeader("content-type", "application/json")
50+
.setStatusCode(401).end();
51+
}
52+
53+
if (!comparePassword && username.equalsIgnoreCase("user03")) {
54+
routingContext.response()
55+
.putHeader("content-type", "application/json")
56+
.setStatusCode(500).end(JsonObject.of("error", "Service Not Available").encodePrettily());
57+
}
58+
59+
final JsonObject responsePayload = new JsonObject();
60+
responsePayload.put("username", username);
61+
responsePayload.put("sub", username);
62+
Optional.ofNullable(usersBundle.getString(username +".preferred_username")).ifPresent(value -> responsePayload.put("preferred_username", value));
63+
Optional.ofNullable(usersBundle.getString(username +".given_name")).ifPresent(value -> responsePayload.put("given_name", value));
64+
Optional.ofNullable(usersBundle.getString(username +".first_name")).ifPresent(value -> responsePayload.put("first_name", value));
65+
Optional.ofNullable(usersBundle.getString(username +".email")).ifPresent(value -> responsePayload.put("email", value));
66+
67+
//response ok
68+
final int statusCode = 200;
69+
routingContext.response()
70+
.putHeader("content-type", "application/json")
71+
.setStatusCode(statusCode)
72+
.end(responsePayload.encodePrettily()); // return JWT as JSON object
73+
} catch (MissingResourceException e) {
74+
LOGGER.info("Username not found", e);
75+
routingContext.fail(401, e);
76+
} catch (Exception e) {
77+
LOGGER.warn("Unable to process the FAPI resource request", e);
78+
routingContext.fail(500, e);
79+
}
80+
}
81+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
user01.password=Test1234567!
2+
user01.preferred_username=alice
3+
user01.given_name=Wonder
4+
user01.first_name=Alice
5+
user01.email[email protected]
6+
user02.password=Test"1234567"!
7+
user02.preferred_username=jdoe
8+
user02.given_name=Doe
9+
user02.first_name=John
10+
user02.email[email protected]
11+
user03.password=Test1234567!
12+
user03.preferred_username=bob
13+
user03.given_name=Dylan
14+
user03.first_name=Bob
15+
user03.email[email protected]

gravitee-am-tooling/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!--
2+
3+
Copyright (C) 2015 The Gravitee team (http://gravitee.io)
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>io.gravitee.am</groupId>
23+
<artifactId>gravitee-am-parent</artifactId>
24+
<version>4.7.0-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>gravitee-am-tooling</artifactId>
28+
<name>Gravitee IO - Access Management - Tooling</name>
29+
<packaging>pom</packaging>
30+
31+
<modules>
32+
<module>gravitee-am-tooling-http-provider</module>
33+
</modules>
34+
35+
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<module>gravitee-am-authdevice-notifier</module>
7070
<module>gravitee-am-ciba-delegated-service</module>
7171
<module>gravitee-am-monitoring</module>
72+
<module>gravitee-am-tooling</module>
7273
</modules>
7374

7475
<properties>

0 commit comments

Comments
 (0)