From b88bfdf99dd6573d70e1910ae6bf5dd8833798b8 Mon Sep 17 00:00:00 2001 From: Anindra Date: Tue, 24 Nov 2026 14:46:43 +0530 Subject: [PATCH 1/2] feat(auth): support per-identity proxy scopes A proxy can send GATEWAY_PROXY_AUTH_SCOPES_HEADER (e.g. X-Forwarded-Scopes) to give one SSO identity read and another read,write. When configured, a missing or unsupported header fails closed instead of inheriting the global scope set. --- docs/SSO.md | 36 ++++++++ .../com/anindra/path/api/ApiException.java | 4 + .../com/anindra/path/config/AuthFilter.java | 29 +++++- .../path/config/GatewayProperties.java | 7 ++ .../src/main/resources/application.yml | 1 + .../path/ProxyAuthScopesHeaderTest.java | 89 +++++++++++++++++++ 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 path-gateway/src/test/java/com/anindra/path/ProxyAuthScopesHeaderTest.java diff --git a/docs/SSO.md b/docs/SSO.md index 83a440c..42b98e5 100644 --- a/docs/SSO.md +++ b/docs/SSO.md @@ -39,6 +39,42 @@ skip its token screen — a signed-in browser lands straight on the dashboard. Every admin write is audited against the identity the proxy supplied, so `GET /admin/audit` answers "who changed this" with an email, not "shared-admin". +## Per-user read-only roles + +By default every proxy-authenticated identity gets the same scopes. To make one +SSO user read-only while another can write, have the proxy send scopes per +identity as well: + +```yaml +GATEWAY_PROXY_AUTH: "true" +GATEWAY_PROXY_AUTH_HEADER: X-Forwarded-User +GATEWAY_PROXY_AUTH_SCOPES_HEADER: X-Forwarded-Scopes +# Fallback only when SCOPES_HEADER is not configured: +GATEWAY_PROXY_AUTH_SCOPES: "read,write" +``` + +The proxy must then send both headers on every `/admin/**` request: + +```text +X-Forwarded-User: viewer@example.com +X-Forwarded-Scopes: read +``` + +`X-Forwarded-Scopes: read,write` grants writes. When `SCOPES_HEADER` is +configured it is **authoritative**: a missing, empty or unsupported scope header +fails closed (`401 proxy_scope_missing` / `403 proxy_scope_invalid`) instead of +falling back to the broader `GATEWAY_PROXY_AUTH_SCOPES` default. + +The admin console reflects this automatically: `/admin/settings` returns the +per-identity scopes and `can_write`, so a read-only user sees the console but +every mutation button is disabled. + +Configure the header mapping in your IdP/proxy. For Authentik, a property mapping +on the proxy provider can emit `X-Forwarded-Scopes` from group membership; for +oauth2-proxy/Keycloak, map a group or realm role to the same header. The gateway +does not read groups or roles itself — it trusts the header, exactly like the +identity header. + ## Authentik (recommended, self-hosted with Dokploy) 1. Authentik → **Providers → Create → Proxy**, mode *Forward auth (single application)*. diff --git a/path-gateway/src/main/java/com/anindra/path/api/ApiException.java b/path-gateway/src/main/java/com/anindra/path/api/ApiException.java index 3492a50..3be5dd1 100644 --- a/path-gateway/src/main/java/com/anindra/path/api/ApiException.java +++ b/path-gateway/src/main/java/com/anindra/path/api/ApiException.java @@ -40,6 +40,10 @@ public static ApiException forbidden(String message) { return new ApiException(403, "permission_error", "forbidden", message); } + public static ApiException forbidden(String message, String code) { + return new ApiException(403, "permission_error", code, message); + } + public static ApiException notFound(String message, String code) { return new ApiException(404, "invalid_request_error", code, message); } diff --git a/path-gateway/src/main/java/com/anindra/path/config/AuthFilter.java b/path-gateway/src/main/java/com/anindra/path/config/AuthFilter.java index ed603a3..414c43a 100644 --- a/path-gateway/src/main/java/com/anindra/path/config/AuthFilter.java +++ b/path-gateway/src/main/java/com/anindra/path/config/AuthFilter.java @@ -122,10 +122,16 @@ private void requireProxyIdentity(HttpServletRequest request) { "proxy_identity_missing"); } java.util.Set scopes = java.util.Arrays.stream( - props.getProxyAuth().getScopes().split(",")) + scopeSource(request).split(",")) .map(String::trim) - .filter(s -> !s.isEmpty()) + .map(String::toLowerCase) + .filter(s -> s.equals("read") || s.equals("write")) .collect(java.util.stream.Collectors.toSet()); + if (scopes.isEmpty()) { + throw ApiException.forbidden( + "The proxy supplied no supported admin scope (read/write).", + "proxy_scope_invalid"); + } request.setAttribute(ADMIN_PRINCIPAL_ATTR, new com.anindra.path.admin.TenancyService.Principal(identity.trim(), scopes)); boolean mutating = !"GET".equalsIgnoreCase(request.getMethod()) @@ -135,6 +141,25 @@ private void requireProxyIdentity(HttpServletRequest request) { } } + /** + * The global scope default, or the per-identity header when {@code scopes-header} is + * configured. When the header is configured it is authoritative: a missing value fails + * closed instead of silently inheriting a broader default. + */ + private String scopeSource(HttpServletRequest request) { + String scopesHeader = props.getProxyAuth().getScopesHeader(); + if (scopesHeader != null && !scopesHeader.isBlank()) { + String provided = request.getHeader(scopesHeader); + if (provided == null || provided.isBlank()) { + throw ApiException.unauthorized( + "Admin access requires the proxy to send " + scopesHeader + ".", + "proxy_scope_missing"); + } + return provided; + } + return props.getProxyAuth().getScopes(); + } + private boolean isPublicModelsRequest(HttpServletRequest request) { return props.isPublicModels() && "GET".equalsIgnoreCase(request.getMethod()) diff --git a/path-gateway/src/main/java/com/anindra/path/config/GatewayProperties.java b/path-gateway/src/main/java/com/anindra/path/config/GatewayProperties.java index e46debc..4f9d60a 100644 --- a/path-gateway/src/main/java/com/anindra/path/config/GatewayProperties.java +++ b/path-gateway/src/main/java/com/anindra/path/config/GatewayProperties.java @@ -141,6 +141,13 @@ public static class ProxyAuth { private String header = "X-Forwarded-User"; /** Scopes granted to a proxy-authenticated identity. */ private String scopes = "read,write"; + /** + * Optional header carrying per-identity scopes. When set, the proxy must send it for + * every admin request; a missing/empty header fails closed instead of inheriting + * the broader {@link #scopes} default. Use it to give one SSO user read-only access + * while another can write. + */ + private String scopesHeader = ""; } @Getter diff --git a/path-gateway/src/main/resources/application.yml b/path-gateway/src/main/resources/application.yml index fcaf092..452baf6 100644 --- a/path-gateway/src/main/resources/application.yml +++ b/path-gateway/src/main/resources/application.yml @@ -88,6 +88,7 @@ gateway: enabled: ${GATEWAY_PROXY_AUTH:false} header: ${GATEWAY_PROXY_AUTH_HEADER:X-Forwarded-User} scopes: ${GATEWAY_PROXY_AUTH_SCOPES:read,write} + scopes-header: ${GATEWAY_PROXY_AUTH_SCOPES_HEADER:} admin-auth: max-failures: ${GATEWAY_ADMIN_AUTH_FAILURES:10} window: ${GATEWAY_ADMIN_AUTH_WINDOW:5m} diff --git a/path-gateway/src/test/java/com/anindra/path/ProxyAuthScopesHeaderTest.java b/path-gateway/src/test/java/com/anindra/path/ProxyAuthScopesHeaderTest.java new file mode 100644 index 0000000..18015c9 --- /dev/null +++ b/path-gateway/src/test/java/com/anindra/path/ProxyAuthScopesHeaderTest.java @@ -0,0 +1,89 @@ +package com.anindra.path; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Per-identity SSO scopes: the proxy sends both the user and a scope header. The header + * is authoritative, so a viewer can read and an admin can write without sharing one + * global scope set. + */ +@SpringBootTest(properties = { + "spring.datasource.url=jdbc:h2:mem:proxyauthscopes;MODE=PostgreSQL;DB_CLOSE_DELAY=-1", + "spring.jpa.hibernate.ddl-auto=validate", + "gateway.admin-token=test-admin", + "gateway.bootstrap-demo-key=false", + "gateway.master-key-path=./target/test-master.key", + "gateway.proxy-auth.enabled=true", + "gateway.proxy-auth.header=X-Forwarded-User", + "gateway.proxy-auth.scopes=read,write", + "gateway.proxy-auth.scopes-header=X-Forwarded-Scopes" +}) +@AutoConfigureMockMvc +class ProxyAuthScopesHeaderTest { + + @Autowired + private MockMvc mvc; + + @Test + void missingScopeHeaderFailsClosed() throws Exception { + mvc.perform(get("/admin/providers").header("X-Forwarded-User", "viewer@example.com")) + .andExpect(status().isUnauthorized()) + .andExpect(jsonPath("$.error.code").value("proxy_scope_missing")); + } + + @Test + void readScopeCanReadButCannotWrite() throws Exception { + mvc.perform(get("/admin/settings") + .header("X-Forwarded-User", "viewer@example.com") + .header("X-Forwarded-Scopes", "read")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.scopes[0]").value("read")) + .andExpect(jsonPath("$.can_write").value(false)); + + mvc.perform(get("/admin/providers") + .header("X-Forwarded-User", "viewer@example.com") + .header("X-Forwarded-Scopes", "read")) + .andExpect(status().isOk()); + + mvc.perform(post("/admin/providers") + .header("X-Forwarded-User", "viewer@example.com") + .header("X-Forwarded-Scopes", "read") + .contentType("application/json") + .content("{\"slug\":\"viewer-nope\",\"type\":\"openai\",\"base_url\":\"https://example.test/v1\"}")) + .andExpect(status().isForbidden()); + } + + @Test + void writeScopeCanWrite() throws Exception { + mvc.perform(get("/admin/settings") + .header("X-Forwarded-User", "admin@example.com") + .header("X-Forwarded-Scopes", "read,write")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.can_write").value(true)); + + mvc.perform(post("/admin/providers") + .header("X-Forwarded-User", "admin@example.com") + .header("X-Forwarded-Scopes", "read,write") + .contentType("application/json") + .content("{\"slug\":\"admin-ok\",\"type\":\"openai\",\"base_url\":\"https://example.test/v1\"}")) + .andExpect(status().isOk()); + } + + @Test + void unsupportedScopeHeaderFailsClosed() throws Exception { + mvc.perform(get("/admin/providers") + .header("X-Forwarded-User", "viewer@example.com") + .header("X-Forwarded-Scopes", "admin")) + .andExpect(status().isForbidden()) + .andExpect(jsonPath("$.error.code").value("proxy_scope_invalid")); + } +} From 5b5c089c650a3e25b077d257a85335b6c43fcc22 Mon Sep 17 00:00:00 2001 From: Anindra Date: Tue, 24 Nov 2026 14:59:30 +0530 Subject: [PATCH 2/2] release: v0.2.8 Per-identity SSO scopes: the proxy can send X-Forwarded-Scopes so one identity is read-only and another can write. Missing or unsupported scope headers fail closed instead of inheriting the global scope set. --- CHANGELOG.md | 1 + changelog/v0.2.8.md | 24 ++++++++++++++++++++++++ deploy/dokploy/README.md | 2 +- docs/SPRING-AI.md | 2 +- path-gateway/pom.xml | 2 +- path-spring-ai/pom.xml | 2 +- pom.xml | 2 +- 7 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 changelog/v0.2.8.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eec869..bafc268 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ verbatim as the body of the matching GitHub release. | Version | Date | Notes | |---|---|---| +| v0.2.8 | 2026-11-24 | [changelog/v0.2.8.md](changelog/v0.2.8.md) | | v0.2.7 | 2026-11-23 | [changelog/v0.2.7.md](changelog/v0.2.7.md) | | v0.2.6 | 2026-11-23 | [changelog/v0.2.6.md](changelog/v0.2.6.md) | | v0.2.5 | 2026-11-23 | [changelog/v0.2.5.md](changelog/v0.2.5.md) | diff --git a/changelog/v0.2.8.md b/changelog/v0.2.8.md new file mode 100644 index 0000000..1b62887 --- /dev/null +++ b/changelog/v0.2.8.md @@ -0,0 +1,24 @@ +# v0.2.8 + +Per-identity SSO scopes. + +## Added + +- **Per-identity SSO scopes.** `GATEWAY_PROXY_AUTH_SCOPES_HEADER` lets an authenticating + reverse proxy send a scope header such as `X-Forwarded-Scopes` alongside the identity + header. One SSO user can be read-only while another gets `read,write`. When the scope + header is configured it is authoritative; a missing, empty or unsupported value fails + closed instead of inheriting the broader `GATEWAY_PROXY_AUTH_SCOPES` default. +- **Read-only admin access is visible in the console.** `/admin/settings` returns the + identity's scopes and `can_write`, so a read-only SSO user or `adm_...` token sees the + console with write actions disabled. + +## Changed + +- Proxy scope parsing accepts only `read` and `write`; unknown values are rejected with + `403 proxy_scope_invalid`. + +## Notes + +- Configure the proxy to send the scope header and strip any client-supplied + `X-Forwarded-*` headers. The gateway must remain unreachable except through the proxy. diff --git a/deploy/dokploy/README.md b/deploy/dokploy/README.md index 1e75ea3..08ee683 100644 --- a/deploy/dokploy/README.md +++ b/deploy/dokploy/README.md @@ -20,7 +20,7 @@ database. The console is always a plain Application. 3. Compose path: `deploy/dokploy/compose.yml`. 4. **Trigger Type: On Tag**, with auto-deploy enabled. The gateway is a production service; a pull request or a push to `Develop` must not restart it. A release tag - (`v0.2.7`) is the only thing that deploys — see "When it deploys" below. + (`v0.2.8`) is the only thing that deploys — see "When it deploys" below. 5. **Environment** (the compose file references all of these): ```bash diff --git a/docs/SPRING-AI.md b/docs/SPRING-AI.md index 6b66519..bcd63df 100644 --- a/docs/SPRING-AI.md +++ b/docs/SPRING-AI.md @@ -13,7 +13,7 @@ on Spring AI; the gateway never depends on it. io.github.an1ndra path-spring-ai - 0.2.7 + 0.2.8 ``` diff --git a/path-gateway/pom.xml b/path-gateway/pom.xml index f35877c..c5b770e 100644 --- a/path-gateway/pom.xml +++ b/path-gateway/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.7 + 0.2.8 ../pom.xml diff --git a/path-spring-ai/pom.xml b/path-spring-ai/pom.xml index ee6d67b..4dbd481 100644 --- a/path-spring-ai/pom.xml +++ b/path-spring-ai/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.7 + 0.2.8 ../pom.xml diff --git a/pom.xml b/pom.xml index 2cb94fb..ec3a81b 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ io.github.an1ndra path-parent - 0.2.7 + 0.2.8 pom path-parent Build parent for Path, the provider-agnostic LLM gateway, and its Spring AI adapter