Make AuthGuard integration decouple from knowing the URL at startup
Every language's AuthGuard client/service used "no URL at construction" to mean "auth disabled forever" - a live SetBaseUrl/setBaseUrl push from ProjectConfigurator could only ever retarget an *already-enabled* client, never turn one on for the first time. That made AUTHGUARD_URL a de-facto required env var everywhere, even though ProjectConfigurator can push it live after startup. - Go: shared/backend/auth's New/NewWithStore now default to a "pending" Service (still enabled, fails closed until a URL arrives) instead of disabled when authGuardURL is empty; add an explicit Disabled() option for the genuine standalone/local-dev opt-out. - Python/Node/Java: add setBaseUrl/set_base_url to each AuthClient so a client built with an empty base URL can be retargeted in place once a URL is pushed, the same as the Go client already supported. - Python/Node: add a coreconfig module (Info/Store), the framework-agnostic equivalent of shared/backend/coreconfig (Go), for services in those languages to receive ProjectConfigurator's core-services push at all. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c09957189f
commit
dcfdb3d7af
18 files changed
+747
-46
No files matched your search
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user