Sync shared snapshot from dcfdb3d
Docker / Docker Build & Push (push) Successful in 6m0s

This commit is contained in:
Gabriel20xx committed 2026-09-14 09:36:48 +02:00
1 parent 8d7c2f69ff
commit a018f3da4c
18 files changed
+747 -46

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@ Shared account-management, TOTP, admin reset-code, and AuthGuard client primitiv
- `Roles` - shared account-management roles (`superadmin`/`admin`/`user`) and permission checks;
- `Totp` - RFC 6238 TOTP secret generation, provisioning URIs, and verification;
- `ResetManager` - a short-lived, one-time admin reset-code primitive printed to the server console;
- `AuthClient` - a client for a centralized AuthGuard instance's `GET /api/session/validate`, for services that want one shared identity instead of their own local user table. Mirrors `shared/backend/authclient` (Go) and `shared/packages/node-backend/src/authclient.js` (Node) - keep the three in sync if any of them changes shape. Additive: no project is wired up to use it just because this class exists.
- `AuthClient` - a client for a centralized AuthGuard instance's `GET /api/session/validate`, for services that want one shared identity instead of their own local user table. Mirrors `shared/backend/authclient` (Go) and `shared/packages/node-backend/src/authclient.js` (Node) - keep the three in sync if any of them changes shape. Additive: no project is wired up to use it just because this class exists. `setBaseUrl(url)` retargets an existing client's AuthGuard URL in place - the natural target for a ProjectConfigurator core-services push handler - so a client can be constructed with `new AuthClient("")` (usable right away, failing closed with `IllegalStateException` until a real URL arrives) rather than needing one known up front. Unlike the Go/Python/Node siblings, this package has no `coreconfig` module of its own: MegaBasterd, its only consumer, wires the `PUT /api/core-services/internal` route directly against a plain field holding the current `AuthGuardUrl`.
## How this package is actually consumed
@@ -115,7 +115,15 @@ public final class AuthClient {
/** Used only by the full-proxy methods below; see the class javadoc's Jackson tradeoff note. */
private static final ObjectMapper JSON = new ObjectMapper();
private final String baseUrl;
/**
* Not final: {@link #setBaseUrl} retargets it live (e.g. from a project's own
* coreconfig-equivalent push handler), so a Service holding this client can start with
* baseUrl empty ("pending" - see {@link #setBaseUrl}'s doc comment) and still be usable
* once a URL is pushed, without discarding and rebuilding the whole client. {@code volatile}
* since HTTP requests happen on arbitrary threads and String is immutable, so a plain
* volatile field is enough for safe publication without a lock.
*/
private volatile String baseUrl;
private final String cookieName;
private final int timeoutMillis;
private final String projectKey;
@@ -155,6 +163,24 @@ public final class AuthClient {
this.projectKey = projectKey == null ? "" : projectKey.trim().toLowerCase();
}
/**
* Atomically replaces the AuthGuard base URL this client calls, normalizing it the same way
* the constructor does. Mirrors Go's {@code authclient.Client.SetBaseURL} and Python's
* {@code AuthClient.set_base_url}: the intended use is a project's own coreconfig-equivalent
* push handler calling this on its live {@code AuthClient} instance so a freshly pushed
* AuthGuard URL takes effect on the client's very next call with no restart - including the
* very first URL a client constructed with {@code baseUrl == ""} (a "pending" client, not yet
* configured but still meant to be used, as opposed to one a project deliberately built with
* no AuthGuard integration at all) ever receives. A blank {@code baseUrl} is ignored (leaving
* the previous value in place) rather than leaving the client unable to reach AuthGuard at all.
*/
public void setBaseUrl(String baseUrl) {
String normalized = normalizeBaseUrl(baseUrl);
if (!normalized.isEmpty()) {
this.baseUrl = normalized;
}
}
/**
* Calls AuthGuard's {@code GET /api/session/validate} with {@code token} as a Bearer credential and
* returns the resolved user. Throws {@link InvalidSessionException} when AuthGuard reports the session
@@ -164,7 +190,8 @@ public final class AuthClient {
* is not the same fact as "this visitor is unauthenticated".
*/
public User validate(String token) throws IOException {
if (baseUrl.isEmpty()) {
String currentBaseUrl = baseUrl;
if (currentBaseUrl.isEmpty()) {
throw new IllegalStateException("authclient: client has no base URL");
}
String trimmed = token == null ? "" : token.trim();
@@ -172,7 +199,7 @@ public final class AuthClient {
throw new InvalidSessionException();
}
HttpURLConnection connection = openConnection(trimmed);
HttpURLConnection connection = openConnection(currentBaseUrl, trimmed);
int status;
String body;
try {
@@ -598,11 +625,12 @@ public final class AuthClient {
* {@code Client.call}/Node's {@code AuthClient#_call}/Python's {@code AuthClient._call} exactly.
*/
private CallResult call(String method, String path, String token, Object body) throws IOException {
if (baseUrl.isEmpty()) {
String currentBaseUrl = baseUrl;
if (currentBaseUrl.isEmpty()) {
throw new IllegalStateException("authclient: client has no base URL");
}
URL url = new URL(baseUrl + path);
URL url = new URL(currentBaseUrl + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setConnectTimeout(timeoutMillis);
@@ -696,8 +724,8 @@ public final class AuthClient {
return "";
}
private HttpURLConnection openConnection(String token) throws IOException {
URL url = new URL(baseUrl + "/api/session/validate");
private HttpURLConnection openConnection(String currentBaseUrl, String token) throws IOException {
URL url = new URL(currentBaseUrl + "/api/session/validate");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (!projectKey.isEmpty()) {
@@ -158,6 +158,37 @@ class AuthClientTest {
assertThrows(InvalidSessionException.class, () -> client.validate("bad-token"));
}
@Test
void validateFailsClosedOnAClientWithNoBaseUrlYet() {
// A client constructed with "" (pending - no AuthGuard URL known yet, e.g. before a
// coreconfig-equivalent push arrives) must fail closed rather than silently letting
// every request through; see AuthClient's setBaseUrl doc comment.
AuthClient client = new AuthClient("");
assertThrows(IllegalStateException.class, () -> client.validate("some-token"));
}
@Test
void setBaseUrlRetargetsAPendingClientToItsFirstRealUrl() throws Exception {
server = fakeAuthGuard(200, "{\"valid\":true,\"user_id\":7,\"username\":\"gabriel\",\"role\":\"admin\"}");
AuthClient client = new AuthClient("");
assertThrows(IllegalStateException.class, () -> client.validate("good-token"));
client.setBaseUrl(baseUrl(server));
AuthClient.User user = client.validate("good-token");
assertEquals("gabriel", user.username);
}
@Test
void setBaseUrlIgnoresABlankValue() {
AuthClient client = new AuthClient("https://auth.example.com");
client.setBaseUrl(" ");
assertEquals("https://auth.example.com", client.baseUrlForTest());
}
@Test
void extractTokenPrefersAuthorizationHeaderOverCookie() {
AuthClient client = new AuthClient("https://auth.example.com");