Sync shared snapshot from f585792
This commit is contained in:
1 parent
803c3ab3ce
commit
aae8d3807d
4 files changed
+71
-4
No files matched your search
+30
@@ -98,6 +98,14 @@ public final class AuthClient {
|
||||
public static final String DEFAULT_COOKIE_NAME = "auth_session";
|
||||
public static final int DEFAULT_TIMEOUT_MILLIS = 5000;
|
||||
|
||||
/**
|
||||
* Header attached to every request when the client is constructed with a projectKey,
|
||||
* identifying the calling project to AuthGuard so it can enforce per-user project-access
|
||||
* restriction (see AuthGuard's project-scope admin UI). Mirrors Go's
|
||||
* {@code authclient.ProjectKeyHeader}.
|
||||
*/
|
||||
public static final String PROJECT_KEY_HEADER = "X-Project-Key";
|
||||
|
||||
private static final Pattern VALID_PATTERN = Pattern.compile("\"valid\"\\s*:\\s*(true|false)");
|
||||
private static final Pattern USER_ID_PATTERN = Pattern.compile("\"user_id\"\\s*:\\s*(-?\\d+)");
|
||||
private static final Pattern USERNAME_PATTERN = Pattern.compile("\"username\"\\s*:\\s*\"((?:[^\"\\\\]|\\\\.)*)\"");
|
||||
@@ -110,6 +118,7 @@ public final class AuthClient {
|
||||
private final String baseUrl;
|
||||
private final String cookieName;
|
||||
private final int timeoutMillis;
|
||||
private final String projectKey;
|
||||
|
||||
public AuthClient(String baseUrl) {
|
||||
this(baseUrl, DEFAULT_COOKIE_NAME, DEFAULT_TIMEOUT_MILLIS);
|
||||
@@ -126,9 +135,24 @@ public final class AuthClient {
|
||||
* @param timeoutMillis connect and read timeout used by {@link #validate(String)}.
|
||||
*/
|
||||
public AuthClient(String baseUrl, String cookieName, int timeoutMillis) {
|
||||
this(baseUrl, cookieName, timeoutMillis, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param baseUrl AuthGuard's base URL (e.g. {@code https://auth.example.com}), no trailing slash required.
|
||||
* @param cookieName cookie {@link #extractToken} falls back to when there is no Authorization header;
|
||||
* blank/null defaults to {@link #DEFAULT_COOKIE_NAME}.
|
||||
* @param timeoutMillis connect and read timeout used by {@link #validate(String)}.
|
||||
* @param projectKey identifies this project to AuthGuard on every request (see
|
||||
* {@link #PROJECT_KEY_HEADER}), enabling per-user project-access
|
||||
* restriction. Normally a project's own lowercase identifier, matching
|
||||
* the key ProjectConfigurator registers it under. Blank/null disables it.
|
||||
*/
|
||||
public AuthClient(String baseUrl, String cookieName, int timeoutMillis, String projectKey) {
|
||||
this.baseUrl = normalizeBaseUrl(baseUrl);
|
||||
this.cookieName = cookieName == null || cookieName.trim().isEmpty() ? DEFAULT_COOKIE_NAME : cookieName.trim();
|
||||
this.timeoutMillis = timeoutMillis;
|
||||
this.projectKey = projectKey == null ? "" : projectKey.trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -583,6 +607,9 @@ public final class AuthClient {
|
||||
connection.setRequestMethod(method);
|
||||
connection.setConnectTimeout(timeoutMillis);
|
||||
connection.setReadTimeout(timeoutMillis);
|
||||
if (!projectKey.isEmpty()) {
|
||||
connection.setRequestProperty(PROJECT_KEY_HEADER, projectKey);
|
||||
}
|
||||
if (token != null && !token.isEmpty()) {
|
||||
connection.setRequestProperty("Authorization", BEARER_PREFIX + token);
|
||||
}
|
||||
@@ -673,6 +700,9 @@ public final class AuthClient {
|
||||
URL url = new URL(baseUrl + "/api/session/validate");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
if (!projectKey.isEmpty()) {
|
||||
connection.setRequestProperty(PROJECT_KEY_HEADER, projectKey);
|
||||
}
|
||||
connection.setRequestProperty("Authorization", BEARER_PREFIX + token);
|
||||
connection.setConnectTimeout(timeoutMillis);
|
||||
connection.setReadTimeout(timeoutMillis);
|
||||
|
||||
Reference in new issue
Block a user