@block-auth.io/core 0.11.0 → 0.12.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 +357 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -5
- package/dist/index.d.ts +74 -5
- package/dist/index.js +355 -43
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -24,13 +24,11 @@ var __export = (target, all) => {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
// src/utils/apiBridge.ts
|
|
27
|
-
var DEFAULT_API_URL = "https://api.block-auth.io";
|
|
28
27
|
function buildApiUrl(path, apiUrl) {
|
|
29
|
-
|
|
30
|
-
return `${base}${path}`;
|
|
28
|
+
return `${apiUrl.replace(/\/+$/, "")}${path}`;
|
|
31
29
|
}
|
|
32
30
|
async function bridgeSessionCookie(token, apiUrl) {
|
|
33
|
-
if (!token || typeof fetch === "undefined") return false;
|
|
31
|
+
if (!apiUrl || !token || typeof fetch === "undefined") return false;
|
|
34
32
|
try {
|
|
35
33
|
const response = await fetch(buildApiUrl("/auth/session", apiUrl), {
|
|
36
34
|
method: "POST",
|
|
@@ -45,7 +43,7 @@ async function bridgeSessionCookie(token, apiUrl) {
|
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
45
|
async function clearSessionCookie(apiUrl) {
|
|
48
|
-
if (typeof fetch === "undefined") return false;
|
|
46
|
+
if (!apiUrl || typeof fetch === "undefined") return false;
|
|
49
47
|
try {
|
|
50
48
|
const response = await fetch(buildApiUrl("/auth/logout", apiUrl), {
|
|
51
49
|
method: "POST",
|
|
@@ -58,7 +56,9 @@ async function clearSessionCookie(apiUrl) {
|
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
async function requestWsTicket(apiUrl) {
|
|
61
|
-
if (typeof fetch === "undefined")
|
|
59
|
+
if (!apiUrl || typeof fetch === "undefined") {
|
|
60
|
+
return { ticket: null, refused: false };
|
|
61
|
+
}
|
|
62
62
|
try {
|
|
63
63
|
const response = await fetch(buildApiUrl("/auth/ws-ticket", apiUrl), {
|
|
64
64
|
method: "GET",
|
|
@@ -89,14 +89,11 @@ function buildWsServerApiUrl(path, wsUrl) {
|
|
|
89
89
|
async function fetchBrowserToken(apiKey, wsUrl) {
|
|
90
90
|
if (!apiKey || typeof fetch === "undefined") return null;
|
|
91
91
|
try {
|
|
92
|
-
const response = await fetch(
|
|
93
|
-
|
|
94
|
-
{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
body: JSON.stringify({ apiKey })
|
|
98
|
-
}
|
|
99
|
-
);
|
|
92
|
+
const response = await fetch(buildWsServerApiUrl("/browser-token", wsUrl), {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: { "Content-Type": "application/json" },
|
|
95
|
+
body: JSON.stringify({ apiKey })
|
|
96
|
+
});
|
|
100
97
|
const payload = await response.json();
|
|
101
98
|
if (!response.ok || !payload?.success) return null;
|
|
102
99
|
return payload.message?.accessToken || null;
|
|
@@ -127,6 +124,7 @@ var WsClient = class {
|
|
|
127
124
|
this._roles = [];
|
|
128
125
|
this._logs = [];
|
|
129
126
|
this._kycIssuerByAddress = {};
|
|
127
|
+
this._connecting = null;
|
|
130
128
|
this._wsUrl = url || "wss://wss.block-auth.io/";
|
|
131
129
|
this._apiUrl = apiUrl;
|
|
132
130
|
this._wsConfig = {
|
|
@@ -158,6 +156,14 @@ var WsClient = class {
|
|
|
158
156
|
* @returns Promise<void>
|
|
159
157
|
*/
|
|
160
158
|
async connect(url = "", waitMs = 5e3, retries = 3) {
|
|
159
|
+
if (this.isConnected()) return;
|
|
160
|
+
if (this._connecting) return this._connecting;
|
|
161
|
+
this._connecting = this._doConnect(url, waitMs, retries).finally(() => {
|
|
162
|
+
this._connecting = null;
|
|
163
|
+
});
|
|
164
|
+
return this._connecting;
|
|
165
|
+
}
|
|
166
|
+
async _doConnect(url, waitMs, retries) {
|
|
161
167
|
const _url = url || this._wsUrl;
|
|
162
168
|
this._wsConfig.auth = (done) => {
|
|
163
169
|
fetchWsTicket(this._apiUrl).then((ticket) => done(ticket ? { ticket } : {})).catch(() => done({}));
|
|
@@ -1003,6 +1009,7 @@ var initial2 = {
|
|
|
1003
1009
|
isSigned: false,
|
|
1004
1010
|
isSigningLoader: false,
|
|
1005
1011
|
isAcceptingTxn: false,
|
|
1012
|
+
isRejectingTxn: false,
|
|
1006
1013
|
acceptTxnError: null,
|
|
1007
1014
|
waitingForSuccessLogInSince: 0,
|
|
1008
1015
|
isOtpRequired: false,
|
|
@@ -1022,6 +1029,7 @@ var setIsSigning = (v) => $ui.setKey("isSigning", v);
|
|
|
1022
1029
|
var setIsSigned = (v) => $ui.setKey("isSigned", v);
|
|
1023
1030
|
var setIsSigningLoader = (v) => $ui.setKey("isSigningLoader", v);
|
|
1024
1031
|
var setIsAcceptingTxn = (v) => $ui.setKey("isAcceptingTxn", v);
|
|
1032
|
+
var setIsRejectingTxn = (v) => $ui.setKey("isRejectingTxn", v);
|
|
1025
1033
|
var setAcceptTxnError = (v) => $ui.setKey("acceptTxnError", v);
|
|
1026
1034
|
var setWaitingForSuccessLogInSince = (v) => $ui.setKey("waitingForSuccessLogInSince", v);
|
|
1027
1035
|
var setIsOtpRequired = (v) => $ui.setKey("isOtpRequired", v);
|
|
@@ -1103,6 +1111,7 @@ var setExtensionIsUser = (v) => $extension.setKey("isUser", v);
|
|
|
1103
1111
|
var setExtensionIsUserActive = (v) => $extension.setKey("isUserActive", v);
|
|
1104
1112
|
var resetExtension = () => $extension.set({ ...initial5 });
|
|
1105
1113
|
var isShowSignin = (state = $extension.get()) => Boolean(state.installed && state.isUser && state.isUserActive && !state.isLocked);
|
|
1114
|
+
var needsExtensionUnlock = (serviceName, did, isLocked) => serviceName === "blockauth" && (isLocked || !did);
|
|
1106
1115
|
var initial6 = { backendMessage: null, signature: null, signedBy: null };
|
|
1107
1116
|
var $web3 = nanostores.map({ ...initial6 });
|
|
1108
1117
|
var setBackendMessage = (v) => $web3.setKey("backendMessage", v);
|
|
@@ -2087,21 +2096,147 @@ function connectBlockAuth(apiUrl) {
|
|
|
2087
2096
|
requestExtensionStateSync();
|
|
2088
2097
|
}
|
|
2089
2098
|
|
|
2090
|
-
// src/auth/
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2099
|
+
// src/auth/popupSize.ts
|
|
2100
|
+
var LOGIN_POPUP_WIDTH = 380;
|
|
2101
|
+
var LOGIN_POPUP_HEIGHT = 640;
|
|
2102
|
+
|
|
2103
|
+
// src/auth/loginWithPopup.ts
|
|
2104
|
+
var AUTH_CODE_MESSAGE = "blockauth:auth-code";
|
|
2105
|
+
var POPUP_CLOSED_POLL_MS = 500;
|
|
2106
|
+
var LOGIN_TIMEOUT_MS = 15 * 60 * 1e3;
|
|
2107
|
+
function base64url(bytes) {
|
|
2108
|
+
let binary = "";
|
|
2109
|
+
for (const b of bytes) binary += String.fromCharCode(b);
|
|
2110
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
2111
|
+
}
|
|
2112
|
+
function randomString(byteLength) {
|
|
2113
|
+
return base64url(crypto.getRandomValues(new Uint8Array(byteLength)));
|
|
2114
|
+
}
|
|
2115
|
+
async function sha256Base64url(value) {
|
|
2116
|
+
const digest = await crypto.subtle.digest(
|
|
2117
|
+
"SHA-256",
|
|
2118
|
+
new TextEncoder().encode(value)
|
|
2119
|
+
);
|
|
2120
|
+
return base64url(new Uint8Array(digest));
|
|
2121
|
+
}
|
|
2122
|
+
async function postWsServer(path, body, wsUrl) {
|
|
2123
|
+
const response = await fetch(buildWsServerApiUrl(path, wsUrl), {
|
|
2124
|
+
method: "POST",
|
|
2125
|
+
headers: { "Content-Type": "application/json" },
|
|
2126
|
+
body: JSON.stringify(body)
|
|
2127
|
+
});
|
|
2128
|
+
const payload = await response.json();
|
|
2129
|
+
if (!response.ok || !payload?.success) {
|
|
2130
|
+
const reason = typeof payload?.message === "string" ? payload.message : "Login failed";
|
|
2131
|
+
throw new Error(reason);
|
|
2132
|
+
}
|
|
2133
|
+
return payload.message;
|
|
2134
|
+
}
|
|
2135
|
+
function waitForCode(popup, authOrigin, state) {
|
|
2136
|
+
return new Promise((resolve, reject) => {
|
|
2094
2137
|
let done = false;
|
|
2095
|
-
const finish = () => {
|
|
2096
|
-
if (
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2138
|
+
const finish = (error, code) => {
|
|
2139
|
+
if (done) return;
|
|
2140
|
+
done = true;
|
|
2141
|
+
window.removeEventListener("message", onMessage);
|
|
2142
|
+
clearInterval(closedPoll);
|
|
2143
|
+
clearTimeout(timeout);
|
|
2144
|
+
if (error) reject(error);
|
|
2145
|
+
else resolve(code);
|
|
2146
|
+
};
|
|
2147
|
+
const onMessage = (event) => {
|
|
2148
|
+
if (event.origin !== authOrigin || event.source !== popup) return;
|
|
2149
|
+
const data = event.data;
|
|
2150
|
+
if (data?.type !== AUTH_CODE_MESSAGE || data.state !== state) return;
|
|
2151
|
+
if (data.code) finish(null, data.code);
|
|
2100
2152
|
};
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2153
|
+
window.addEventListener("message", onMessage);
|
|
2154
|
+
const closedPoll = setInterval(() => {
|
|
2155
|
+
if (!popup.closed) return;
|
|
2156
|
+
setTimeout(
|
|
2157
|
+
() => finish(new Error("Login window closed")),
|
|
2158
|
+
POPUP_CLOSED_POLL_MS
|
|
2159
|
+
);
|
|
2160
|
+
}, POPUP_CLOSED_POLL_MS);
|
|
2161
|
+
const timeout = setTimeout(
|
|
2162
|
+
() => finish(new Error("Login timed out")),
|
|
2163
|
+
LOGIN_TIMEOUT_MS
|
|
2164
|
+
);
|
|
2104
2165
|
});
|
|
2166
|
+
}
|
|
2167
|
+
function popupFeatures() {
|
|
2168
|
+
const left = Math.max(
|
|
2169
|
+
0,
|
|
2170
|
+
Math.round(window.screenX + (window.outerWidth - LOGIN_POPUP_WIDTH) / 2)
|
|
2171
|
+
);
|
|
2172
|
+
const top = Math.max(
|
|
2173
|
+
0,
|
|
2174
|
+
Math.round(window.screenY + (window.outerHeight - LOGIN_POPUP_HEIGHT) / 2)
|
|
2175
|
+
);
|
|
2176
|
+
return `popup,width=${LOGIN_POPUP_WIDTH},height=${LOGIN_POPUP_HEIGHT},left=${left},top=${top}`;
|
|
2177
|
+
}
|
|
2178
|
+
async function runAuthorize(options) {
|
|
2179
|
+
const popup = window.open("about:blank", "blockauth-login", popupFeatures());
|
|
2180
|
+
if (!popup) throw new Error("Login popup blocked");
|
|
2181
|
+
const redirectUri = `${window.location.origin}/`;
|
|
2182
|
+
try {
|
|
2183
|
+
const codeVerifier = randomString(32);
|
|
2184
|
+
const state = randomString(16);
|
|
2185
|
+
const codeChallenge = await sha256Base64url(codeVerifier);
|
|
2186
|
+
const { url } = await postWsServer(
|
|
2187
|
+
"/auth/authorize",
|
|
2188
|
+
{ apiKey: options.apiKey, redirectUri, state, codeChallenge },
|
|
2189
|
+
options.wsUrl
|
|
2190
|
+
);
|
|
2191
|
+
popup.location.href = url;
|
|
2192
|
+
const code = await waitForCode(popup, new URL(url).origin, state);
|
|
2193
|
+
return { code, codeVerifier, redirectUri };
|
|
2194
|
+
} catch (error) {
|
|
2195
|
+
popup.close();
|
|
2196
|
+
throw error;
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
var inFlight = null;
|
|
2200
|
+
var loginInFlight = null;
|
|
2201
|
+
function authorizeWithPopup(options) {
|
|
2202
|
+
inFlight ?? (inFlight = runAuthorize(options).finally(() => {
|
|
2203
|
+
inFlight = null;
|
|
2204
|
+
}));
|
|
2205
|
+
return inFlight;
|
|
2206
|
+
}
|
|
2207
|
+
function loginWithPopup(options) {
|
|
2208
|
+
loginInFlight ?? (loginInFlight = authorizeWithPopup(options).then(
|
|
2209
|
+
({ code, codeVerifier, redirectUri }) => postWsServer(
|
|
2210
|
+
"/auth/token",
|
|
2211
|
+
{ apiKey: options.apiKey, code, redirectUri, codeVerifier },
|
|
2212
|
+
options.wsUrl
|
|
2213
|
+
)
|
|
2214
|
+
).finally(() => {
|
|
2215
|
+
loginInFlight = null;
|
|
2216
|
+
}));
|
|
2217
|
+
return loginInFlight;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
// src/auth/logout.ts
|
|
2221
|
+
async function performLogout(args) {
|
|
2222
|
+
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
2223
|
+
if (apiUrl) {
|
|
2224
|
+
await new Promise((resolve) => {
|
|
2225
|
+
let done = false;
|
|
2226
|
+
const finish = () => {
|
|
2227
|
+
if (!done) {
|
|
2228
|
+
done = true;
|
|
2229
|
+
resolve();
|
|
2230
|
+
}
|
|
2231
|
+
};
|
|
2232
|
+
wsc.ws?.once?.("signout", finish);
|
|
2233
|
+
wsc.emit("signout", { token, apiKey, sessionId });
|
|
2234
|
+
setTimeout(finish, 1500);
|
|
2235
|
+
});
|
|
2236
|
+
} else {
|
|
2237
|
+
resetUser();
|
|
2238
|
+
browserBase.reset("session");
|
|
2239
|
+
}
|
|
2105
2240
|
if (typeof localStorage !== "undefined") {
|
|
2106
2241
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
2107
2242
|
}
|
|
@@ -2336,6 +2471,19 @@ async function signInWithMicrosoft(overrides = {}) {
|
|
|
2336
2471
|
setServiceIcon(imgUrlByService("microsoft"));
|
|
2337
2472
|
return { accessToken: response.accessToken, response };
|
|
2338
2473
|
}
|
|
2474
|
+
function rememberHostedLogin(result) {
|
|
2475
|
+
const did = result.did ?? result.address;
|
|
2476
|
+
browserBase.setSession({ did, serviceName: "blockauth" });
|
|
2477
|
+
setUser({
|
|
2478
|
+
address: result.address,
|
|
2479
|
+
did,
|
|
2480
|
+
roles: result.roles ?? []
|
|
2481
|
+
});
|
|
2482
|
+
setServiceName("blockauth");
|
|
2483
|
+
setIsServiceConnected(true);
|
|
2484
|
+
setIsSigned(true);
|
|
2485
|
+
setIsSignedFirstCheck(true);
|
|
2486
|
+
}
|
|
2339
2487
|
function createBlockAuthClient(config3) {
|
|
2340
2488
|
if (config3.locale) setLocale(config3.locale);
|
|
2341
2489
|
const provider = new BlockAuthProvider(
|
|
@@ -2380,13 +2528,15 @@ function createBlockAuthClient(config3) {
|
|
|
2380
2528
|
const ext = browserBase.getExternalService();
|
|
2381
2529
|
return ext.service !== "blockauth" ? ext.token ?? "" : s.token ?? "";
|
|
2382
2530
|
}
|
|
2531
|
+
let runtimeBridge = null;
|
|
2383
2532
|
return {
|
|
2384
2533
|
state: {
|
|
2385
2534
|
$ws,
|
|
2386
2535
|
$ui,
|
|
2387
2536
|
$wallet,
|
|
2388
2537
|
$openModals,
|
|
2389
|
-
$user
|
|
2538
|
+
$user,
|
|
2539
|
+
$extension
|
|
2390
2540
|
},
|
|
2391
2541
|
/** App apiKey from client config (also seeded into $ws). */
|
|
2392
2542
|
apiKey: config3.apiKey,
|
|
@@ -2413,6 +2563,23 @@ function createBlockAuthClient(config3) {
|
|
|
2413
2563
|
// BlockAuth extension connect (sets blockauth as the active service);
|
|
2414
2564
|
// the txn-signing flow is driven by the caller via requestSignin.
|
|
2415
2565
|
connectBlockAuth: () => connectBlockAuth(config3.apiUrl),
|
|
2566
|
+
// Login for an app without a backend: hosted login in a popup, PKCE
|
|
2567
|
+
// instead of the apiSecret (see auth/loginWithPopup.ts). Call it from a
|
|
2568
|
+
// click handler.
|
|
2569
|
+
loginWithPopup: async () => {
|
|
2570
|
+
const result = await loginWithPopup({
|
|
2571
|
+
apiKey: config3.apiKey,
|
|
2572
|
+
wsUrl: config3.wsUrl
|
|
2573
|
+
});
|
|
2574
|
+
rememberHostedLogin(result);
|
|
2575
|
+
return result;
|
|
2576
|
+
},
|
|
2577
|
+
// Same popup, but the code goes to the app's backend, which redeems it
|
|
2578
|
+
// with its apiSecret (see PopupAuthCode). Call it from a click handler.
|
|
2579
|
+
authorizeWithPopup: () => authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl }),
|
|
2580
|
+
// After the backend redeemed the code: shows the user in the page's UI
|
|
2581
|
+
// (profile dropdown, roles). The backend's answer is the source of truth.
|
|
2582
|
+
rememberHostedLogin: (user) => rememberHostedLogin(user),
|
|
2416
2583
|
// oauth
|
|
2417
2584
|
oauth: {
|
|
2418
2585
|
...oauth_exports,
|
|
@@ -2453,10 +2620,13 @@ function createBlockAuthClient(config3) {
|
|
|
2453
2620
|
}),
|
|
2454
2621
|
// transaction accept/reject
|
|
2455
2622
|
txns: {
|
|
2456
|
-
reject: () =>
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2623
|
+
reject: () => {
|
|
2624
|
+
setIsRejectingTxn(true);
|
|
2625
|
+
rejectTxn({
|
|
2626
|
+
wsc: provider.wsc,
|
|
2627
|
+
token: userToken()
|
|
2628
|
+
});
|
|
2629
|
+
},
|
|
2460
2630
|
accept: (opts) => acceptTxn({
|
|
2461
2631
|
wsc: provider.wsc,
|
|
2462
2632
|
token: userToken(),
|
|
@@ -2496,6 +2666,10 @@ function createBlockAuthClient(config3) {
|
|
|
2496
2666
|
openModal,
|
|
2497
2667
|
closeModal,
|
|
2498
2668
|
closeAllModals,
|
|
2669
|
+
registerRuntime: (bridge) => {
|
|
2670
|
+
runtimeBridge = bridge;
|
|
2671
|
+
},
|
|
2672
|
+
switchAccount: () => runtimeBridge?.switchAccount() ?? Promise.resolve(),
|
|
2499
2673
|
// opciones del connect inicial del WS (las consume createAuthRuntime)
|
|
2500
2674
|
wsConnectTimeout: config3.wsConnectTimeout ?? 300,
|
|
2501
2675
|
wsConnectRetries: config3.wsConnectRetries ?? 3,
|
|
@@ -2870,6 +3044,87 @@ var undoSolicitedValue = (form, item) => ({
|
|
|
2870
3044
|
shared: (item.value ?? "").trim() ? form.shared : omitKey(form.shared, item.slug)
|
|
2871
3045
|
});
|
|
2872
3046
|
|
|
3047
|
+
// src/auth/redirectPopup.ts
|
|
3048
|
+
var AUTH_CODE_MESSAGE2 = "blockauth:auth-code";
|
|
3049
|
+
function centeredPopupFeatures(width, height) {
|
|
3050
|
+
const base = `popup,width=${width},height=${height}`;
|
|
3051
|
+
if (typeof window === "undefined") return base;
|
|
3052
|
+
const screenLeft = window.screenLeft ?? window.screenX ?? 0;
|
|
3053
|
+
const screenTop = window.screenTop ?? window.screenY ?? 0;
|
|
3054
|
+
const viewportWidth = window.innerWidth || document.documentElement?.clientWidth || window.screen?.width || width;
|
|
3055
|
+
const viewportHeight = window.innerHeight || document.documentElement?.clientHeight || window.screen?.height || height;
|
|
3056
|
+
const left = Math.max(
|
|
3057
|
+
0,
|
|
3058
|
+
Math.round(screenLeft + (viewportWidth - width) / 2)
|
|
3059
|
+
);
|
|
3060
|
+
const top = Math.max(
|
|
3061
|
+
0,
|
|
3062
|
+
Math.round(screenTop + (viewportHeight - height) / 2)
|
|
3063
|
+
);
|
|
3064
|
+
return `${base},left=${left},top=${top}`;
|
|
3065
|
+
}
|
|
3066
|
+
function openLoginPopup({
|
|
3067
|
+
url,
|
|
3068
|
+
expectedOrigin,
|
|
3069
|
+
timeoutMs = 3e5,
|
|
3070
|
+
windowName = "blockauth-login",
|
|
3071
|
+
windowFeatures,
|
|
3072
|
+
width = LOGIN_POPUP_WIDTH,
|
|
3073
|
+
height = LOGIN_POPUP_HEIGHT
|
|
3074
|
+
}) {
|
|
3075
|
+
return new Promise((resolve, reject) => {
|
|
3076
|
+
if (typeof window === "undefined" || typeof window.open !== "function") {
|
|
3077
|
+
reject(new Error("Popup login is only available in a browser"));
|
|
3078
|
+
return;
|
|
3079
|
+
}
|
|
3080
|
+
const features = windowFeatures ?? centeredPopupFeatures(width, height);
|
|
3081
|
+
const popup = window.open(url, windowName, features);
|
|
3082
|
+
if (!popup) {
|
|
3083
|
+
reject(new Error("Popup blocked"));
|
|
3084
|
+
return;
|
|
3085
|
+
}
|
|
3086
|
+
let settled = false;
|
|
3087
|
+
let closedTimer = null;
|
|
3088
|
+
let timeoutTimer = null;
|
|
3089
|
+
const cleanup = () => {
|
|
3090
|
+
window.removeEventListener("message", onMessage);
|
|
3091
|
+
if (closedTimer !== null) clearInterval(closedTimer);
|
|
3092
|
+
if (timeoutTimer !== null) clearTimeout(timeoutTimer);
|
|
3093
|
+
};
|
|
3094
|
+
const finish = (action) => {
|
|
3095
|
+
if (settled) return;
|
|
3096
|
+
settled = true;
|
|
3097
|
+
cleanup();
|
|
3098
|
+
action();
|
|
3099
|
+
};
|
|
3100
|
+
const onMessage = (event) => {
|
|
3101
|
+
if (event.origin !== expectedOrigin) return;
|
|
3102
|
+
if (event.source !== popup) return;
|
|
3103
|
+
const data = event.data;
|
|
3104
|
+
if (!data || data.type !== AUTH_CODE_MESSAGE2) return;
|
|
3105
|
+
if (typeof data.code !== "string" || typeof data.state !== "string") {
|
|
3106
|
+
finish(() => reject(new Error("Malformed auth code message")));
|
|
3107
|
+
return;
|
|
3108
|
+
}
|
|
3109
|
+
finish(
|
|
3110
|
+
() => resolve({ code: data.code, state: data.state })
|
|
3111
|
+
);
|
|
3112
|
+
};
|
|
3113
|
+
window.addEventListener("message", onMessage);
|
|
3114
|
+
closedTimer = setInterval(() => {
|
|
3115
|
+
if (popup.closed) {
|
|
3116
|
+
finish(() => reject(new Error("Login popup closed")));
|
|
3117
|
+
}
|
|
3118
|
+
}, 500);
|
|
3119
|
+
timeoutTimer = setTimeout(() => {
|
|
3120
|
+
finish(() => {
|
|
3121
|
+
if (!popup.closed) popup.close();
|
|
3122
|
+
reject(new Error("Login popup timed out"));
|
|
3123
|
+
});
|
|
3124
|
+
}, timeoutMs);
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
|
|
2873
3128
|
// src/session/sessionExpired.ts
|
|
2874
3129
|
var USER_SESSION_REQUIRED = "USER_SESSION_REQUIRED";
|
|
2875
3130
|
var SESSION_EXPIRED_MARK = "blockauth-session-expired";
|
|
@@ -2916,8 +3171,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2916
3171
|
let unsignTimeout = null;
|
|
2917
3172
|
let signinTimeout = null;
|
|
2918
3173
|
const wsc = () => provider.wsc;
|
|
2919
|
-
const emitSdkSign = (token, address) => {
|
|
2920
|
-
wsc().emit("sdk:sign", { token, address });
|
|
3174
|
+
const emitSdkSign = (token, address, afterSign = false) => {
|
|
3175
|
+
wsc().emit("sdk:sign", { token, address, afterSign });
|
|
2921
3176
|
};
|
|
2922
3177
|
const ownTxnItems = (items) => {
|
|
2923
3178
|
const apiKey = client.apiKey;
|
|
@@ -2952,6 +3207,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2952
3207
|
});
|
|
2953
3208
|
const onLogout = callbacks.onLogout ?? (async () => {
|
|
2954
3209
|
});
|
|
3210
|
+
const onCancel = callbacks.onCancel ?? (async () => {
|
|
3211
|
+
});
|
|
2955
3212
|
const _onSuccess = async (address) => {
|
|
2956
3213
|
deletion.resetHandlingState();
|
|
2957
3214
|
await onSuccess(address);
|
|
@@ -3289,7 +3546,10 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3289
3546
|
hookState.pendingSignin = null;
|
|
3290
3547
|
hookState.pendingTxnId = null;
|
|
3291
3548
|
hookState.settledAttemptId = hookState.signinAttemptId;
|
|
3292
|
-
if (!hookState.isSigned)
|
|
3549
|
+
if (!hookState.isSigned) {
|
|
3550
|
+
closeAllModals();
|
|
3551
|
+
void onCancel();
|
|
3552
|
+
}
|
|
3293
3553
|
return;
|
|
3294
3554
|
}
|
|
3295
3555
|
if (hookState.settledAttemptId === hookState.signinAttemptId) {
|
|
@@ -3300,18 +3560,24 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3300
3560
|
hookState.pendingSignin = null;
|
|
3301
3561
|
const willVerifySign = outcome === "resolved" && !hookState.isSigned && !!pendingSignin;
|
|
3302
3562
|
if (willVerifySign) {
|
|
3303
|
-
emitSdkSign(pendingSignin.accessToken, pendingSignin.did);
|
|
3563
|
+
emitSdkSign(pendingSignin.accessToken, pendingSignin.did, true);
|
|
3304
3564
|
} else if (!hookState.isSigned) {
|
|
3305
3565
|
closeAllModals();
|
|
3306
3566
|
}
|
|
3307
3567
|
};
|
|
3568
|
+
const consumePendingReject = () => {
|
|
3569
|
+
if (!$ui.get().isRejectingTxn) return false;
|
|
3570
|
+
setIsRejectingTxn(false);
|
|
3571
|
+
settleSigningAttempt("cancelled");
|
|
3572
|
+
return true;
|
|
3573
|
+
};
|
|
3308
3574
|
const onUserTxns = async (data) => {
|
|
3309
3575
|
const { success, message } = data;
|
|
3310
3576
|
if (!success) return;
|
|
3311
3577
|
const items = ownTxnItems(message.items ?? []);
|
|
3312
3578
|
captureOwnTxnId(items);
|
|
3313
3579
|
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
3314
|
-
if (!hasInitialTxns) {
|
|
3580
|
+
if (!hasInitialTxns && !consumePendingReject()) {
|
|
3315
3581
|
settleSigningAttempt("resolved");
|
|
3316
3582
|
}
|
|
3317
3583
|
setRequests(items);
|
|
@@ -3322,7 +3588,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3322
3588
|
const items = ownTxnItems(message.items ?? []);
|
|
3323
3589
|
captureOwnTxnId(items);
|
|
3324
3590
|
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
3325
|
-
if (!hasInitialTxns) {
|
|
3591
|
+
if (!hasInitialTxns && !consumePendingReject()) {
|
|
3326
3592
|
const txnId = hookState.pendingTxnId;
|
|
3327
3593
|
const wasCancelled = txnId != null && !items.some((item) => item.id === txnId);
|
|
3328
3594
|
settleSigningAttempt(
|
|
@@ -3334,6 +3600,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3334
3600
|
const onUserTxnsCreate = async (data) => {
|
|
3335
3601
|
const { success, message } = data;
|
|
3336
3602
|
if (success) {
|
|
3603
|
+
setIsRejectingTxn(false);
|
|
3337
3604
|
setIsSigning(true);
|
|
3338
3605
|
hookState.isSigning = true;
|
|
3339
3606
|
const attemptId = ++hookState.signinAttemptId;
|
|
@@ -3382,6 +3649,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3382
3649
|
setToken(_token);
|
|
3383
3650
|
setIsSigned(success);
|
|
3384
3651
|
hookState.isSigned = success;
|
|
3652
|
+
if (success) setIsAcceptingTxn(false);
|
|
3385
3653
|
if (success) clearSessionExpiredMark();
|
|
3386
3654
|
if (success && hookState.isUnsignTimeoutSet && message?.signedExpiredAt) {
|
|
3387
3655
|
rearmUnsignTimeout(message.signedExpiredAt, _did);
|
|
@@ -3550,10 +3818,10 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3550
3818
|
_token = externalService.token;
|
|
3551
3819
|
_did = externalService.did;
|
|
3552
3820
|
}
|
|
3553
|
-
setDid(_did);
|
|
3554
|
-
setToken(_token);
|
|
3555
|
-
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
3556
3821
|
const isLoggedOut = typeof localStorage !== "undefined" && localStorage.getItem("blockauth-logged-out") === "true";
|
|
3822
|
+
setDid(isLoggedOut ? null : _did);
|
|
3823
|
+
setToken(isLoggedOut ? "" : _token);
|
|
3824
|
+
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
3557
3825
|
setIsServiceConnected(isLoggedOut ? false : externalService.connected);
|
|
3558
3826
|
setServiceName(externalService.service);
|
|
3559
3827
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
@@ -3667,7 +3935,46 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3667
3935
|
const logout = async () => {
|
|
3668
3936
|
await _onLogout();
|
|
3669
3937
|
};
|
|
3670
|
-
|
|
3938
|
+
const switchAccount = async () => {
|
|
3939
|
+
if (hookState.isLoggingOut) return;
|
|
3940
|
+
if (unsignTimeout) {
|
|
3941
|
+
clearTimeout(unsignTimeout);
|
|
3942
|
+
unsignTimeout = null;
|
|
3943
|
+
}
|
|
3944
|
+
if (signinTimeout) {
|
|
3945
|
+
clearTimeout(signinTimeout);
|
|
3946
|
+
signinTimeout = null;
|
|
3947
|
+
}
|
|
3948
|
+
if (getUserTxnsInterval) {
|
|
3949
|
+
clearInterval(getUserTxnsInterval);
|
|
3950
|
+
getUserTxnsInterval = null;
|
|
3951
|
+
}
|
|
3952
|
+
hookState.isUnsignTimeoutSet = false;
|
|
3953
|
+
hookState.isSigning = false;
|
|
3954
|
+
hookState.pendingSignin = null;
|
|
3955
|
+
hookState.pendingTxnId = null;
|
|
3956
|
+
hookState.settledAttemptId = hookState.signinAttemptId;
|
|
3957
|
+
setIsSigning(false);
|
|
3958
|
+
setIsSigningLoader(false);
|
|
3959
|
+
setIsAcceptingTxn(false);
|
|
3960
|
+
setIsOtpRequired(false);
|
|
3961
|
+
setWaitingForSuccessLogInSince(0);
|
|
3962
|
+
closeAllModals();
|
|
3963
|
+
openModal("signup");
|
|
3964
|
+
const s = browserBase.getSession();
|
|
3965
|
+
const ext = browserBase.getExternalService();
|
|
3966
|
+
const token = ext.service !== "blockauth" ? ext.token : s.token;
|
|
3967
|
+
const apiKey = $ws.get().apiKey ?? client.apiKey;
|
|
3968
|
+
const sessionId = browserBase.getDeviceSessionId();
|
|
3969
|
+
await performLogout({
|
|
3970
|
+
wsc: wsc(),
|
|
3971
|
+
token: token ?? "",
|
|
3972
|
+
apiKey,
|
|
3973
|
+
sessionId,
|
|
3974
|
+
apiUrl: client.apiUrl
|
|
3975
|
+
});
|
|
3976
|
+
};
|
|
3977
|
+
return { start, stop, logout, switchAccount };
|
|
3671
3978
|
}
|
|
3672
3979
|
function deepLinkFor(service, domain) {
|
|
3673
3980
|
switch (service) {
|
|
@@ -3742,6 +4049,7 @@ var TEXTS = {
|
|
|
3742
4049
|
solicitedData: { es: "Datos solicitados", en: "Solicited data" },
|
|
3743
4050
|
reject: { es: "RECHAZAR", en: "REJECT" },
|
|
3744
4051
|
accept: { es: "ACEPTAR", en: "ACCEPT" },
|
|
4052
|
+
useAnotherAccount: { es: "Usar otra cuenta", en: "Use another account" },
|
|
3745
4053
|
iAuthorizeThe: { es: "Autorizo la", en: "I authorize the" },
|
|
3746
4054
|
privacyPolicy: { es: "Pol\xEDtica de Privacidad", en: "Privacy Policy" },
|
|
3747
4055
|
andThe: { es: "y los", en: "and the" },
|
|
@@ -3785,6 +4093,7 @@ var TEXTS = {
|
|
|
3785
4093
|
},
|
|
3786
4094
|
clearField: { es: "Limpiar campo", en: "Clear field" },
|
|
3787
4095
|
acceptData: { es: "Aceptar", en: "Accept" },
|
|
4096
|
+
cancelData: { es: "Cancelar", en: "Cancel" },
|
|
3788
4097
|
noTransactionsFound: {
|
|
3789
4098
|
es: "No se encontraron transacciones",
|
|
3790
4099
|
en: "No transactions found"
|
|
@@ -3845,7 +4154,10 @@ var TEXTS = {
|
|
|
3845
4154
|
en: "Enter a valid phone number"
|
|
3846
4155
|
},
|
|
3847
4156
|
phoneCountry: { es: "Pa\xEDs", en: "Country" },
|
|
3848
|
-
searchPhoneCountry: {
|
|
4157
|
+
searchPhoneCountry: {
|
|
4158
|
+
es: "Buscar pa\xEDs o prefijo",
|
|
4159
|
+
en: "Search country or code"
|
|
4160
|
+
},
|
|
3849
4161
|
noPhoneCountryResults: {
|
|
3850
4162
|
es: "No hay pa\xEDses que coincidan",
|
|
3851
4163
|
en: "No matching countries"
|
|
@@ -3947,11 +4259,13 @@ exports.isValidPhoneValue = isValidPhoneValue;
|
|
|
3947
4259
|
exports.loadUserProfile = loadUserProfile;
|
|
3948
4260
|
exports.makeQrDataUrl = makeQrDataUrl;
|
|
3949
4261
|
exports.markSdkPresence = markSdkPresence;
|
|
4262
|
+
exports.needsExtensionUnlock = needsExtensionUnlock;
|
|
3950
4263
|
exports.normalizeParamValue = normalizeParamValue;
|
|
3951
4264
|
exports.oauth = oauth_exports;
|
|
3952
4265
|
exports.onExtensionRemoved = onExtensionRemoved;
|
|
3953
4266
|
exports.onExtensionStatePublished = onExtensionStatePublished;
|
|
3954
4267
|
exports.openExtensionOnTxn = openExtensionOnTxn;
|
|
4268
|
+
exports.openLoginPopup = openLoginPopup;
|
|
3955
4269
|
exports.openModal = openModal;
|
|
3956
4270
|
exports.parsePhoneValue = parsePhoneValue;
|
|
3957
4271
|
exports.probeExtensionPresence = probeExtensionPresence;
|
|
@@ -3985,6 +4299,7 @@ exports.setIsCheckingAppActive = setIsCheckingAppActive;
|
|
|
3985
4299
|
exports.setIsLoadingUser = setIsLoadingUser;
|
|
3986
4300
|
exports.setIsOnDeletion = setIsOnDeletion;
|
|
3987
4301
|
exports.setIsOtpRequired = setIsOtpRequired;
|
|
4302
|
+
exports.setIsRejectingTxn = setIsRejectingTxn;
|
|
3988
4303
|
exports.setIsSdkSignedIn = setIsSdkSignedIn;
|
|
3989
4304
|
exports.setIsServiceConnected = setIsServiceConnected;
|
|
3990
4305
|
exports.setIsSigned = setIsSigned;
|