@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.cjs
CHANGED
|
@@ -2115,10 +2115,43 @@ function connectBlockAuth(apiUrl) {
|
|
|
2115
2115
|
var LOGIN_POPUP_WIDTH = 380;
|
|
2116
2116
|
var LOGIN_POPUP_HEIGHT = 640;
|
|
2117
2117
|
|
|
2118
|
-
// src/auth/
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2118
|
+
// src/auth/idToken.ts
|
|
2119
|
+
function decodeIdTokenClaims(idToken) {
|
|
2120
|
+
const parts = idToken.split(".");
|
|
2121
|
+
if (parts.length !== 3) return null;
|
|
2122
|
+
try {
|
|
2123
|
+
const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
2124
|
+
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
2125
|
+
const bytes = Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
|
|
2126
|
+
const claims = JSON.parse(new TextDecoder().decode(bytes));
|
|
2127
|
+
if (typeof claims?.iss !== "string" || typeof claims.sub !== "string" || typeof claims.aud !== "string" || typeof claims.exp !== "number") {
|
|
2128
|
+
return null;
|
|
2129
|
+
}
|
|
2130
|
+
return {
|
|
2131
|
+
iss: claims.iss,
|
|
2132
|
+
sub: claims.sub,
|
|
2133
|
+
aud: claims.aud,
|
|
2134
|
+
exp: claims.exp,
|
|
2135
|
+
nonce: typeof claims.nonce === "string" ? claims.nonce : void 0
|
|
2136
|
+
};
|
|
2137
|
+
} catch {
|
|
2138
|
+
return null;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
function isIdTokenExpired(claims) {
|
|
2142
|
+
return claims.exp * 1e3 <= Date.now();
|
|
2143
|
+
}
|
|
2144
|
+
function assertValidIdToken(idToken, expected) {
|
|
2145
|
+
const claims = decodeIdTokenClaims(idToken);
|
|
2146
|
+
if (!claims) throw new Error("Invalid idToken");
|
|
2147
|
+
const issuer = new URL(buildWsServerApiUrl("/", expected.wsUrl)).origin;
|
|
2148
|
+
if (claims.aud !== expected.apiKey || expected.nonce !== void 0 && claims.nonce !== expected.nonce || claims.iss !== issuer || claims.sub !== expected.address || isIdTokenExpired(claims)) {
|
|
2149
|
+
throw new Error("Invalid idToken");
|
|
2150
|
+
}
|
|
2151
|
+
return claims;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
// src/auth/pkce.ts
|
|
2122
2155
|
function base64url(bytes) {
|
|
2123
2156
|
let binary = "";
|
|
2124
2157
|
for (const b of bytes) binary += String.fromCharCode(b);
|
|
@@ -2147,6 +2180,54 @@ async function postWsServer(path, body, wsUrl) {
|
|
|
2147
2180
|
}
|
|
2148
2181
|
return payload.message;
|
|
2149
2182
|
}
|
|
2183
|
+
function pkceRedirectUri() {
|
|
2184
|
+
return `${window.location.origin}/`;
|
|
2185
|
+
}
|
|
2186
|
+
async function createPkceChallenge() {
|
|
2187
|
+
const codeVerifier = randomString(32);
|
|
2188
|
+
return {
|
|
2189
|
+
codeVerifier,
|
|
2190
|
+
state: randomString(16),
|
|
2191
|
+
nonce: randomString(32),
|
|
2192
|
+
codeChallenge: await sha256Base64url(codeVerifier)
|
|
2193
|
+
};
|
|
2194
|
+
}
|
|
2195
|
+
async function requestAuthorizeUrl(options, challenge, responseMode) {
|
|
2196
|
+
const { url } = await postWsServer(
|
|
2197
|
+
"/auth/authorize",
|
|
2198
|
+
{
|
|
2199
|
+
apiKey: options.apiKey,
|
|
2200
|
+
redirectUri: pkceRedirectUri(),
|
|
2201
|
+
state: challenge.state,
|
|
2202
|
+
codeChallenge: challenge.codeChallenge,
|
|
2203
|
+
nonce: challenge.nonce,
|
|
2204
|
+
...responseMode ? { responseMode } : {}
|
|
2205
|
+
},
|
|
2206
|
+
options.wsUrl
|
|
2207
|
+
);
|
|
2208
|
+
return url;
|
|
2209
|
+
}
|
|
2210
|
+
async function redeemAuthCode(options, { code, codeVerifier, redirectUri, nonce }) {
|
|
2211
|
+
const result = await postWsServer(
|
|
2212
|
+
"/auth/token",
|
|
2213
|
+
{ apiKey: options.apiKey, code, redirectUri, codeVerifier },
|
|
2214
|
+
options.wsUrl
|
|
2215
|
+
);
|
|
2216
|
+
if (result.idToken) {
|
|
2217
|
+
assertValidIdToken(result.idToken, {
|
|
2218
|
+
apiKey: options.apiKey,
|
|
2219
|
+
wsUrl: options.wsUrl,
|
|
2220
|
+
nonce,
|
|
2221
|
+
address: result.address
|
|
2222
|
+
});
|
|
2223
|
+
}
|
|
2224
|
+
return result;
|
|
2225
|
+
}
|
|
2226
|
+
|
|
2227
|
+
// src/auth/loginWithPopup.ts
|
|
2228
|
+
var AUTH_CODE_MESSAGE = "blockauth:auth-code";
|
|
2229
|
+
var POPUP_CLOSED_POLL_MS = 500;
|
|
2230
|
+
var LOGIN_TIMEOUT_MS = 15 * 60 * 1e3;
|
|
2150
2231
|
function waitForCode(popup, authOrigin, state) {
|
|
2151
2232
|
return new Promise((resolve, reject) => {
|
|
2152
2233
|
let done = false;
|
|
@@ -2193,19 +2274,18 @@ function popupFeatures() {
|
|
|
2193
2274
|
async function runAuthorize(options) {
|
|
2194
2275
|
const popup = window.open("about:blank", "blockauth-login", popupFeatures());
|
|
2195
2276
|
if (!popup) throw new Error("Login popup blocked");
|
|
2196
|
-
const redirectUri =
|
|
2277
|
+
const redirectUri = pkceRedirectUri();
|
|
2197
2278
|
try {
|
|
2198
|
-
const
|
|
2199
|
-
const
|
|
2200
|
-
const codeChallenge = await sha256Base64url(codeVerifier);
|
|
2201
|
-
const { url } = await postWsServer(
|
|
2202
|
-
"/auth/authorize",
|
|
2203
|
-
{ apiKey: options.apiKey, redirectUri, state, codeChallenge },
|
|
2204
|
-
options.wsUrl
|
|
2205
|
-
);
|
|
2279
|
+
const challenge = await createPkceChallenge();
|
|
2280
|
+
const url = await requestAuthorizeUrl(options, challenge);
|
|
2206
2281
|
popup.location.href = url;
|
|
2207
|
-
const code = await waitForCode(popup, new URL(url).origin, state);
|
|
2208
|
-
return {
|
|
2282
|
+
const code = await waitForCode(popup, new URL(url).origin, challenge.state);
|
|
2283
|
+
return {
|
|
2284
|
+
code,
|
|
2285
|
+
codeVerifier: challenge.codeVerifier,
|
|
2286
|
+
redirectUri,
|
|
2287
|
+
nonce: challenge.nonce
|
|
2288
|
+
};
|
|
2209
2289
|
} catch (error) {
|
|
2210
2290
|
popup.close();
|
|
2211
2291
|
throw error;
|
|
@@ -2220,18 +2300,290 @@ function authorizeWithPopup(options) {
|
|
|
2220
2300
|
return inFlight;
|
|
2221
2301
|
}
|
|
2222
2302
|
function loginWithPopup(options) {
|
|
2223
|
-
loginInFlight ?? (loginInFlight = authorizeWithPopup(options).then(
|
|
2224
|
-
({ code, codeVerifier, redirectUri }) => postWsServer(
|
|
2225
|
-
"/auth/token",
|
|
2226
|
-
{ apiKey: options.apiKey, code, redirectUri, codeVerifier },
|
|
2227
|
-
options.wsUrl
|
|
2228
|
-
)
|
|
2229
|
-
).finally(() => {
|
|
2303
|
+
loginInFlight ?? (loginInFlight = authorizeWithPopup(options).then((authCode) => redeemAuthCode(options, authCode)).finally(() => {
|
|
2230
2304
|
loginInFlight = null;
|
|
2231
2305
|
}));
|
|
2232
2306
|
return loginInFlight;
|
|
2233
2307
|
}
|
|
2234
2308
|
|
|
2309
|
+
// src/auth/pkceStorage.ts
|
|
2310
|
+
var sessionKey = (apiKey) => `blockauth:pkce:session:${apiKey}`;
|
|
2311
|
+
var pendingKey = (apiKey) => `blockauth:pkce:pending:${apiKey}`;
|
|
2312
|
+
function readJson(key) {
|
|
2313
|
+
try {
|
|
2314
|
+
const raw = sessionStorage.getItem(key);
|
|
2315
|
+
return raw ? JSON.parse(raw) : null;
|
|
2316
|
+
} catch {
|
|
2317
|
+
return null;
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
function writeJson(key, value) {
|
|
2321
|
+
try {
|
|
2322
|
+
sessionStorage.setItem(key, JSON.stringify(value));
|
|
2323
|
+
} catch {
|
|
2324
|
+
return;
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
function remove(key) {
|
|
2328
|
+
try {
|
|
2329
|
+
sessionStorage.removeItem(key);
|
|
2330
|
+
} catch {
|
|
2331
|
+
return;
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
function saveSession(apiKey, session) {
|
|
2335
|
+
writeJson(sessionKey(apiKey), session);
|
|
2336
|
+
}
|
|
2337
|
+
function clearSession(apiKey) {
|
|
2338
|
+
remove(sessionKey(apiKey));
|
|
2339
|
+
}
|
|
2340
|
+
function loadSession(apiKey) {
|
|
2341
|
+
const stored = readJson(
|
|
2342
|
+
sessionKey(apiKey)
|
|
2343
|
+
);
|
|
2344
|
+
const renewClaims = typeof stored?.renewToken === "string" ? decodeIdTokenClaims(stored.renewToken) : null;
|
|
2345
|
+
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)) {
|
|
2346
|
+
clearSession(apiKey);
|
|
2347
|
+
return null;
|
|
2348
|
+
}
|
|
2349
|
+
return {
|
|
2350
|
+
idToken: stored.idToken,
|
|
2351
|
+
renewToken: stored.renewToken,
|
|
2352
|
+
user: stored.user
|
|
2353
|
+
};
|
|
2354
|
+
}
|
|
2355
|
+
function savePendingLogin(apiKey, pending) {
|
|
2356
|
+
writeJson(pendingKey(apiKey), pending);
|
|
2357
|
+
}
|
|
2358
|
+
function takePendingLogin(apiKey) {
|
|
2359
|
+
const stored = readJson(
|
|
2360
|
+
pendingKey(apiKey)
|
|
2361
|
+
);
|
|
2362
|
+
remove(pendingKey(apiKey));
|
|
2363
|
+
if (typeof stored?.codeVerifier !== "string" || typeof stored.state !== "string" || typeof stored.nonce !== "string" || typeof stored.returnTo !== "string") {
|
|
2364
|
+
return null;
|
|
2365
|
+
}
|
|
2366
|
+
return {
|
|
2367
|
+
codeVerifier: stored.codeVerifier,
|
|
2368
|
+
state: stored.state,
|
|
2369
|
+
nonce: stored.nonce,
|
|
2370
|
+
returnTo: stored.returnTo
|
|
2371
|
+
};
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
// src/auth/redirectLogin.ts
|
|
2375
|
+
function isSafeReturnTo(returnTo) {
|
|
2376
|
+
if (typeof returnTo !== "string" || !returnTo.startsWith("/") || returnTo.startsWith("//") || returnTo.startsWith("/\\")) {
|
|
2377
|
+
return false;
|
|
2378
|
+
}
|
|
2379
|
+
try {
|
|
2380
|
+
return new URL(returnTo, window.location.origin).origin === window.location.origin;
|
|
2381
|
+
} catch {
|
|
2382
|
+
return false;
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
function currentPath() {
|
|
2386
|
+
const { pathname, search } = window.location;
|
|
2387
|
+
return `${pathname}${search}`;
|
|
2388
|
+
}
|
|
2389
|
+
async function startRedirectLogin(options, { returnTo = currentPath() } = {}) {
|
|
2390
|
+
if (!isSafeReturnTo(returnTo)) throw new Error("Invalid returnTo");
|
|
2391
|
+
const challenge = await createPkceChallenge();
|
|
2392
|
+
const url = await requestAuthorizeUrl(options, challenge, "redirect");
|
|
2393
|
+
savePendingLogin(options.apiKey, {
|
|
2394
|
+
codeVerifier: challenge.codeVerifier,
|
|
2395
|
+
state: challenge.state,
|
|
2396
|
+
nonce: challenge.nonce,
|
|
2397
|
+
returnTo
|
|
2398
|
+
});
|
|
2399
|
+
window.location.assign(url);
|
|
2400
|
+
}
|
|
2401
|
+
async function completeRedirectLogin(options) {
|
|
2402
|
+
const url = new URL(window.location.href);
|
|
2403
|
+
const code = url.searchParams.get("code");
|
|
2404
|
+
const error = url.searchParams.get("error");
|
|
2405
|
+
const state = url.searchParams.get("state");
|
|
2406
|
+
if (!state || !code && !error) return null;
|
|
2407
|
+
const pending = takePendingLogin(options.apiKey);
|
|
2408
|
+
if (!pending) return null;
|
|
2409
|
+
["code", "state", "error"].forEach((name) => url.searchParams.delete(name));
|
|
2410
|
+
const cleaned = `${url.pathname}${url.search}${url.hash}`;
|
|
2411
|
+
const returnTo = isSafeReturnTo(pending.returnTo) ? pending.returnTo : cleaned;
|
|
2412
|
+
window.history.replaceState(window.history.state, "", returnTo);
|
|
2413
|
+
if (pending.state !== state) {
|
|
2414
|
+
throw new Error("Login state mismatch");
|
|
2415
|
+
}
|
|
2416
|
+
if (error) {
|
|
2417
|
+
throw new Error(
|
|
2418
|
+
error === "access_denied" ? "Login denied" : "Login failed"
|
|
2419
|
+
);
|
|
2420
|
+
}
|
|
2421
|
+
return redeemAuthCode(options, {
|
|
2422
|
+
code,
|
|
2423
|
+
codeVerifier: pending.codeVerifier,
|
|
2424
|
+
redirectUri: pkceRedirectUri(),
|
|
2425
|
+
nonce: pending.nonce
|
|
2426
|
+
});
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
// src/session/sessionWatch.ts
|
|
2430
|
+
var MAX_TIMER_MS = 2147483647;
|
|
2431
|
+
function createSessionWatch({
|
|
2432
|
+
check,
|
|
2433
|
+
onEnded,
|
|
2434
|
+
intervalMs,
|
|
2435
|
+
minGapMs = 5e3
|
|
2436
|
+
}) {
|
|
2437
|
+
let active = false;
|
|
2438
|
+
let timer = null;
|
|
2439
|
+
let running = false;
|
|
2440
|
+
let lastRun = 0;
|
|
2441
|
+
const run = async () => {
|
|
2442
|
+
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2443
|
+
running = true;
|
|
2444
|
+
lastRun = Date.now();
|
|
2445
|
+
try {
|
|
2446
|
+
const alive = await check();
|
|
2447
|
+
if (active && alive === false) {
|
|
2448
|
+
stop();
|
|
2449
|
+
onEnded();
|
|
2450
|
+
}
|
|
2451
|
+
} finally {
|
|
2452
|
+
running = false;
|
|
2453
|
+
}
|
|
2454
|
+
};
|
|
2455
|
+
const onVisible = () => {
|
|
2456
|
+
if (document.visibilityState === "visible") void run();
|
|
2457
|
+
};
|
|
2458
|
+
const onFocus = () => void run();
|
|
2459
|
+
function start() {
|
|
2460
|
+
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2461
|
+
return;
|
|
2462
|
+
active = true;
|
|
2463
|
+
lastRun = Date.now();
|
|
2464
|
+
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2465
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
2466
|
+
window.addEventListener("focus", onFocus);
|
|
2467
|
+
}
|
|
2468
|
+
function stop() {
|
|
2469
|
+
if (!active) return;
|
|
2470
|
+
active = false;
|
|
2471
|
+
if (timer !== null) clearInterval(timer);
|
|
2472
|
+
timer = null;
|
|
2473
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
2474
|
+
window.removeEventListener("focus", onFocus);
|
|
2475
|
+
}
|
|
2476
|
+
return { start, stop };
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
// src/auth/pkceSession.ts
|
|
2480
|
+
var PKCE_SESSION_CHECK_MS = 5 * 60 * 1e3;
|
|
2481
|
+
var ID_TOKEN_RENEW_MARGIN_MS = 60 * 1e3;
|
|
2482
|
+
function expiresSoon(idToken) {
|
|
2483
|
+
const claims = decodeIdTokenClaims(idToken);
|
|
2484
|
+
return !claims || claims.exp * 1e3 - Date.now() <= ID_TOKEN_RENEW_MARGIN_MS;
|
|
2485
|
+
}
|
|
2486
|
+
function createPkceSession({
|
|
2487
|
+
apiKey,
|
|
2488
|
+
wsUrl,
|
|
2489
|
+
onEnded
|
|
2490
|
+
}) {
|
|
2491
|
+
let renewing = null;
|
|
2492
|
+
const requestRenewal = async () => {
|
|
2493
|
+
const session = loadSession(apiKey);
|
|
2494
|
+
if (!session) return false;
|
|
2495
|
+
let response;
|
|
2496
|
+
try {
|
|
2497
|
+
response = await postWsServer(
|
|
2498
|
+
"/auth/session",
|
|
2499
|
+
{ apiKey, renewToken: session.renewToken },
|
|
2500
|
+
wsUrl
|
|
2501
|
+
);
|
|
2502
|
+
} catch {
|
|
2503
|
+
return null;
|
|
2504
|
+
}
|
|
2505
|
+
if (!response.active || !response.idToken) return false;
|
|
2506
|
+
try {
|
|
2507
|
+
assertValidIdToken(response.idToken, {
|
|
2508
|
+
apiKey,
|
|
2509
|
+
wsUrl,
|
|
2510
|
+
address: session.user.address
|
|
2511
|
+
});
|
|
2512
|
+
} catch {
|
|
2513
|
+
return null;
|
|
2514
|
+
}
|
|
2515
|
+
saveSession(apiKey, { ...session, idToken: response.idToken });
|
|
2516
|
+
return true;
|
|
2517
|
+
};
|
|
2518
|
+
const renew = () => {
|
|
2519
|
+
renewing ?? (renewing = requestRenewal().finally(() => {
|
|
2520
|
+
renewing = null;
|
|
2521
|
+
}));
|
|
2522
|
+
return renewing;
|
|
2523
|
+
};
|
|
2524
|
+
const ended = () => {
|
|
2525
|
+
clearSession(apiKey);
|
|
2526
|
+
onEnded();
|
|
2527
|
+
};
|
|
2528
|
+
const watch = createSessionWatch({
|
|
2529
|
+
intervalMs: PKCE_SESSION_CHECK_MS,
|
|
2530
|
+
check: renew,
|
|
2531
|
+
onEnded: ended
|
|
2532
|
+
});
|
|
2533
|
+
return {
|
|
2534
|
+
restore: () => {
|
|
2535
|
+
const session = loadSession(apiKey);
|
|
2536
|
+
if (session) watch.start();
|
|
2537
|
+
return session;
|
|
2538
|
+
},
|
|
2539
|
+
remember: (result) => {
|
|
2540
|
+
const { renewToken, ...publicResult } = result;
|
|
2541
|
+
if (result.idToken && renewToken) {
|
|
2542
|
+
const { address, did, roles } = result;
|
|
2543
|
+
saveSession(apiKey, {
|
|
2544
|
+
idToken: result.idToken,
|
|
2545
|
+
renewToken,
|
|
2546
|
+
user: { address, did, roles }
|
|
2547
|
+
});
|
|
2548
|
+
watch.start();
|
|
2549
|
+
}
|
|
2550
|
+
return publicResult;
|
|
2551
|
+
},
|
|
2552
|
+
hasSession: () => loadSession(apiKey) !== null,
|
|
2553
|
+
getIdToken: async () => {
|
|
2554
|
+
const session = loadSession(apiKey);
|
|
2555
|
+
if (!session) return null;
|
|
2556
|
+
if (!expiresSoon(session.idToken)) return session.idToken;
|
|
2557
|
+
const renewed = await renew();
|
|
2558
|
+
if (renewed === false) {
|
|
2559
|
+
watch.stop();
|
|
2560
|
+
ended();
|
|
2561
|
+
return null;
|
|
2562
|
+
}
|
|
2563
|
+
const current = loadSession(apiKey)?.idToken ?? null;
|
|
2564
|
+
const claims = current ? decodeIdTokenClaims(current) : null;
|
|
2565
|
+
return claims && !isIdTokenExpired(claims) ? current : null;
|
|
2566
|
+
},
|
|
2567
|
+
signOut: async () => {
|
|
2568
|
+
watch.stop();
|
|
2569
|
+
const session = loadSession(apiKey);
|
|
2570
|
+
try {
|
|
2571
|
+
if (session) {
|
|
2572
|
+
await postWsServer(
|
|
2573
|
+
"/auth/signout",
|
|
2574
|
+
{ apiKey, idToken: session.idToken },
|
|
2575
|
+
wsUrl
|
|
2576
|
+
);
|
|
2577
|
+
}
|
|
2578
|
+
} catch {
|
|
2579
|
+
return;
|
|
2580
|
+
} finally {
|
|
2581
|
+
clearSession(apiKey);
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
};
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2235
2587
|
// src/auth/authEndpoint.ts
|
|
2236
2588
|
var DEFAULT_AUTH_ENDPOINT = "/auth/blockauth";
|
|
2237
2589
|
var BACKEND_ROUTES_GUIDE = "https://docs.block-auth.io/sdk-integration-spa-backend/#backend-routes";
|
|
@@ -2283,7 +2635,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2283
2635
|
throw error;
|
|
2284
2636
|
}
|
|
2285
2637
|
};
|
|
2286
|
-
const
|
|
2638
|
+
const readJson2 = async (path, response) => {
|
|
2287
2639
|
try {
|
|
2288
2640
|
return await response.json();
|
|
2289
2641
|
} catch (error) {
|
|
@@ -2300,7 +2652,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2300
2652
|
body: JSON.stringify(body)
|
|
2301
2653
|
});
|
|
2302
2654
|
const readUser = async (path, response) => {
|
|
2303
|
-
const payload = await
|
|
2655
|
+
const payload = await readJson2(path, response);
|
|
2304
2656
|
try {
|
|
2305
2657
|
return toUser(payload);
|
|
2306
2658
|
} catch (error) {
|
|
@@ -2349,7 +2701,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2349
2701
|
const response = await request("/link", { method: "GET" });
|
|
2350
2702
|
if (response.status === 401) return null;
|
|
2351
2703
|
if (!response.ok) fail("/link", response);
|
|
2352
|
-
const { linked } = await
|
|
2704
|
+
const { linked } = await readJson2("/link", response);
|
|
2353
2705
|
return linked === true;
|
|
2354
2706
|
},
|
|
2355
2707
|
link: async ({ code, codeVerifier, redirectUri }) => {
|
|
@@ -2365,56 +2717,6 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2365
2717
|
};
|
|
2366
2718
|
}
|
|
2367
2719
|
|
|
2368
|
-
// src/session/sessionWatch.ts
|
|
2369
|
-
var MAX_TIMER_MS = 2147483647;
|
|
2370
|
-
function createSessionWatch({
|
|
2371
|
-
check,
|
|
2372
|
-
onEnded,
|
|
2373
|
-
intervalMs,
|
|
2374
|
-
minGapMs = 5e3
|
|
2375
|
-
}) {
|
|
2376
|
-
let active = false;
|
|
2377
|
-
let timer = null;
|
|
2378
|
-
let running = false;
|
|
2379
|
-
let lastRun = 0;
|
|
2380
|
-
const run = async () => {
|
|
2381
|
-
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2382
|
-
running = true;
|
|
2383
|
-
lastRun = Date.now();
|
|
2384
|
-
try {
|
|
2385
|
-
const alive = await check();
|
|
2386
|
-
if (active && alive === false) {
|
|
2387
|
-
stop();
|
|
2388
|
-
onEnded();
|
|
2389
|
-
}
|
|
2390
|
-
} finally {
|
|
2391
|
-
running = false;
|
|
2392
|
-
}
|
|
2393
|
-
};
|
|
2394
|
-
const onVisible = () => {
|
|
2395
|
-
if (document.visibilityState === "visible") void run();
|
|
2396
|
-
};
|
|
2397
|
-
const onFocus = () => void run();
|
|
2398
|
-
function start() {
|
|
2399
|
-
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2400
|
-
return;
|
|
2401
|
-
active = true;
|
|
2402
|
-
lastRun = Date.now();
|
|
2403
|
-
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2404
|
-
document.addEventListener("visibilitychange", onVisible);
|
|
2405
|
-
window.addEventListener("focus", onFocus);
|
|
2406
|
-
}
|
|
2407
|
-
function stop() {
|
|
2408
|
-
if (!active) return;
|
|
2409
|
-
active = false;
|
|
2410
|
-
if (timer !== null) clearInterval(timer);
|
|
2411
|
-
timer = null;
|
|
2412
|
-
document.removeEventListener("visibilitychange", onVisible);
|
|
2413
|
-
window.removeEventListener("focus", onFocus);
|
|
2414
|
-
}
|
|
2415
|
-
return { start, stop };
|
|
2416
|
-
}
|
|
2417
|
-
|
|
2418
2720
|
// src/auth/logout.ts
|
|
2419
2721
|
async function performLogout(args) {
|
|
2420
2722
|
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
@@ -2720,6 +3022,14 @@ function createBlockAuthClient(config3) {
|
|
|
2720
3022
|
},
|
|
2721
3023
|
intervalMs: (config3.sessionCheckSeconds ?? 60) * 1e3
|
|
2722
3024
|
}) : null;
|
|
3025
|
+
const pkceSession = createPkceSession({
|
|
3026
|
+
apiKey: config3.apiKey,
|
|
3027
|
+
wsUrl: config3.wsUrl,
|
|
3028
|
+
onEnded: () => {
|
|
3029
|
+
forgetHostedUser();
|
|
3030
|
+
config3.onSessionEnded?.();
|
|
3031
|
+
}
|
|
3032
|
+
});
|
|
2723
3033
|
const provider = new BlockAuthProvider(
|
|
2724
3034
|
config3.apiKey,
|
|
2725
3035
|
config3.apiSecret,
|
|
@@ -2804,8 +3114,22 @@ function createBlockAuthClient(config3) {
|
|
|
2804
3114
|
wsUrl: config3.wsUrl
|
|
2805
3115
|
});
|
|
2806
3116
|
rememberHostedLogin(result);
|
|
2807
|
-
return result;
|
|
3117
|
+
return pkceSession.remember(result);
|
|
2808
3118
|
},
|
|
3119
|
+
loginWithRedirect: (options) => startRedirectLogin(
|
|
3120
|
+
{ apiKey: config3.apiKey, wsUrl: config3.wsUrl },
|
|
3121
|
+
options
|
|
3122
|
+
),
|
|
3123
|
+
handleRedirectCallback: async () => {
|
|
3124
|
+
const result = await completeRedirectLogin({
|
|
3125
|
+
apiKey: config3.apiKey,
|
|
3126
|
+
wsUrl: config3.wsUrl
|
|
3127
|
+
});
|
|
3128
|
+
if (!result) return null;
|
|
3129
|
+
rememberHostedLogin(result);
|
|
3130
|
+
return pkceSession.remember(result);
|
|
3131
|
+
},
|
|
3132
|
+
getIdToken: () => pkceSession.getIdToken(),
|
|
2809
3133
|
// Same popup, but the code goes to the app's backend, which redeems it
|
|
2810
3134
|
// with its apiSecret (see PopupAuthCode). Call it from a click handler.
|
|
2811
3135
|
authorizeWithPopup: () => authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl }),
|
|
@@ -2843,6 +3167,11 @@ function createBlockAuthClient(config3) {
|
|
|
2843
3167
|
);
|
|
2844
3168
|
},
|
|
2845
3169
|
restoreHostedLogin: async () => {
|
|
3170
|
+
const stored = pkceSession.restore();
|
|
3171
|
+
if (stored) {
|
|
3172
|
+
showHostedUser(stored.user);
|
|
3173
|
+
return stored.user;
|
|
3174
|
+
}
|
|
2846
3175
|
if (!authEndpoint) return null;
|
|
2847
3176
|
const user = await authEndpoint.me();
|
|
2848
3177
|
if (user) {
|
|
@@ -2930,6 +3259,9 @@ function createBlockAuthClient(config3) {
|
|
|
2930
3259
|
// restoration-expired modal.
|
|
2931
3260
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2932
3261
|
logout: async () => {
|
|
3262
|
+
const hadPkceSession = pkceSession.hasSession();
|
|
3263
|
+
await pkceSession.signOut();
|
|
3264
|
+
if (hadPkceSession) forgetHostedUser();
|
|
2933
3265
|
if (authEndpoint && !config3.apiUrl && sessionOnAuthEndpoint)
|
|
2934
3266
|
await authEndpoint.logout();
|
|
2935
3267
|
sessionWatch?.stop();
|