Sync shared snapshot from de9b599
This commit is contained in:
1 parent
eb113a7029
commit
49e3c7d561
9 files changed
+697
-44
No files matched your search
@@ -57,15 +57,25 @@ export class APIError extends Error {
|
||||
export class AuthClient {
|
||||
/**
|
||||
* @param {string} baseUrl - AuthGuard's base URL (e.g. from AUTHGUARD_URL), no trailing slash required.
|
||||
* May be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080") -
|
||||
* "https://" is assumed in that case, so a deployment only needs to name the
|
||||
* host, not repeat the scheme; an explicit "http://" (for a plain-HTTP
|
||||
* local/LAN AuthGuard) is left as-is.
|
||||
* @param {{cookieName?: string, timeoutMs?: number, fetchImpl?: typeof fetch}} [options]
|
||||
*/
|
||||
constructor(baseUrl, options = {}) {
|
||||
this.baseUrl = String(baseUrl ?? '').trim().replace(/\/+$/, '');
|
||||
this.baseUrl = AuthClient._normalizeBaseUrl(baseUrl);
|
||||
this.cookieName = options.cookieName ?? DEFAULT_COOKIE_NAME;
|
||||
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
||||
this.fetchImpl = options.fetchImpl ?? fetch;
|
||||
}
|
||||
|
||||
static _normalizeBaseUrl(baseUrl) {
|
||||
const trimmed = String(baseUrl ?? '').trim().replace(/\/+$/, '');
|
||||
if (!trimmed) return trimmed;
|
||||
return /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
||||
}
|
||||
|
||||
// --- session validation (the original, minimal surface) -----------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,26 @@ function recordingFetch(responses) {
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
test('constructor assumes https for a bare host', () => {
|
||||
assert.equal(new AuthClient('auth.example.com').baseUrl, 'https://auth.example.com');
|
||||
assert.equal(new AuthClient('192.168.1.50:8080').baseUrl, 'https://192.168.1.50:8080');
|
||||
});
|
||||
|
||||
test('constructor leaves an explicit scheme alone', () => {
|
||||
assert.equal(new AuthClient('http://auth.example.com').baseUrl, 'http://auth.example.com');
|
||||
assert.equal(new AuthClient('https://auth.example.com').baseUrl, 'https://auth.example.com');
|
||||
assert.equal(new AuthClient('HTTP://auth.example.com').baseUrl, 'HTTP://auth.example.com');
|
||||
});
|
||||
|
||||
test('constructor strips a trailing slash before checking the scheme', () => {
|
||||
assert.equal(new AuthClient('auth.example.com/').baseUrl, 'https://auth.example.com');
|
||||
});
|
||||
|
||||
test('constructor trims whitespace and leaves an empty base URL empty', () => {
|
||||
assert.equal(new AuthClient(' auth.example.com ').baseUrl, 'https://auth.example.com');
|
||||
assert.equal(new AuthClient('').baseUrl, '');
|
||||
});
|
||||
|
||||
test('validate resolves the user on a valid session', async () => {
|
||||
const client = new AuthClient('https://auth.example.com', {
|
||||
fetchImpl: fakeFetch([{ status: 200, body: { valid: true, user_id: 7, username: 'gabriel', role: 'admin' } }]),
|
||||
|
||||
Reference in new issue
Block a user