@block-auth.io/core 0.11.1 → 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 +355 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +68 -5
- package/dist/index.d.ts +68 -5
- package/dist/index.js +354 -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);
|
|
@@ -2088,21 +2096,147 @@ function connectBlockAuth(apiUrl) {
|
|
|
2088
2096
|
requestExtensionStateSync();
|
|
2089
2097
|
}
|
|
2090
2098
|
|
|
2091
|
-
// src/auth/
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
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) => {
|
|
2095
2137
|
let done = false;
|
|
2096
|
-
const finish = () => {
|
|
2097
|
-
if (
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
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);
|
|
2101
2152
|
};
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
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
|
+
);
|
|
2105
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
|
+
}
|
|
2106
2240
|
if (typeof localStorage !== "undefined") {
|
|
2107
2241
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
2108
2242
|
}
|
|
@@ -2337,6 +2471,19 @@ async function signInWithMicrosoft(overrides = {}) {
|
|
|
2337
2471
|
setServiceIcon(imgUrlByService("microsoft"));
|
|
2338
2472
|
return { accessToken: response.accessToken, response };
|
|
2339
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
|
+
}
|
|
2340
2487
|
function createBlockAuthClient(config3) {
|
|
2341
2488
|
if (config3.locale) setLocale(config3.locale);
|
|
2342
2489
|
const provider = new BlockAuthProvider(
|
|
@@ -2381,13 +2528,15 @@ function createBlockAuthClient(config3) {
|
|
|
2381
2528
|
const ext = browserBase.getExternalService();
|
|
2382
2529
|
return ext.service !== "blockauth" ? ext.token ?? "" : s.token ?? "";
|
|
2383
2530
|
}
|
|
2531
|
+
let runtimeBridge = null;
|
|
2384
2532
|
return {
|
|
2385
2533
|
state: {
|
|
2386
2534
|
$ws,
|
|
2387
2535
|
$ui,
|
|
2388
2536
|
$wallet,
|
|
2389
2537
|
$openModals,
|
|
2390
|
-
$user
|
|
2538
|
+
$user,
|
|
2539
|
+
$extension
|
|
2391
2540
|
},
|
|
2392
2541
|
/** App apiKey from client config (also seeded into $ws). */
|
|
2393
2542
|
apiKey: config3.apiKey,
|
|
@@ -2414,6 +2563,23 @@ function createBlockAuthClient(config3) {
|
|
|
2414
2563
|
// BlockAuth extension connect (sets blockauth as the active service);
|
|
2415
2564
|
// the txn-signing flow is driven by the caller via requestSignin.
|
|
2416
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),
|
|
2417
2583
|
// oauth
|
|
2418
2584
|
oauth: {
|
|
2419
2585
|
...oauth_exports,
|
|
@@ -2454,10 +2620,13 @@ function createBlockAuthClient(config3) {
|
|
|
2454
2620
|
}),
|
|
2455
2621
|
// transaction accept/reject
|
|
2456
2622
|
txns: {
|
|
2457
|
-
reject: () =>
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2623
|
+
reject: () => {
|
|
2624
|
+
setIsRejectingTxn(true);
|
|
2625
|
+
rejectTxn({
|
|
2626
|
+
wsc: provider.wsc,
|
|
2627
|
+
token: userToken()
|
|
2628
|
+
});
|
|
2629
|
+
},
|
|
2461
2630
|
accept: (opts) => acceptTxn({
|
|
2462
2631
|
wsc: provider.wsc,
|
|
2463
2632
|
token: userToken(),
|
|
@@ -2497,6 +2666,10 @@ function createBlockAuthClient(config3) {
|
|
|
2497
2666
|
openModal,
|
|
2498
2667
|
closeModal,
|
|
2499
2668
|
closeAllModals,
|
|
2669
|
+
registerRuntime: (bridge) => {
|
|
2670
|
+
runtimeBridge = bridge;
|
|
2671
|
+
},
|
|
2672
|
+
switchAccount: () => runtimeBridge?.switchAccount() ?? Promise.resolve(),
|
|
2500
2673
|
// opciones del connect inicial del WS (las consume createAuthRuntime)
|
|
2501
2674
|
wsConnectTimeout: config3.wsConnectTimeout ?? 300,
|
|
2502
2675
|
wsConnectRetries: config3.wsConnectRetries ?? 3,
|
|
@@ -2871,6 +3044,87 @@ var undoSolicitedValue = (form, item) => ({
|
|
|
2871
3044
|
shared: (item.value ?? "").trim() ? form.shared : omitKey(form.shared, item.slug)
|
|
2872
3045
|
});
|
|
2873
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
|
+
|
|
2874
3128
|
// src/session/sessionExpired.ts
|
|
2875
3129
|
var USER_SESSION_REQUIRED = "USER_SESSION_REQUIRED";
|
|
2876
3130
|
var SESSION_EXPIRED_MARK = "blockauth-session-expired";
|
|
@@ -2917,8 +3171,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2917
3171
|
let unsignTimeout = null;
|
|
2918
3172
|
let signinTimeout = null;
|
|
2919
3173
|
const wsc = () => provider.wsc;
|
|
2920
|
-
const emitSdkSign = (token, address) => {
|
|
2921
|
-
wsc().emit("sdk:sign", { token, address });
|
|
3174
|
+
const emitSdkSign = (token, address, afterSign = false) => {
|
|
3175
|
+
wsc().emit("sdk:sign", { token, address, afterSign });
|
|
2922
3176
|
};
|
|
2923
3177
|
const ownTxnItems = (items) => {
|
|
2924
3178
|
const apiKey = client.apiKey;
|
|
@@ -2953,6 +3207,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2953
3207
|
});
|
|
2954
3208
|
const onLogout = callbacks.onLogout ?? (async () => {
|
|
2955
3209
|
});
|
|
3210
|
+
const onCancel = callbacks.onCancel ?? (async () => {
|
|
3211
|
+
});
|
|
2956
3212
|
const _onSuccess = async (address) => {
|
|
2957
3213
|
deletion.resetHandlingState();
|
|
2958
3214
|
await onSuccess(address);
|
|
@@ -3290,7 +3546,10 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3290
3546
|
hookState.pendingSignin = null;
|
|
3291
3547
|
hookState.pendingTxnId = null;
|
|
3292
3548
|
hookState.settledAttemptId = hookState.signinAttemptId;
|
|
3293
|
-
if (!hookState.isSigned)
|
|
3549
|
+
if (!hookState.isSigned) {
|
|
3550
|
+
closeAllModals();
|
|
3551
|
+
void onCancel();
|
|
3552
|
+
}
|
|
3294
3553
|
return;
|
|
3295
3554
|
}
|
|
3296
3555
|
if (hookState.settledAttemptId === hookState.signinAttemptId) {
|
|
@@ -3301,18 +3560,24 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3301
3560
|
hookState.pendingSignin = null;
|
|
3302
3561
|
const willVerifySign = outcome === "resolved" && !hookState.isSigned && !!pendingSignin;
|
|
3303
3562
|
if (willVerifySign) {
|
|
3304
|
-
emitSdkSign(pendingSignin.accessToken, pendingSignin.did);
|
|
3563
|
+
emitSdkSign(pendingSignin.accessToken, pendingSignin.did, true);
|
|
3305
3564
|
} else if (!hookState.isSigned) {
|
|
3306
3565
|
closeAllModals();
|
|
3307
3566
|
}
|
|
3308
3567
|
};
|
|
3568
|
+
const consumePendingReject = () => {
|
|
3569
|
+
if (!$ui.get().isRejectingTxn) return false;
|
|
3570
|
+
setIsRejectingTxn(false);
|
|
3571
|
+
settleSigningAttempt("cancelled");
|
|
3572
|
+
return true;
|
|
3573
|
+
};
|
|
3309
3574
|
const onUserTxns = async (data) => {
|
|
3310
3575
|
const { success, message } = data;
|
|
3311
3576
|
if (!success) return;
|
|
3312
3577
|
const items = ownTxnItems(message.items ?? []);
|
|
3313
3578
|
captureOwnTxnId(items);
|
|
3314
3579
|
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
3315
|
-
if (!hasInitialTxns) {
|
|
3580
|
+
if (!hasInitialTxns && !consumePendingReject()) {
|
|
3316
3581
|
settleSigningAttempt("resolved");
|
|
3317
3582
|
}
|
|
3318
3583
|
setRequests(items);
|
|
@@ -3323,7 +3588,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3323
3588
|
const items = ownTxnItems(message.items ?? []);
|
|
3324
3589
|
captureOwnTxnId(items);
|
|
3325
3590
|
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
3326
|
-
if (!hasInitialTxns) {
|
|
3591
|
+
if (!hasInitialTxns && !consumePendingReject()) {
|
|
3327
3592
|
const txnId = hookState.pendingTxnId;
|
|
3328
3593
|
const wasCancelled = txnId != null && !items.some((item) => item.id === txnId);
|
|
3329
3594
|
settleSigningAttempt(
|
|
@@ -3335,6 +3600,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3335
3600
|
const onUserTxnsCreate = async (data) => {
|
|
3336
3601
|
const { success, message } = data;
|
|
3337
3602
|
if (success) {
|
|
3603
|
+
setIsRejectingTxn(false);
|
|
3338
3604
|
setIsSigning(true);
|
|
3339
3605
|
hookState.isSigning = true;
|
|
3340
3606
|
const attemptId = ++hookState.signinAttemptId;
|
|
@@ -3383,6 +3649,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3383
3649
|
setToken(_token);
|
|
3384
3650
|
setIsSigned(success);
|
|
3385
3651
|
hookState.isSigned = success;
|
|
3652
|
+
if (success) setIsAcceptingTxn(false);
|
|
3386
3653
|
if (success) clearSessionExpiredMark();
|
|
3387
3654
|
if (success && hookState.isUnsignTimeoutSet && message?.signedExpiredAt) {
|
|
3388
3655
|
rearmUnsignTimeout(message.signedExpiredAt, _did);
|
|
@@ -3551,10 +3818,10 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3551
3818
|
_token = externalService.token;
|
|
3552
3819
|
_did = externalService.did;
|
|
3553
3820
|
}
|
|
3554
|
-
setDid(_did);
|
|
3555
|
-
setToken(_token);
|
|
3556
|
-
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
3557
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);
|
|
3558
3825
|
setIsServiceConnected(isLoggedOut ? false : externalService.connected);
|
|
3559
3826
|
setServiceName(externalService.service);
|
|
3560
3827
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
@@ -3668,7 +3935,46 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3668
3935
|
const logout = async () => {
|
|
3669
3936
|
await _onLogout();
|
|
3670
3937
|
};
|
|
3671
|
-
|
|
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 };
|
|
3672
3978
|
}
|
|
3673
3979
|
function deepLinkFor(service, domain) {
|
|
3674
3980
|
switch (service) {
|
|
@@ -3743,6 +4049,7 @@ var TEXTS = {
|
|
|
3743
4049
|
solicitedData: { es: "Datos solicitados", en: "Solicited data" },
|
|
3744
4050
|
reject: { es: "RECHAZAR", en: "REJECT" },
|
|
3745
4051
|
accept: { es: "ACEPTAR", en: "ACCEPT" },
|
|
4052
|
+
useAnotherAccount: { es: "Usar otra cuenta", en: "Use another account" },
|
|
3746
4053
|
iAuthorizeThe: { es: "Autorizo la", en: "I authorize the" },
|
|
3747
4054
|
privacyPolicy: { es: "Pol\xEDtica de Privacidad", en: "Privacy Policy" },
|
|
3748
4055
|
andThe: { es: "y los", en: "and the" },
|
|
@@ -3786,6 +4093,7 @@ var TEXTS = {
|
|
|
3786
4093
|
},
|
|
3787
4094
|
clearField: { es: "Limpiar campo", en: "Clear field" },
|
|
3788
4095
|
acceptData: { es: "Aceptar", en: "Accept" },
|
|
4096
|
+
cancelData: { es: "Cancelar", en: "Cancel" },
|
|
3789
4097
|
noTransactionsFound: {
|
|
3790
4098
|
es: "No se encontraron transacciones",
|
|
3791
4099
|
en: "No transactions found"
|
|
@@ -3846,7 +4154,10 @@ var TEXTS = {
|
|
|
3846
4154
|
en: "Enter a valid phone number"
|
|
3847
4155
|
},
|
|
3848
4156
|
phoneCountry: { es: "Pa\xEDs", en: "Country" },
|
|
3849
|
-
searchPhoneCountry: {
|
|
4157
|
+
searchPhoneCountry: {
|
|
4158
|
+
es: "Buscar pa\xEDs o prefijo",
|
|
4159
|
+
en: "Search country or code"
|
|
4160
|
+
},
|
|
3850
4161
|
noPhoneCountryResults: {
|
|
3851
4162
|
es: "No hay pa\xEDses que coincidan",
|
|
3852
4163
|
en: "No matching countries"
|
|
@@ -3954,6 +4265,7 @@ exports.oauth = oauth_exports;
|
|
|
3954
4265
|
exports.onExtensionRemoved = onExtensionRemoved;
|
|
3955
4266
|
exports.onExtensionStatePublished = onExtensionStatePublished;
|
|
3956
4267
|
exports.openExtensionOnTxn = openExtensionOnTxn;
|
|
4268
|
+
exports.openLoginPopup = openLoginPopup;
|
|
3957
4269
|
exports.openModal = openModal;
|
|
3958
4270
|
exports.parsePhoneValue = parsePhoneValue;
|
|
3959
4271
|
exports.probeExtensionPresence = probeExtensionPresence;
|
|
@@ -3987,6 +4299,7 @@ exports.setIsCheckingAppActive = setIsCheckingAppActive;
|
|
|
3987
4299
|
exports.setIsLoadingUser = setIsLoadingUser;
|
|
3988
4300
|
exports.setIsOnDeletion = setIsOnDeletion;
|
|
3989
4301
|
exports.setIsOtpRequired = setIsOtpRequired;
|
|
4302
|
+
exports.setIsRejectingTxn = setIsRejectingTxn;
|
|
3990
4303
|
exports.setIsSdkSignedIn = setIsSdkSignedIn;
|
|
3991
4304
|
exports.setIsServiceConnected = setIsServiceConnected;
|
|
3992
4305
|
exports.setIsSigned = setIsSigned;
|