@block-auth.io/core 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +408 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -42
- package/dist/index.d.ts +54 -42
- package/dist/index.js +408 -76
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2107,10 +2107,43 @@ function connectBlockAuth(apiUrl) {
|
|
|
2107
2107
|
var LOGIN_POPUP_WIDTH = 380;
|
|
2108
2108
|
var LOGIN_POPUP_HEIGHT = 640;
|
|
2109
2109
|
|
|
2110
|
-
// src/auth/
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2110
|
+
// src/auth/idToken.ts
|
|
2111
|
+
function decodeIdTokenClaims(idToken) {
|
|
2112
|
+
const parts = idToken.split(".");
|
|
2113
|
+
if (parts.length !== 3) return null;
|
|
2114
|
+
try {
|
|
2115
|
+
const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
2116
|
+
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
2117
|
+
const bytes = Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
|
|
2118
|
+
const claims = JSON.parse(new TextDecoder().decode(bytes));
|
|
2119
|
+
if (typeof claims?.iss !== "string" || typeof claims.sub !== "string" || typeof claims.aud !== "string" || typeof claims.exp !== "number") {
|
|
2120
|
+
return null;
|
|
2121
|
+
}
|
|
2122
|
+
return {
|
|
2123
|
+
iss: claims.iss,
|
|
2124
|
+
sub: claims.sub,
|
|
2125
|
+
aud: claims.aud,
|
|
2126
|
+
exp: claims.exp,
|
|
2127
|
+
nonce: typeof claims.nonce === "string" ? claims.nonce : void 0
|
|
2128
|
+
};
|
|
2129
|
+
} catch {
|
|
2130
|
+
return null;
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
function isIdTokenExpired(claims) {
|
|
2134
|
+
return claims.exp * 1e3 <= Date.now();
|
|
2135
|
+
}
|
|
2136
|
+
function assertValidIdToken(idToken, expected) {
|
|
2137
|
+
const claims = decodeIdTokenClaims(idToken);
|
|
2138
|
+
if (!claims) throw new Error("Invalid idToken");
|
|
2139
|
+
const issuer = new URL(buildWsServerApiUrl("/", expected.wsUrl)).origin;
|
|
2140
|
+
if (claims.aud !== expected.apiKey || expected.nonce !== void 0 && claims.nonce !== expected.nonce || claims.iss !== issuer || claims.sub !== expected.address || isIdTokenExpired(claims)) {
|
|
2141
|
+
throw new Error("Invalid idToken");
|
|
2142
|
+
}
|
|
2143
|
+
return claims;
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
// src/auth/pkce.ts
|
|
2114
2147
|
function base64url(bytes) {
|
|
2115
2148
|
let binary = "";
|
|
2116
2149
|
for (const b of bytes) binary += String.fromCharCode(b);
|
|
@@ -2139,6 +2172,54 @@ async function postWsServer(path, body, wsUrl) {
|
|
|
2139
2172
|
}
|
|
2140
2173
|
return payload.message;
|
|
2141
2174
|
}
|
|
2175
|
+
function pkceRedirectUri() {
|
|
2176
|
+
return `${window.location.origin}/`;
|
|
2177
|
+
}
|
|
2178
|
+
async function createPkceChallenge() {
|
|
2179
|
+
const codeVerifier = randomString(32);
|
|
2180
|
+
return {
|
|
2181
|
+
codeVerifier,
|
|
2182
|
+
state: randomString(16),
|
|
2183
|
+
nonce: randomString(32),
|
|
2184
|
+
codeChallenge: await sha256Base64url(codeVerifier)
|
|
2185
|
+
};
|
|
2186
|
+
}
|
|
2187
|
+
async function requestAuthorizeUrl(options, challenge, responseMode) {
|
|
2188
|
+
const { url } = await postWsServer(
|
|
2189
|
+
"/auth/authorize",
|
|
2190
|
+
{
|
|
2191
|
+
apiKey: options.apiKey,
|
|
2192
|
+
redirectUri: pkceRedirectUri(),
|
|
2193
|
+
state: challenge.state,
|
|
2194
|
+
codeChallenge: challenge.codeChallenge,
|
|
2195
|
+
nonce: challenge.nonce,
|
|
2196
|
+
...responseMode ? { responseMode } : {}
|
|
2197
|
+
},
|
|
2198
|
+
options.wsUrl
|
|
2199
|
+
);
|
|
2200
|
+
return url;
|
|
2201
|
+
}
|
|
2202
|
+
async function redeemAuthCode(options, { code, codeVerifier, redirectUri, nonce }) {
|
|
2203
|
+
const result = await postWsServer(
|
|
2204
|
+
"/auth/token",
|
|
2205
|
+
{ apiKey: options.apiKey, code, redirectUri, codeVerifier },
|
|
2206
|
+
options.wsUrl
|
|
2207
|
+
);
|
|
2208
|
+
if (result.idToken) {
|
|
2209
|
+
assertValidIdToken(result.idToken, {
|
|
2210
|
+
apiKey: options.apiKey,
|
|
2211
|
+
wsUrl: options.wsUrl,
|
|
2212
|
+
nonce,
|
|
2213
|
+
address: result.address
|
|
2214
|
+
});
|
|
2215
|
+
}
|
|
2216
|
+
return result;
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// src/auth/loginWithPopup.ts
|
|
2220
|
+
var AUTH_CODE_MESSAGE = "blockauth:auth-code";
|
|
2221
|
+
var POPUP_CLOSED_POLL_MS = 500;
|
|
2222
|
+
var LOGIN_TIMEOUT_MS = 15 * 60 * 1e3;
|
|
2142
2223
|
function waitForCode(popup, authOrigin, state) {
|
|
2143
2224
|
return new Promise((resolve, reject) => {
|
|
2144
2225
|
let done = false;
|
|
@@ -2185,19 +2266,18 @@ function popupFeatures() {
|
|
|
2185
2266
|
async function runAuthorize(options) {
|
|
2186
2267
|
const popup = window.open("about:blank", "blockauth-login", popupFeatures());
|
|
2187
2268
|
if (!popup) throw new Error("Login popup blocked");
|
|
2188
|
-
const redirectUri =
|
|
2269
|
+
const redirectUri = pkceRedirectUri();
|
|
2189
2270
|
try {
|
|
2190
|
-
const
|
|
2191
|
-
const
|
|
2192
|
-
const codeChallenge = await sha256Base64url(codeVerifier);
|
|
2193
|
-
const { url } = await postWsServer(
|
|
2194
|
-
"/auth/authorize",
|
|
2195
|
-
{ apiKey: options.apiKey, redirectUri, state, codeChallenge },
|
|
2196
|
-
options.wsUrl
|
|
2197
|
-
);
|
|
2271
|
+
const challenge = await createPkceChallenge();
|
|
2272
|
+
const url = await requestAuthorizeUrl(options, challenge);
|
|
2198
2273
|
popup.location.href = url;
|
|
2199
|
-
const code = await waitForCode(popup, new URL(url).origin, state);
|
|
2200
|
-
return {
|
|
2274
|
+
const code = await waitForCode(popup, new URL(url).origin, challenge.state);
|
|
2275
|
+
return {
|
|
2276
|
+
code,
|
|
2277
|
+
codeVerifier: challenge.codeVerifier,
|
|
2278
|
+
redirectUri,
|
|
2279
|
+
nonce: challenge.nonce
|
|
2280
|
+
};
|
|
2201
2281
|
} catch (error) {
|
|
2202
2282
|
popup.close();
|
|
2203
2283
|
throw error;
|
|
@@ -2212,18 +2292,290 @@ function authorizeWithPopup(options) {
|
|
|
2212
2292
|
return inFlight;
|
|
2213
2293
|
}
|
|
2214
2294
|
function loginWithPopup(options) {
|
|
2215
|
-
loginInFlight ?? (loginInFlight = authorizeWithPopup(options).then(
|
|
2216
|
-
({ code, codeVerifier, redirectUri }) => postWsServer(
|
|
2217
|
-
"/auth/token",
|
|
2218
|
-
{ apiKey: options.apiKey, code, redirectUri, codeVerifier },
|
|
2219
|
-
options.wsUrl
|
|
2220
|
-
)
|
|
2221
|
-
).finally(() => {
|
|
2295
|
+
loginInFlight ?? (loginInFlight = authorizeWithPopup(options).then((authCode) => redeemAuthCode(options, authCode)).finally(() => {
|
|
2222
2296
|
loginInFlight = null;
|
|
2223
2297
|
}));
|
|
2224
2298
|
return loginInFlight;
|
|
2225
2299
|
}
|
|
2226
2300
|
|
|
2301
|
+
// src/auth/pkceStorage.ts
|
|
2302
|
+
var sessionKey = (apiKey) => `blockauth:pkce:session:${apiKey}`;
|
|
2303
|
+
var pendingKey = (apiKey) => `blockauth:pkce:pending:${apiKey}`;
|
|
2304
|
+
function readJson(key) {
|
|
2305
|
+
try {
|
|
2306
|
+
const raw = sessionStorage.getItem(key);
|
|
2307
|
+
return raw ? JSON.parse(raw) : null;
|
|
2308
|
+
} catch {
|
|
2309
|
+
return null;
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
function writeJson(key, value) {
|
|
2313
|
+
try {
|
|
2314
|
+
sessionStorage.setItem(key, JSON.stringify(value));
|
|
2315
|
+
} catch {
|
|
2316
|
+
return;
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
function remove(key) {
|
|
2320
|
+
try {
|
|
2321
|
+
sessionStorage.removeItem(key);
|
|
2322
|
+
} catch {
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
function saveSession(apiKey, session) {
|
|
2327
|
+
writeJson(sessionKey(apiKey), session);
|
|
2328
|
+
}
|
|
2329
|
+
function clearSession(apiKey) {
|
|
2330
|
+
remove(sessionKey(apiKey));
|
|
2331
|
+
}
|
|
2332
|
+
function loadSession(apiKey) {
|
|
2333
|
+
const stored = readJson(
|
|
2334
|
+
sessionKey(apiKey)
|
|
2335
|
+
);
|
|
2336
|
+
const renewClaims = typeof stored?.renewToken === "string" ? decodeIdTokenClaims(stored.renewToken) : null;
|
|
2337
|
+
if (typeof stored?.idToken !== "string" || typeof stored.renewToken !== "string" || !renewClaims || typeof stored.user?.address !== "string" || renewClaims.aud !== apiKey || renewClaims.sub !== stored.user.address || isIdTokenExpired(renewClaims)) {
|
|
2338
|
+
clearSession(apiKey);
|
|
2339
|
+
return null;
|
|
2340
|
+
}
|
|
2341
|
+
return {
|
|
2342
|
+
idToken: stored.idToken,
|
|
2343
|
+
renewToken: stored.renewToken,
|
|
2344
|
+
user: stored.user
|
|
2345
|
+
};
|
|
2346
|
+
}
|
|
2347
|
+
function savePendingLogin(apiKey, pending) {
|
|
2348
|
+
writeJson(pendingKey(apiKey), pending);
|
|
2349
|
+
}
|
|
2350
|
+
function takePendingLogin(apiKey) {
|
|
2351
|
+
const stored = readJson(
|
|
2352
|
+
pendingKey(apiKey)
|
|
2353
|
+
);
|
|
2354
|
+
remove(pendingKey(apiKey));
|
|
2355
|
+
if (typeof stored?.codeVerifier !== "string" || typeof stored.state !== "string" || typeof stored.nonce !== "string" || typeof stored.returnTo !== "string") {
|
|
2356
|
+
return null;
|
|
2357
|
+
}
|
|
2358
|
+
return {
|
|
2359
|
+
codeVerifier: stored.codeVerifier,
|
|
2360
|
+
state: stored.state,
|
|
2361
|
+
nonce: stored.nonce,
|
|
2362
|
+
returnTo: stored.returnTo
|
|
2363
|
+
};
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
// src/auth/redirectLogin.ts
|
|
2367
|
+
function isSafeReturnTo(returnTo) {
|
|
2368
|
+
if (typeof returnTo !== "string" || !returnTo.startsWith("/") || returnTo.startsWith("//") || returnTo.startsWith("/\\")) {
|
|
2369
|
+
return false;
|
|
2370
|
+
}
|
|
2371
|
+
try {
|
|
2372
|
+
return new URL(returnTo, window.location.origin).origin === window.location.origin;
|
|
2373
|
+
} catch {
|
|
2374
|
+
return false;
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
function currentPath() {
|
|
2378
|
+
const { pathname, search } = window.location;
|
|
2379
|
+
return `${pathname}${search}`;
|
|
2380
|
+
}
|
|
2381
|
+
async function startRedirectLogin(options, { returnTo = currentPath() } = {}) {
|
|
2382
|
+
if (!isSafeReturnTo(returnTo)) throw new Error("Invalid returnTo");
|
|
2383
|
+
const challenge = await createPkceChallenge();
|
|
2384
|
+
const url = await requestAuthorizeUrl(options, challenge, "redirect");
|
|
2385
|
+
savePendingLogin(options.apiKey, {
|
|
2386
|
+
codeVerifier: challenge.codeVerifier,
|
|
2387
|
+
state: challenge.state,
|
|
2388
|
+
nonce: challenge.nonce,
|
|
2389
|
+
returnTo
|
|
2390
|
+
});
|
|
2391
|
+
window.location.assign(url);
|
|
2392
|
+
}
|
|
2393
|
+
async function completeRedirectLogin(options) {
|
|
2394
|
+
const url = new URL(window.location.href);
|
|
2395
|
+
const code = url.searchParams.get("code");
|
|
2396
|
+
const error = url.searchParams.get("error");
|
|
2397
|
+
const state = url.searchParams.get("state");
|
|
2398
|
+
if (!state || !code && !error) return null;
|
|
2399
|
+
const pending = takePendingLogin(options.apiKey);
|
|
2400
|
+
if (!pending) return null;
|
|
2401
|
+
["code", "state", "error"].forEach((name) => url.searchParams.delete(name));
|
|
2402
|
+
const cleaned = `${url.pathname}${url.search}${url.hash}`;
|
|
2403
|
+
const returnTo = isSafeReturnTo(pending.returnTo) ? pending.returnTo : cleaned;
|
|
2404
|
+
window.history.replaceState(window.history.state, "", returnTo);
|
|
2405
|
+
if (pending.state !== state) {
|
|
2406
|
+
throw new Error("Login state mismatch");
|
|
2407
|
+
}
|
|
2408
|
+
if (error) {
|
|
2409
|
+
throw new Error(
|
|
2410
|
+
error === "access_denied" ? "Login denied" : "Login failed"
|
|
2411
|
+
);
|
|
2412
|
+
}
|
|
2413
|
+
return redeemAuthCode(options, {
|
|
2414
|
+
code,
|
|
2415
|
+
codeVerifier: pending.codeVerifier,
|
|
2416
|
+
redirectUri: pkceRedirectUri(),
|
|
2417
|
+
nonce: pending.nonce
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
// src/session/sessionWatch.ts
|
|
2422
|
+
var MAX_TIMER_MS = 2147483647;
|
|
2423
|
+
function createSessionWatch({
|
|
2424
|
+
check,
|
|
2425
|
+
onEnded,
|
|
2426
|
+
intervalMs,
|
|
2427
|
+
minGapMs = 5e3
|
|
2428
|
+
}) {
|
|
2429
|
+
let active = false;
|
|
2430
|
+
let timer = null;
|
|
2431
|
+
let running = false;
|
|
2432
|
+
let lastRun = 0;
|
|
2433
|
+
const run = async () => {
|
|
2434
|
+
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2435
|
+
running = true;
|
|
2436
|
+
lastRun = Date.now();
|
|
2437
|
+
try {
|
|
2438
|
+
const alive = await check();
|
|
2439
|
+
if (active && alive === false) {
|
|
2440
|
+
stop();
|
|
2441
|
+
onEnded();
|
|
2442
|
+
}
|
|
2443
|
+
} finally {
|
|
2444
|
+
running = false;
|
|
2445
|
+
}
|
|
2446
|
+
};
|
|
2447
|
+
const onVisible = () => {
|
|
2448
|
+
if (document.visibilityState === "visible") void run();
|
|
2449
|
+
};
|
|
2450
|
+
const onFocus = () => void run();
|
|
2451
|
+
function start() {
|
|
2452
|
+
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2453
|
+
return;
|
|
2454
|
+
active = true;
|
|
2455
|
+
lastRun = Date.now();
|
|
2456
|
+
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2457
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
2458
|
+
window.addEventListener("focus", onFocus);
|
|
2459
|
+
}
|
|
2460
|
+
function stop() {
|
|
2461
|
+
if (!active) return;
|
|
2462
|
+
active = false;
|
|
2463
|
+
if (timer !== null) clearInterval(timer);
|
|
2464
|
+
timer = null;
|
|
2465
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
2466
|
+
window.removeEventListener("focus", onFocus);
|
|
2467
|
+
}
|
|
2468
|
+
return { start, stop };
|
|
2469
|
+
}
|
|
2470
|
+
|
|
2471
|
+
// src/auth/pkceSession.ts
|
|
2472
|
+
var PKCE_SESSION_CHECK_MS = 5 * 60 * 1e3;
|
|
2473
|
+
var ID_TOKEN_RENEW_MARGIN_MS = 60 * 1e3;
|
|
2474
|
+
function expiresSoon(idToken) {
|
|
2475
|
+
const claims = decodeIdTokenClaims(idToken);
|
|
2476
|
+
return !claims || claims.exp * 1e3 - Date.now() <= ID_TOKEN_RENEW_MARGIN_MS;
|
|
2477
|
+
}
|
|
2478
|
+
function createPkceSession({
|
|
2479
|
+
apiKey,
|
|
2480
|
+
wsUrl,
|
|
2481
|
+
onEnded
|
|
2482
|
+
}) {
|
|
2483
|
+
let renewing = null;
|
|
2484
|
+
const requestRenewal = async () => {
|
|
2485
|
+
const session = loadSession(apiKey);
|
|
2486
|
+
if (!session) return false;
|
|
2487
|
+
let response;
|
|
2488
|
+
try {
|
|
2489
|
+
response = await postWsServer(
|
|
2490
|
+
"/auth/session",
|
|
2491
|
+
{ apiKey, renewToken: session.renewToken },
|
|
2492
|
+
wsUrl
|
|
2493
|
+
);
|
|
2494
|
+
} catch {
|
|
2495
|
+
return null;
|
|
2496
|
+
}
|
|
2497
|
+
if (!response.active || !response.idToken) return false;
|
|
2498
|
+
try {
|
|
2499
|
+
assertValidIdToken(response.idToken, {
|
|
2500
|
+
apiKey,
|
|
2501
|
+
wsUrl,
|
|
2502
|
+
address: session.user.address
|
|
2503
|
+
});
|
|
2504
|
+
} catch {
|
|
2505
|
+
return null;
|
|
2506
|
+
}
|
|
2507
|
+
saveSession(apiKey, { ...session, idToken: response.idToken });
|
|
2508
|
+
return true;
|
|
2509
|
+
};
|
|
2510
|
+
const renew = () => {
|
|
2511
|
+
renewing ?? (renewing = requestRenewal().finally(() => {
|
|
2512
|
+
renewing = null;
|
|
2513
|
+
}));
|
|
2514
|
+
return renewing;
|
|
2515
|
+
};
|
|
2516
|
+
const ended = () => {
|
|
2517
|
+
clearSession(apiKey);
|
|
2518
|
+
onEnded();
|
|
2519
|
+
};
|
|
2520
|
+
const watch = createSessionWatch({
|
|
2521
|
+
intervalMs: PKCE_SESSION_CHECK_MS,
|
|
2522
|
+
check: renew,
|
|
2523
|
+
onEnded: ended
|
|
2524
|
+
});
|
|
2525
|
+
return {
|
|
2526
|
+
restore: () => {
|
|
2527
|
+
const session = loadSession(apiKey);
|
|
2528
|
+
if (session) watch.start();
|
|
2529
|
+
return session;
|
|
2530
|
+
},
|
|
2531
|
+
remember: (result) => {
|
|
2532
|
+
const { renewToken, ...publicResult } = result;
|
|
2533
|
+
if (result.idToken && renewToken) {
|
|
2534
|
+
const { address, did, roles } = result;
|
|
2535
|
+
saveSession(apiKey, {
|
|
2536
|
+
idToken: result.idToken,
|
|
2537
|
+
renewToken,
|
|
2538
|
+
user: { address, did, roles }
|
|
2539
|
+
});
|
|
2540
|
+
watch.start();
|
|
2541
|
+
}
|
|
2542
|
+
return publicResult;
|
|
2543
|
+
},
|
|
2544
|
+
hasSession: () => loadSession(apiKey) !== null,
|
|
2545
|
+
getIdToken: async () => {
|
|
2546
|
+
const session = loadSession(apiKey);
|
|
2547
|
+
if (!session) return null;
|
|
2548
|
+
if (!expiresSoon(session.idToken)) return session.idToken;
|
|
2549
|
+
const renewed = await renew();
|
|
2550
|
+
if (renewed === false) {
|
|
2551
|
+
watch.stop();
|
|
2552
|
+
ended();
|
|
2553
|
+
return null;
|
|
2554
|
+
}
|
|
2555
|
+
const current = loadSession(apiKey)?.idToken ?? null;
|
|
2556
|
+
const claims = current ? decodeIdTokenClaims(current) : null;
|
|
2557
|
+
return claims && !isIdTokenExpired(claims) ? current : null;
|
|
2558
|
+
},
|
|
2559
|
+
signOut: async () => {
|
|
2560
|
+
watch.stop();
|
|
2561
|
+
const session = loadSession(apiKey);
|
|
2562
|
+
try {
|
|
2563
|
+
if (session) {
|
|
2564
|
+
await postWsServer(
|
|
2565
|
+
"/auth/signout",
|
|
2566
|
+
{ apiKey, idToken: session.idToken },
|
|
2567
|
+
wsUrl
|
|
2568
|
+
);
|
|
2569
|
+
}
|
|
2570
|
+
} catch {
|
|
2571
|
+
return;
|
|
2572
|
+
} finally {
|
|
2573
|
+
clearSession(apiKey);
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
};
|
|
2577
|
+
}
|
|
2578
|
+
|
|
2227
2579
|
// src/auth/authEndpoint.ts
|
|
2228
2580
|
var DEFAULT_AUTH_ENDPOINT = "/auth/blockauth";
|
|
2229
2581
|
var BACKEND_ROUTES_GUIDE = "https://docs.block-auth.io/sdk-integration-spa-backend/#backend-routes";
|
|
@@ -2275,7 +2627,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2275
2627
|
throw error;
|
|
2276
2628
|
}
|
|
2277
2629
|
};
|
|
2278
|
-
const
|
|
2630
|
+
const readJson2 = async (path, response) => {
|
|
2279
2631
|
try {
|
|
2280
2632
|
return await response.json();
|
|
2281
2633
|
} catch (error) {
|
|
@@ -2292,7 +2644,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2292
2644
|
body: JSON.stringify(body)
|
|
2293
2645
|
});
|
|
2294
2646
|
const readUser = async (path, response) => {
|
|
2295
|
-
const payload = await
|
|
2647
|
+
const payload = await readJson2(path, response);
|
|
2296
2648
|
try {
|
|
2297
2649
|
return toUser(payload);
|
|
2298
2650
|
} catch (error) {
|
|
@@ -2341,7 +2693,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2341
2693
|
const response = await request("/link", { method: "GET" });
|
|
2342
2694
|
if (response.status === 401) return null;
|
|
2343
2695
|
if (!response.ok) fail("/link", response);
|
|
2344
|
-
const { linked } = await
|
|
2696
|
+
const { linked } = await readJson2("/link", response);
|
|
2345
2697
|
return linked === true;
|
|
2346
2698
|
},
|
|
2347
2699
|
link: async ({ code, codeVerifier, redirectUri }) => {
|
|
@@ -2357,56 +2709,6 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2357
2709
|
};
|
|
2358
2710
|
}
|
|
2359
2711
|
|
|
2360
|
-
// src/session/sessionWatch.ts
|
|
2361
|
-
var MAX_TIMER_MS = 2147483647;
|
|
2362
|
-
function createSessionWatch({
|
|
2363
|
-
check,
|
|
2364
|
-
onEnded,
|
|
2365
|
-
intervalMs,
|
|
2366
|
-
minGapMs = 5e3
|
|
2367
|
-
}) {
|
|
2368
|
-
let active = false;
|
|
2369
|
-
let timer = null;
|
|
2370
|
-
let running = false;
|
|
2371
|
-
let lastRun = 0;
|
|
2372
|
-
const run = async () => {
|
|
2373
|
-
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2374
|
-
running = true;
|
|
2375
|
-
lastRun = Date.now();
|
|
2376
|
-
try {
|
|
2377
|
-
const alive = await check();
|
|
2378
|
-
if (active && alive === false) {
|
|
2379
|
-
stop();
|
|
2380
|
-
onEnded();
|
|
2381
|
-
}
|
|
2382
|
-
} finally {
|
|
2383
|
-
running = false;
|
|
2384
|
-
}
|
|
2385
|
-
};
|
|
2386
|
-
const onVisible = () => {
|
|
2387
|
-
if (document.visibilityState === "visible") void run();
|
|
2388
|
-
};
|
|
2389
|
-
const onFocus = () => void run();
|
|
2390
|
-
function start() {
|
|
2391
|
-
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2392
|
-
return;
|
|
2393
|
-
active = true;
|
|
2394
|
-
lastRun = Date.now();
|
|
2395
|
-
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2396
|
-
document.addEventListener("visibilitychange", onVisible);
|
|
2397
|
-
window.addEventListener("focus", onFocus);
|
|
2398
|
-
}
|
|
2399
|
-
function stop() {
|
|
2400
|
-
if (!active) return;
|
|
2401
|
-
active = false;
|
|
2402
|
-
if (timer !== null) clearInterval(timer);
|
|
2403
|
-
timer = null;
|
|
2404
|
-
document.removeEventListener("visibilitychange", onVisible);
|
|
2405
|
-
window.removeEventListener("focus", onFocus);
|
|
2406
|
-
}
|
|
2407
|
-
return { start, stop };
|
|
2408
|
-
}
|
|
2409
|
-
|
|
2410
2712
|
// src/auth/logout.ts
|
|
2411
2713
|
async function performLogout(args) {
|
|
2412
2714
|
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
@@ -2712,6 +3014,14 @@ function createBlockAuthClient(config3) {
|
|
|
2712
3014
|
},
|
|
2713
3015
|
intervalMs: (config3.sessionCheckSeconds ?? 60) * 1e3
|
|
2714
3016
|
}) : null;
|
|
3017
|
+
const pkceSession = createPkceSession({
|
|
3018
|
+
apiKey: config3.apiKey,
|
|
3019
|
+
wsUrl: config3.wsUrl,
|
|
3020
|
+
onEnded: () => {
|
|
3021
|
+
forgetHostedUser();
|
|
3022
|
+
config3.onSessionEnded?.();
|
|
3023
|
+
}
|
|
3024
|
+
});
|
|
2715
3025
|
const provider = new BlockAuthProvider(
|
|
2716
3026
|
config3.apiKey,
|
|
2717
3027
|
config3.apiSecret,
|
|
@@ -2796,8 +3106,22 @@ function createBlockAuthClient(config3) {
|
|
|
2796
3106
|
wsUrl: config3.wsUrl
|
|
2797
3107
|
});
|
|
2798
3108
|
rememberHostedLogin(result);
|
|
2799
|
-
return result;
|
|
3109
|
+
return pkceSession.remember(result);
|
|
2800
3110
|
},
|
|
3111
|
+
loginWithRedirect: (options) => startRedirectLogin(
|
|
3112
|
+
{ apiKey: config3.apiKey, wsUrl: config3.wsUrl },
|
|
3113
|
+
options
|
|
3114
|
+
),
|
|
3115
|
+
handleRedirectCallback: async () => {
|
|
3116
|
+
const result = await completeRedirectLogin({
|
|
3117
|
+
apiKey: config3.apiKey,
|
|
3118
|
+
wsUrl: config3.wsUrl
|
|
3119
|
+
});
|
|
3120
|
+
if (!result) return null;
|
|
3121
|
+
rememberHostedLogin(result);
|
|
3122
|
+
return pkceSession.remember(result);
|
|
3123
|
+
},
|
|
3124
|
+
getIdToken: () => pkceSession.getIdToken(),
|
|
2801
3125
|
// Same popup, but the code goes to the app's backend, which redeems it
|
|
2802
3126
|
// with its apiSecret (see PopupAuthCode). Call it from a click handler.
|
|
2803
3127
|
authorizeWithPopup: () => authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl }),
|
|
@@ -2835,6 +3159,11 @@ function createBlockAuthClient(config3) {
|
|
|
2835
3159
|
);
|
|
2836
3160
|
},
|
|
2837
3161
|
restoreHostedLogin: async () => {
|
|
3162
|
+
const stored = pkceSession.restore();
|
|
3163
|
+
if (stored) {
|
|
3164
|
+
showHostedUser(stored.user);
|
|
3165
|
+
return stored.user;
|
|
3166
|
+
}
|
|
2838
3167
|
if (!authEndpoint) return null;
|
|
2839
3168
|
const user = await authEndpoint.me();
|
|
2840
3169
|
if (user) {
|
|
@@ -2922,6 +3251,9 @@ function createBlockAuthClient(config3) {
|
|
|
2922
3251
|
// restoration-expired modal.
|
|
2923
3252
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2924
3253
|
logout: async () => {
|
|
3254
|
+
const hadPkceSession = pkceSession.hasSession();
|
|
3255
|
+
await pkceSession.signOut();
|
|
3256
|
+
if (hadPkceSession) forgetHostedUser();
|
|
2925
3257
|
if (authEndpoint && !config3.apiUrl && sessionOnAuthEndpoint)
|
|
2926
3258
|
await authEndpoint.logout();
|
|
2927
3259
|
sessionWatch?.stop();
|