Skip to content

Commit 01dc851

Browse files
authored
Merge pull request #71 from thiagoelg/fix-http-root-path
Remove httpRootPath prefix from routes
2 parents 6996e11 + 0dfd55b commit 01dc851

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.quarkus.oidc.proxy;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.htmlunit.SilentCssErrorHandler;
6+
import org.htmlunit.TextPage;
7+
import org.htmlunit.WebClient;
8+
import org.htmlunit.html.HtmlForm;
9+
import org.htmlunit.html.HtmlPage;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import io.quarkus.test.QuarkusDevModeTest;
14+
15+
public class OidcProxyHttpRootPathTestCase {
16+
17+
private static Class<?>[] testClasses = {
18+
OidcServiceResource.class,
19+
OidcWebAppResource.class,
20+
ServiceApiClient.class
21+
};
22+
23+
@RegisterExtension
24+
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
25+
.withApplicationRoot((jar) -> jar
26+
.addClasses(testClasses)
27+
.addAsResource("application-http-root-path.properties", "application.properties"));
28+
29+
@Test
30+
public void testOidcProxy() throws Exception {
31+
32+
try (final WebClient webClient = createWebClient()) {
33+
HtmlPage page = webClient.getPage("http://localhost:8080/my-custom-root-path/web-app");
34+
35+
assertEquals("Sign in to quarkus", page.getTitleText());
36+
37+
HtmlForm loginForm = page.getForms().get(0);
38+
39+
loginForm.getInputByName("username").setValueAttribute("alice");
40+
loginForm.getInputByName("password").setValueAttribute("alice");
41+
42+
TextPage textPage = loginForm.getButtonByName("login").click();
43+
44+
assertEquals("web-app: ID alice, service: Bearer alice", textPage.getContent());
45+
46+
webClient.getCookieManager().clearCookies();
47+
}
48+
49+
}
50+
51+
private WebClient createWebClient() {
52+
WebClient webClient = new WebClient();
53+
webClient.setCssErrorHandler(new SilentCssErrorHandler());
54+
return webClient;
55+
}
56+
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
quarkus.http.root-path=/my-custom-root-path
2+
3+
quarkus.oidc.web-app.auth-server-url=http://localhost:8080/my-custom-root-path/q/oidc
4+
quarkus.oidc.web-app.client-id=${quarkus.oidc.client-id}
5+
quarkus.oidc.web-app.credentials.secret=secret
6+
quarkus.oidc.web-app.application-type=web-app
7+
quarkus.oidc.web-app.authentication.cookie-path=/my-custom-root-path/web-app
8+
quarkus.rest-client.service-api-client.url=http://localhost:8080/my-custom-root-path/service
9+
10+
quarkus.log.category."org.htmlunit".level=ERROR

runtime/src/main/java/io/quarkus/oidc/proxy/runtime/OidcProxy.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ public class OidcProxy {
4444
final OidcProxyConfig oidcProxyConfig;
4545
final WebClient client;
4646
final String configuredClientSecret;
47+
final String httpRootPath;
4748

48-
public OidcProxy(TenantConfigBean tenantConfig, OidcProxyConfig oidcProxyConfig) {
49+
public OidcProxy(TenantConfigBean tenantConfig, OidcProxyConfig oidcProxyConfig, String httpRootPath) {
4950
TenantConfigContext tenantConfigContext = oidcProxyConfig.tenantId().isEmpty() ? tenantConfig.getDefaultTenant()
5051
: tenantConfig.getStaticTenantsConfig().get(oidcProxyConfig.tenantId().get());
5152
this.oidcTenantConfig = tenantConfigContext.getOidcTenantConfig();
5253
this.oidcMetadata = tenantConfigContext.getOidcMetadata();
5354
this.client = tenantConfigContext.getOidcProviderClient().getWebClient();
5455
this.oidcProxyConfig = oidcProxyConfig;
5556
this.configuredClientSecret = OidcCommonUtils.clientSecret(oidcTenantConfig.credentials);
57+
this.httpRootPath = httpRootPath;
5658
}
5759

58-
public void setup(Router router, String httpRootPath) {
60+
public void setup(Router router) {
5961
if (oidcTenantConfig.applicationType.orElse(ApplicationType.SERVICE) == ApplicationType.WEB_APP) {
6062
throw new ConfigurationException("OIDC Proxy can only be used with OIDC service applications");
6163
}
@@ -72,16 +74,16 @@ public void setup(Router router, String httpRootPath) {
7274
throw new ConfigurationException(
7375
"Unsupported OIDC service client authentication method");
7476
}
75-
router.get(httpRootPath + oidcProxyConfig.rootPath() + OidcConstants.WELL_KNOWN_CONFIGURATION)
77+
router.get(oidcProxyConfig.rootPath() + OidcConstants.WELL_KNOWN_CONFIGURATION)
7678
.handler(this::wellKnownConfig);
7779
if (oidcMetadata.getJsonWebKeySetUri() != null) {
78-
router.get(httpRootPath + oidcProxyConfig.rootPath() + oidcProxyConfig.jwksPath()).handler(this::jwks);
80+
router.get(oidcProxyConfig.rootPath() + oidcProxyConfig.jwksPath()).handler(this::jwks);
7981
}
8082
if (oidcMetadata.getUserInfoUri() != null && oidcProxyConfig.allowIdToken()) {
81-
router.get(httpRootPath + oidcProxyConfig.rootPath() + oidcProxyConfig.userInfoPath()).handler(this::userinfo);
83+
router.get(oidcProxyConfig.rootPath() + oidcProxyConfig.userInfoPath()).handler(this::userinfo);
8284
}
83-
router.get(httpRootPath + oidcProxyConfig.rootPath() + oidcProxyConfig.authorizationPath()).handler(this::authorize);
84-
router.post(httpRootPath + oidcProxyConfig.rootPath() + oidcProxyConfig.tokenPath()).handler(this::token);
85+
router.get(oidcProxyConfig.rootPath() + oidcProxyConfig.authorizationPath()).handler(this::authorize);
86+
router.post(oidcProxyConfig.rootPath() + oidcProxyConfig.tokenPath()).handler(this::token);
8587
if (oidcTenantConfig.authentication.redirectPath.isPresent()) {
8688
if (!oidcProxyConfig.externalRedirectUri().isPresent()) {
8789
throw new ConfigurationException("oidc-proxy.external-redirect-uri property must be configured because"
@@ -91,7 +93,7 @@ public void setup(Router router, String httpRootPath) {
9193
}
9294

9395
if (oidcMetadata.getEndSessionUri() != null) {
94-
router.get(httpRootPath + oidcProxyConfig.rootPath() + oidcProxyConfig.endSessionPath()).handler(this::endSession);
96+
router.get(oidcProxyConfig.rootPath() + oidcProxyConfig.endSessionPath()).handler(this::endSession);
9597
if (oidcTenantConfig.logout().postLogoutPath().isPresent()) {
9698
if (!oidcProxyConfig.externalPostLogoutUri().isPresent()) {
9799
throw new ConfigurationException("oidc-proxy.external-post-logout-uri property must be configured because"
@@ -511,6 +513,7 @@ private String buildUri(RoutingContext context, String path) {
511513
: context.request().scheme();
512514
return new StringBuilder(scheme).append("://")
513515
.append(authority)
516+
.append(httpRootPath)
514517
.append(path)
515518
.toString();
516519
}

runtime/src/main/java/io/quarkus/oidc/proxy/runtime/OidcProxyRecorder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public OidcProxyRecorder(RuntimeValue<OidcProxyConfig> oidcProxyConfig) {
1717

1818
public void setupRoutes(BeanContainer beanContainer, RuntimeValue<Router> routerValue, String httpRootPath) {
1919
TenantConfigBean oidcTenantBean = beanContainer.beanInstance(TenantConfigBean.class);
20-
OidcProxy proxy = new OidcProxy(oidcTenantBean, oidcProxyConfig.getValue());
20+
OidcProxy proxy = new OidcProxy(oidcTenantBean, oidcProxyConfig.getValue(), httpRootPath);
2121
Router router = routerValue.getValue();
22-
proxy.setup(router, httpRootPath);
22+
proxy.setup(router);
2323
}
2424
}

0 commit comments

Comments
 (0)