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
@@ -25,6 +25,12 @@
|
||||
const DEFAULT_COOKIE_NAME = 'auth_session';
|
||||
const DEFAULT_TIMEOUT_MS = 5000;
|
||||
|
||||
// PROJECT_KEY_HEADER is the header AuthClient attaches to every request when
|
||||
// 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 authclient.ProjectKeyHeader.
|
||||
export const PROJECT_KEY_HEADER = 'X-Project-Key';
|
||||
|
||||
export class InvalidSessionError extends Error {
|
||||
constructor(message = 'authclient: invalid or missing session') {
|
||||
super(message);
|
||||
@@ -63,13 +69,22 @@ export class AuthClient {
|
||||
* AuthGuard on a local network is the common case for those), anything else
|
||||
* defaults to "https://". An explicit "http://" or "https://" is always left
|
||||
* as-is, overriding the inference.
|
||||
* @param {{cookieName?: string, timeoutMs?: number, fetchImpl?: typeof fetch}} [options]
|
||||
* @param {{cookieName?: string, timeoutMs?: number, fetchImpl?: typeof fetch, projectKey?: string}} [options]
|
||||
*/
|
||||
constructor(baseUrl, options = {}) {
|
||||
this.baseUrl = AuthClient._normalizeBaseUrl(baseUrl);
|
||||
this.cookieName = options.cookieName ?? DEFAULT_COOKIE_NAME;
|
||||
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
||||
this.fetchImpl = options.fetchImpl ?? fetch;
|
||||
// projectKey identifies this project to AuthGuard on every request (see
|
||||
// PROJECT_KEY_HEADER), enabling per-user project-access restriction.
|
||||
// Normally a project's own lowercase identifier, matching the key
|
||||
// ProjectConfigurator registers it under.
|
||||
this.projectKey = String(options.projectKey ?? '').trim().toLowerCase();
|
||||
}
|
||||
|
||||
_projectKeyHeaders() {
|
||||
return this.projectKey ? { [PROJECT_KEY_HEADER]: this.projectKey } : {};
|
||||
}
|
||||
|
||||
static _normalizeBaseUrl(baseUrl) {
|
||||
@@ -142,7 +157,7 @@ export class AuthClient {
|
||||
let response;
|
||||
try {
|
||||
response = await this.fetchImpl(`${this.baseUrl}/api/session/validate`, {
|
||||
headers: { Authorization: `Bearer ${trimmed}` },
|
||||
headers: { Authorization: `Bearer ${trimmed}`, ...this._projectKeyHeaders() },
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -220,7 +235,7 @@ export class AuthClient {
|
||||
if (!this.baseUrl) {
|
||||
throw new Error('authclient: client has no base URL');
|
||||
}
|
||||
const headers = {};
|
||||
const headers = { ...this._projectKeyHeaders() };
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
|
||||
Reference in new issue
Block a user