|
3 | 3 | import static io.quarkus.test.bootstrap.KeycloakService.DEFAULT_REALM; |
4 | 4 | import static io.quarkus.test.bootstrap.KeycloakService.DEFAULT_REALM_BASE_PATH; |
5 | 5 | import static io.quarkus.test.bootstrap.KeycloakService.DEFAULT_REALM_FILE; |
6 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
7 | | - |
8 | | -import org.apache.http.HttpStatus; |
9 | | -import org.junit.jupiter.api.Assertions; |
10 | | -import org.junit.jupiter.api.MethodOrderer; |
11 | | -import org.junit.jupiter.api.Order; |
12 | | -import org.junit.jupiter.api.Test; |
13 | | -import org.junit.jupiter.api.TestMethodOrder; |
14 | 6 |
|
15 | 7 | import io.quarkus.test.bootstrap.KeycloakService; |
16 | | -import io.quarkus.test.bootstrap.Protocol; |
17 | | -import io.quarkus.test.bootstrap.RestService; |
18 | 8 | import io.quarkus.test.scenarios.QuarkusScenario; |
19 | | -import io.quarkus.test.services.Certificate; |
20 | 9 | import io.quarkus.test.services.KeycloakContainer; |
21 | | -import io.quarkus.test.services.QuarkusApplication; |
22 | | -import io.quarkus.test.services.URILike; |
23 | | -import io.restassured.path.json.JsonPath; |
24 | | -import io.restassured.response.Response; |
25 | 10 |
|
26 | 11 | @QuarkusScenario |
27 | | -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
28 | | -public class MetaEndpointIT { |
| 12 | +public class MetaEndpointIT extends AbstractMetaEndpointIT { |
29 | 13 |
|
30 | 14 | @KeycloakContainer(runKeycloakInProdMode = true) |
31 | 15 | static KeycloakService keycloak = new KeycloakService(DEFAULT_REALM_FILE, DEFAULT_REALM, DEFAULT_REALM_BASE_PATH); |
32 | | - static final String CUSTOM_ENDPOINT = "/custom-endpoint"; |
33 | | - private static final String DEFAULT_META_ENDPOINT = ".well-known/oauth-protected-resource"; |
34 | | - static final String OVERLOADED_META_ENDPOINT = "/" + DEFAULT_META_ENDPOINT + CUSTOM_ENDPOINT; |
35 | | - |
36 | | - @QuarkusApplication |
37 | | - static RestService http = new RestService() |
38 | | - .withProperty("quarkus.oidc.auth-server-url", () -> keycloak.getRealmUrl()) |
39 | | - .withProperty("quarkus.oidc.resource-metadata.enabled", "true") |
40 | | - .withProperty("quarkus.oidc.client-id", BaseOidcSecurityIT.CLIENT_ID_DEFAULT) |
41 | | - .withProperty("quarkus.oidc.credentials.secret", BaseOidcSecurityIT.CLIENT_SECRET_DEFAULT) |
42 | | - .withProperties(keycloak::getTlsProperties); |
43 | | - |
44 | | - @QuarkusApplication(ssl = true, certificates = @Certificate(configureKeystore = true, configureTruststore = true, configureHttpServer = true)) |
45 | | - static RestService https = new RestService() |
46 | | - .withProperty("quarkus.oidc.auth-server-url", () -> keycloak.getRealmUrl()) |
47 | | - .withProperty("quarkus.oidc.resource-metadata.enabled", "true") |
48 | | - .withProperty("quarkus.http.ssl.client-auth", "request") |
49 | | - .withProperty("quarkus.http.auth.permission.auth.policy", "authenticated") |
50 | | - .withProperty("quarkus.http.auth.permission.auth.paths", "/user") |
51 | | - .withProperty("quarkus.oidc.resource-metadata.resource", CUSTOM_ENDPOINT) |
52 | | - .withProperty("quarkus.oidc.client-id", BaseOidcSecurityIT.CLIENT_ID_DEFAULT) |
53 | | - .withProperty("quarkus.oidc.credentials.secret", BaseOidcSecurityIT.CLIENT_SECRET_DEFAULT) |
54 | | - .withProperties(keycloak::getTlsProperties); |
55 | | - |
56 | | - @Test |
57 | | - @Order(1) |
58 | | - public void httpHasHTTPSMetadata() { |
59 | | - Response response = http.given() |
60 | | - .when() |
61 | | - .get(DEFAULT_META_ENDPOINT); |
62 | | - assertEquals(HttpStatus.SC_OK, response.statusCode()); |
63 | | - JsonPath jsonPath = response.body().jsonPath(); |
64 | | - assertEquals(getAppURL(http, Protocol.HTTP).withScheme("https").toString(), |
65 | | - jsonPath.getString("resource"), |
66 | | - "No app URL in the body: " + response.body().asString()); |
67 | | - assertEquals(keycloak.getRealmUrl(), |
68 | | - jsonPath.getString("authorization_servers[0]"), |
69 | | - "No authorization server URL in the body: " + response.body().asString()); |
70 | | - } |
71 | | - |
72 | | - @Test |
73 | | - @Order(2) |
74 | | - public void noForcedHttps() { |
75 | | - http.stop(); |
76 | | - http.withProperty("quarkus.oidc.resource-metadata.force-https-scheme", "false"); |
77 | | - http.start(); |
78 | | - Response response = http.given() |
79 | | - .when() |
80 | | - .get(DEFAULT_META_ENDPOINT); |
81 | | - assertEquals(HttpStatus.SC_OK, response.statusCode()); |
82 | | - JsonPath jsonPath = response.body().jsonPath(); |
83 | | - assertEquals(getAppURL(http, Protocol.HTTP).toString(), |
84 | | - jsonPath.getString("resource"), |
85 | | - "No app URL in the body: " + response.body().asString()); |
86 | | - assertEquals(keycloak.getRealmUrl(), |
87 | | - jsonPath.getString("authorization_servers[0]"), |
88 | | - "No authorization server URL in the body: " + response.body().asString()); |
89 | | - } |
90 | | - |
91 | | - @Test |
92 | | - public void endpointInfoInHeader() { |
93 | | - Response response = https |
94 | | - .relaxedHttps() |
95 | | - .given() |
96 | | - .when() |
97 | | - .get("/user"); |
98 | | - assertEquals(HttpStatus.SC_UNAUTHORIZED, response.statusCode()); |
99 | | - String header = response.header("www-authenticate"); |
100 | | - Assertions.assertNotNull(header, "There is no authentication header in the answer!"); |
101 | | - String metadataURL = getAppURL(https, Protocol.HTTPS) |
102 | | - .withPath(OVERLOADED_META_ENDPOINT) |
103 | | - .toString(); |
104 | | - // What value of the 'www-authenticate' header looks like: |
105 | | - // Bearer resource_metadata="https://localhost:1103/.well-known/oauth-protected-resource/custom-endpoint" |
106 | | - assertEquals(metadataURL, |
107 | | - header.split("=")[1].replaceAll("\"", ""), |
108 | | - "Authentication header doesn't contain metadata endpoint"); |
109 | | - } |
110 | | - |
111 | | - URILike getAppURL(RestService app, Protocol protocol) { |
112 | | - return app.getURI(protocol); |
113 | | - } |
114 | 16 |
|
115 | | - @Test |
116 | | - public void httpsHasMetadata() { |
117 | | - Response response = https |
118 | | - .relaxedHttps() |
119 | | - .given() |
120 | | - .when() |
121 | | - .get(OVERLOADED_META_ENDPOINT); |
122 | | - JsonPath jsonPath = response.body().jsonPath(); |
123 | | - assertEquals(getAppURL(https, Protocol.HTTPS).withPath(CUSTOM_ENDPOINT).toString(), |
124 | | - jsonPath.getString("resource"), |
125 | | - "No app URL in the body: " + response.body().asString()); |
126 | | - assertEquals(keycloak.getRealmUrl(), |
127 | | - jsonPath.getString("authorization_servers[0]"), |
128 | | - "No authorization server URL in the body: " + response.body().asString()); |
129 | | - } |
130 | 17 | } |
0 commit comments