@block-auth.io/core 0.4.1 → 0.5.1
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 +364 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +78 -6
- package/dist/index.d.ts +78 -6
- package/dist/index.js +364 -72
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -21,8 +21,70 @@ var __export = (target, all) => {
|
|
|
21
21
|
for (var name in all)
|
|
22
22
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
23
|
};
|
|
24
|
+
|
|
25
|
+
// src/utils/apiBridge.ts
|
|
26
|
+
var DEFAULT_API_URL = "https://api.block-auth.io";
|
|
27
|
+
function buildApiUrl(path, apiUrl) {
|
|
28
|
+
const base = (apiUrl || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
29
|
+
return `${base}${path}`;
|
|
30
|
+
}
|
|
31
|
+
async function bridgeSessionCookie(token, apiUrl) {
|
|
32
|
+
if (!token || typeof fetch === "undefined") return false;
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(buildApiUrl("/auth/session", apiUrl), {
|
|
35
|
+
method: "POST",
|
|
36
|
+
credentials: "include",
|
|
37
|
+
headers: { "Content-Type": "application/json" },
|
|
38
|
+
body: JSON.stringify({ token })
|
|
39
|
+
});
|
|
40
|
+
return response.ok;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.warn("[block-auth] failed to bridge session cookie", error);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function clearSessionCookie(apiUrl) {
|
|
47
|
+
if (typeof fetch === "undefined") return false;
|
|
48
|
+
try {
|
|
49
|
+
const response = await fetch(buildApiUrl("/auth/logout", apiUrl), {
|
|
50
|
+
method: "POST",
|
|
51
|
+
credentials: "include"
|
|
52
|
+
});
|
|
53
|
+
return response.ok;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
console.warn("[block-auth] failed to clear session cookie", error);
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function fetchWsTicket(apiUrl) {
|
|
60
|
+
if (typeof fetch === "undefined") return null;
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(buildApiUrl("/auth/ws-ticket", apiUrl), {
|
|
63
|
+
method: "GET",
|
|
64
|
+
credentials: "include"
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) return null;
|
|
67
|
+
const data = await response.json();
|
|
68
|
+
return data?.ticket || null;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.warn("[block-auth] failed to fetch ws ticket", error);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/protocol/WsClient.ts
|
|
24
76
|
var WsClient = class {
|
|
25
|
-
|
|
77
|
+
/**
|
|
78
|
+
* @param url - the wss:// endpoint to connect to.
|
|
79
|
+
* @param reconnect - allow socket.io to auto-reconnect.
|
|
80
|
+
* @param apiUrl - backend base url used to fetch the short-lived WS
|
|
81
|
+
* connect ticket (GET /auth/ws-ticket) right before opening the socket.
|
|
82
|
+
* Defaults to https://api.block-auth.io. The cookie set by
|
|
83
|
+
* POST /auth/session is host-only to the API origin, so it can't reach
|
|
84
|
+
* this (different-host) WS endpoint directly — the ticket is what
|
|
85
|
+
* actually authenticates the connection now.
|
|
86
|
+
*/
|
|
87
|
+
constructor(url = "", reconnect = false, apiUrl) {
|
|
26
88
|
this._uiConfig = {};
|
|
27
89
|
this._accessToken = "";
|
|
28
90
|
this._refreshToken = "";
|
|
@@ -32,8 +94,13 @@ var WsClient = class {
|
|
|
32
94
|
this._logs = [];
|
|
33
95
|
this._kycIssuerByAddress = {};
|
|
34
96
|
this._wsUrl = url || "wss://wss.block-auth.io/";
|
|
97
|
+
this._apiUrl = apiUrl;
|
|
35
98
|
this._wsConfig = {
|
|
36
99
|
transports: ["websocket"],
|
|
100
|
+
// No longer load-bearing for auth (the session cookie can't reach
|
|
101
|
+
// this host), kept as a harmless default; see the ticket-based `auth`
|
|
102
|
+
// option added in connect() below for the auth that actually matters.
|
|
103
|
+
withCredentials: true,
|
|
37
104
|
reconnection: reconnect,
|
|
38
105
|
reconnectionDelay: 100,
|
|
39
106
|
reconnectionDelayMax: 3e3,
|
|
@@ -58,6 +125,9 @@ var WsClient = class {
|
|
|
58
125
|
*/
|
|
59
126
|
async connect(url = "", waitMs = 5e3, retries = 3) {
|
|
60
127
|
const _url = url || this._wsUrl;
|
|
128
|
+
this._wsConfig.auth = (done) => {
|
|
129
|
+
fetchWsTicket(this._apiUrl).then((ticket) => done(ticket ? { ticket } : {})).catch(() => done({}));
|
|
130
|
+
};
|
|
61
131
|
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
62
132
|
if (this._ws) {
|
|
63
133
|
this._ws.disconnect();
|
|
@@ -111,6 +181,24 @@ var WsClient = class {
|
|
|
111
181
|
this.on("sdk:roles", this.onSdkRoles);
|
|
112
182
|
this.on("sdk:logs", this.onSdkLogs);
|
|
113
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Re-authenticates the CURRENT connection in place: fetches a fresh
|
|
186
|
+
* ws-ticket and sends it over the already-open socket, so the server
|
|
187
|
+
* updates its view of who this connection belongs to without a
|
|
188
|
+
* disconnect/reconnect. A full reconnect would replace `this._ws` with a
|
|
189
|
+
* brand new socket.io instance, silently dropping every listener
|
|
190
|
+
* authRuntime registered on the old one (e.g. the response side of
|
|
191
|
+
* requestSignin -> user:txns:create -> sign) - this avoids that.
|
|
192
|
+
* No-op if there is no open connection yet (connect() will pick up a
|
|
193
|
+
* ticket on its own once one exists).
|
|
194
|
+
* @returns Promise<void>
|
|
195
|
+
*/
|
|
196
|
+
async refreshAuth() {
|
|
197
|
+
if (!this.isConnected()) return;
|
|
198
|
+
const ticket = await fetchWsTicket(this._apiUrl);
|
|
199
|
+
if (!ticket) return;
|
|
200
|
+
this._ws.emit("auth:refresh", ticket);
|
|
201
|
+
}
|
|
114
202
|
/**
|
|
115
203
|
* Allow you to subscribe to an event, and
|
|
116
204
|
* verify if the event is already subscribed
|
|
@@ -519,9 +607,9 @@ var hasAnyRoleBySlug = (user, roleSlugs) => {
|
|
|
519
607
|
|
|
520
608
|
// src/protocol/BlockAuthProvider.ts
|
|
521
609
|
var BlockAuthProvider = class {
|
|
522
|
-
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io") {
|
|
610
|
+
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io", options = {}) {
|
|
523
611
|
this._rpc = new RpcClient();
|
|
524
|
-
this._wsc = new WsClient(wsUrl, true);
|
|
612
|
+
this._wsc = new WsClient(wsUrl, true, options.apiUrl);
|
|
525
613
|
this._apiKey = apiKey;
|
|
526
614
|
this._apiSecret = apiSecret;
|
|
527
615
|
}
|
|
@@ -947,6 +1035,7 @@ var resetWeb3 = () => $web3.set({ ...initial6 });
|
|
|
947
1035
|
var UI = "blockauth-ui";
|
|
948
1036
|
var SESSION = "blockauth-session";
|
|
949
1037
|
var EXTERNAL_SERVICE = "blockauth-connected";
|
|
1038
|
+
var DEVICE_SESSION_ID = "blockauth-device-session-id";
|
|
950
1039
|
var BLOCKAUTH_STORAGE_KEYS = [
|
|
951
1040
|
SESSION,
|
|
952
1041
|
UI,
|
|
@@ -961,9 +1050,7 @@ var UI_CONFIG = {
|
|
|
961
1050
|
isLocked: false
|
|
962
1051
|
};
|
|
963
1052
|
var SESSION_CONFIG = {
|
|
964
|
-
did: ""
|
|
965
|
-
token: ""
|
|
966
|
-
};
|
|
1053
|
+
did: ""};
|
|
967
1054
|
var EXTERNAL_SERVICE_CONFIG = {
|
|
968
1055
|
connected: false,
|
|
969
1056
|
service: "blockauth",
|
|
@@ -981,6 +1068,11 @@ function getStorage() {
|
|
|
981
1068
|
return null;
|
|
982
1069
|
}
|
|
983
1070
|
var BrowserBase = class {
|
|
1071
|
+
constructor() {
|
|
1072
|
+
// In-memory only - see the SECURITY NOTE above. Never persisted.
|
|
1073
|
+
this._sessionToken = "";
|
|
1074
|
+
this._externalToken = "";
|
|
1075
|
+
}
|
|
984
1076
|
/**
|
|
985
1077
|
* Wipe every SDK-owned localStorage key and reseed the defaults. Unlike a
|
|
986
1078
|
* per-key reset it also clears the `blockauth-logged-out` /
|
|
@@ -999,6 +1091,8 @@ var BrowserBase = class {
|
|
|
999
1091
|
if (keepExtensionInstalled) {
|
|
1000
1092
|
storage.setItem(UI, JSON.stringify({ ...UI_CONFIG, installed: true }));
|
|
1001
1093
|
}
|
|
1094
|
+
this._sessionToken = "";
|
|
1095
|
+
this._externalToken = "";
|
|
1002
1096
|
this.getUI();
|
|
1003
1097
|
this.getSession();
|
|
1004
1098
|
this.getExternalService();
|
|
@@ -1015,13 +1109,30 @@ var BrowserBase = class {
|
|
|
1015
1109
|
}
|
|
1016
1110
|
if (target === "session") {
|
|
1017
1111
|
storage?.removeItem(SESSION);
|
|
1112
|
+
this._sessionToken = "";
|
|
1018
1113
|
this.getSession();
|
|
1019
1114
|
}
|
|
1020
1115
|
if (target === "external") {
|
|
1021
1116
|
storage?.removeItem(EXTERNAL_SERVICE);
|
|
1117
|
+
this._externalToken = "";
|
|
1022
1118
|
this.getExternalService();
|
|
1023
1119
|
}
|
|
1024
1120
|
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1123
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1124
|
+
* server scope a signout to just this browser instead of every device
|
|
1125
|
+
* signed in with the same account.
|
|
1126
|
+
*/
|
|
1127
|
+
getDeviceSessionId() {
|
|
1128
|
+
const storage = getStorage();
|
|
1129
|
+
if (!storage) return "";
|
|
1130
|
+
const existing = storage.getItem(DEVICE_SESSION_ID);
|
|
1131
|
+
if (existing) return existing;
|
|
1132
|
+
const generated = typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
1133
|
+
storage.setItem(DEVICE_SESSION_ID, generated);
|
|
1134
|
+
return generated;
|
|
1135
|
+
}
|
|
1025
1136
|
/**
|
|
1026
1137
|
* Obtains the current UI configuration using localStorage as the
|
|
1027
1138
|
* source of truth, seeding the default if none is stored.
|
|
@@ -1063,33 +1174,55 @@ var BrowserBase = class {
|
|
|
1063
1174
|
})
|
|
1064
1175
|
);
|
|
1065
1176
|
}
|
|
1177
|
+
/**
|
|
1178
|
+
* `did` is read from localStorage; `token` is never persisted there (see
|
|
1179
|
+
* the SECURITY NOTE above) and comes from the in-memory field instead, so
|
|
1180
|
+
* it reads back as '' after a page reload even though `did` survives.
|
|
1181
|
+
*/
|
|
1066
1182
|
getSession() {
|
|
1067
1183
|
const storage = getStorage();
|
|
1068
|
-
if (!storage) return {
|
|
1184
|
+
if (!storage) return { did: "", token: this._sessionToken };
|
|
1069
1185
|
const currentConfig = storage.getItem(SESSION);
|
|
1070
|
-
if (currentConfig)
|
|
1071
|
-
|
|
1072
|
-
|
|
1186
|
+
if (currentConfig) {
|
|
1187
|
+
const { did } = JSON.parse(currentConfig);
|
|
1188
|
+
return { did: did || "", token: this._sessionToken };
|
|
1189
|
+
}
|
|
1190
|
+
storage.setItem(SESSION, JSON.stringify({ did: SESSION_CONFIG.did }));
|
|
1191
|
+
return { did: "", token: this._sessionToken };
|
|
1073
1192
|
}
|
|
1074
1193
|
/**
|
|
1075
|
-
* Obtains the configuration about the address and the service
|
|
1076
|
-
*
|
|
1194
|
+
* Obtains the configuration about the address and the service where the
|
|
1195
|
+
* user is connected. As with `getSession()`, `token` is never persisted
|
|
1196
|
+
* to localStorage - it's overlaid here from the in-memory field.
|
|
1077
1197
|
*/
|
|
1078
1198
|
getExternalService() {
|
|
1079
1199
|
const storage = getStorage();
|
|
1080
|
-
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG };
|
|
1200
|
+
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1081
1201
|
const currentConfig = storage.getItem(EXTERNAL_SERVICE);
|
|
1082
|
-
if (currentConfig)
|
|
1083
|
-
|
|
1202
|
+
if (currentConfig) {
|
|
1203
|
+
const parsed = JSON.parse(currentConfig);
|
|
1204
|
+
return { ...parsed, token: this._externalToken };
|
|
1205
|
+
}
|
|
1084
1206
|
storage.setItem(EXTERNAL_SERVICE, JSON.stringify(EXTERNAL_SERVICE_CONFIG));
|
|
1085
|
-
return
|
|
1086
|
-
storage.getItem(EXTERNAL_SERVICE)
|
|
1087
|
-
);
|
|
1207
|
+
return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1088
1208
|
}
|
|
1089
1209
|
/**
|
|
1090
|
-
* Auxiliary method to update the SESSION configuration using websocket
|
|
1210
|
+
* Auxiliary method to update the SESSION configuration using websocket
|
|
1211
|
+
* events.
|
|
1212
|
+
*
|
|
1213
|
+
* IMPORTANT: the wallet JWT (`token`) is never persisted to localStorage
|
|
1214
|
+
* anymore (see the SECURITY NOTE at the top of this file). It's kept in
|
|
1215
|
+
* the `_sessionToken`/`_externalToken` in-memory fields instead, and -
|
|
1216
|
+
* when it's a fresh/changed `blockauth` token - bridged into an httpOnly
|
|
1217
|
+
* `blockauth-session` cookie via the `/auth/session` backend endpoint.
|
|
1218
|
+
* Only the (non-sensitive) `did` is persisted, for UI/reload purposes.
|
|
1219
|
+
*
|
|
1220
|
+
* Returns a Promise once the (best-effort) session-bridge call has
|
|
1221
|
+
* settled; callers that don't need to wait for it can keep calling this
|
|
1222
|
+
* without awaiting, same as before - every state mutation below happens
|
|
1223
|
+
* synchronously, before the `await`.
|
|
1091
1224
|
*/
|
|
1092
|
-
setSession({
|
|
1225
|
+
async setSession({
|
|
1093
1226
|
installed = void 0,
|
|
1094
1227
|
isUser = void 0,
|
|
1095
1228
|
isUserActive = void 0,
|
|
@@ -1097,7 +1230,8 @@ var BrowserBase = class {
|
|
|
1097
1230
|
did = void 0,
|
|
1098
1231
|
token = void 0,
|
|
1099
1232
|
externalService = void 0,
|
|
1100
|
-
serviceName = "blockauth"
|
|
1233
|
+
serviceName = "blockauth",
|
|
1234
|
+
apiUrl = void 0
|
|
1101
1235
|
} = {}) {
|
|
1102
1236
|
const storage = getStorage();
|
|
1103
1237
|
if (!storage) return;
|
|
@@ -1114,16 +1248,20 @@ var BrowserBase = class {
|
|
|
1114
1248
|
isLocked: isLockedValue
|
|
1115
1249
|
};
|
|
1116
1250
|
storage.setItem(UI, JSON.stringify(newCfgUI));
|
|
1251
|
+
let tokenToBridge;
|
|
1117
1252
|
if (serviceName === "blockauth") {
|
|
1118
|
-
const
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1253
|
+
const didValue = did !== void 0 ? did : cfgSession.did || "";
|
|
1254
|
+
const tokenValue = token !== void 0 ? token : cfgSession.token || "";
|
|
1255
|
+
storage.setItem(SESSION, JSON.stringify({ did: didValue }));
|
|
1256
|
+
if (tokenValue && tokenValue !== this._sessionToken) {
|
|
1257
|
+
tokenToBridge = tokenValue;
|
|
1258
|
+
}
|
|
1259
|
+
this._sessionToken = tokenValue;
|
|
1123
1260
|
}
|
|
1124
1261
|
if (externalService) {
|
|
1125
1262
|
const cfgExternalService = this.getExternalService();
|
|
1126
1263
|
const isBlockauth = serviceName === "blockauth";
|
|
1264
|
+
const prevExternalToken = this._externalToken;
|
|
1127
1265
|
const connected = externalService.connected;
|
|
1128
1266
|
const service = externalService.service;
|
|
1129
1267
|
const didEs = externalService.did;
|
|
@@ -1145,20 +1283,29 @@ var BrowserBase = class {
|
|
|
1145
1283
|
account: !isBlockauth ? accountValue : "",
|
|
1146
1284
|
networkId: !isBlockauth ? networkIdValue : ""
|
|
1147
1285
|
};
|
|
1148
|
-
storage.setItem(
|
|
1286
|
+
storage.setItem(
|
|
1287
|
+
EXTERNAL_SERVICE,
|
|
1288
|
+
JSON.stringify({ ...newCfgExternalService, token: "" })
|
|
1289
|
+
);
|
|
1290
|
+
this._externalToken = newCfgExternalService.token;
|
|
1149
1291
|
if (!isBlockauth) {
|
|
1150
1292
|
storage.setItem(
|
|
1151
1293
|
SESSION,
|
|
1152
|
-
JSON.stringify({
|
|
1153
|
-
did: newCfgExternalService.did,
|
|
1154
|
-
token: newCfgExternalService.token
|
|
1155
|
-
})
|
|
1294
|
+
JSON.stringify({ did: newCfgExternalService.did, token: "" })
|
|
1156
1295
|
);
|
|
1296
|
+
this._sessionToken = newCfgExternalService.token;
|
|
1297
|
+
if (newCfgExternalService.token && newCfgExternalService.token !== prevExternalToken) {
|
|
1298
|
+
tokenToBridge = newCfgExternalService.token;
|
|
1299
|
+
}
|
|
1157
1300
|
}
|
|
1158
1301
|
}
|
|
1302
|
+
if (tokenToBridge) {
|
|
1303
|
+
await bridgeSessionCookie(tokenToBridge, apiUrl);
|
|
1304
|
+
}
|
|
1159
1305
|
}
|
|
1160
1306
|
externalServiceDisconnect() {
|
|
1161
1307
|
const storage = getStorage();
|
|
1308
|
+
this._externalToken = "";
|
|
1162
1309
|
if (!storage) return;
|
|
1163
1310
|
storage.setItem(
|
|
1164
1311
|
EXTERNAL_SERVICE,
|
|
@@ -1337,13 +1484,14 @@ function rejectTxn(args) {
|
|
|
1337
1484
|
args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
|
|
1338
1485
|
}
|
|
1339
1486
|
function acceptTxn(args) {
|
|
1340
|
-
const { wsc, token, lifetime, providerSlug, userParams } = args;
|
|
1487
|
+
const { wsc, token, lifetime, providerSlug, userParams, sessionId } = args;
|
|
1341
1488
|
wsc.emit("provider:signin", {
|
|
1342
1489
|
token,
|
|
1343
1490
|
lifetime,
|
|
1344
1491
|
state: "approved",
|
|
1345
1492
|
providerSlug,
|
|
1346
|
-
userParams: userParams ?? {}
|
|
1493
|
+
userParams: userParams ?? {},
|
|
1494
|
+
sessionId
|
|
1347
1495
|
});
|
|
1348
1496
|
}
|
|
1349
1497
|
function reportTxn(args) {
|
|
@@ -1775,12 +1923,16 @@ function startExtensionSync() {
|
|
|
1775
1923
|
}
|
|
1776
1924
|
|
|
1777
1925
|
// src/auth/connectBlockAuth.ts
|
|
1778
|
-
function connectBlockAuth() {
|
|
1926
|
+
function connectBlockAuth(apiUrl) {
|
|
1779
1927
|
setServiceName("blockauth");
|
|
1780
|
-
|
|
1928
|
+
if (typeof localStorage !== "undefined") {
|
|
1929
|
+
localStorage.removeItem("blockauth-logged-out");
|
|
1930
|
+
}
|
|
1931
|
+
void browserBase.setSession({
|
|
1781
1932
|
...browserBase.getSession(),
|
|
1782
1933
|
externalService: { service: "blockauth", connected: true },
|
|
1783
|
-
serviceName: "blockauth"
|
|
1934
|
+
serviceName: "blockauth",
|
|
1935
|
+
apiUrl
|
|
1784
1936
|
});
|
|
1785
1937
|
setIsServiceConnected(true);
|
|
1786
1938
|
setWaitingForSuccessLogInSince(0);
|
|
@@ -1789,7 +1941,7 @@ function connectBlockAuth() {
|
|
|
1789
1941
|
|
|
1790
1942
|
// src/auth/logout.ts
|
|
1791
1943
|
async function performLogout(args) {
|
|
1792
|
-
const { wsc, token, apiKey } = args;
|
|
1944
|
+
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
1793
1945
|
await new Promise((resolve) => {
|
|
1794
1946
|
let done = false;
|
|
1795
1947
|
const finish = () => {
|
|
@@ -1799,12 +1951,13 @@ async function performLogout(args) {
|
|
|
1799
1951
|
}
|
|
1800
1952
|
};
|
|
1801
1953
|
wsc.ws?.once?.("signout", finish);
|
|
1802
|
-
wsc.emit("signout", { token, apiKey });
|
|
1954
|
+
wsc.emit("signout", { token, apiKey, sessionId });
|
|
1803
1955
|
setTimeout(finish, 1500);
|
|
1804
1956
|
});
|
|
1805
1957
|
if (typeof localStorage !== "undefined") {
|
|
1806
1958
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
1807
1959
|
}
|
|
1960
|
+
await clearSessionCookie(apiUrl);
|
|
1808
1961
|
resetWs();
|
|
1809
1962
|
resetUi();
|
|
1810
1963
|
resetWallet();
|
|
@@ -2040,7 +2193,8 @@ function createBlockAuthClient(config3) {
|
|
|
2040
2193
|
const provider = new BlockAuthProvider(
|
|
2041
2194
|
config3.apiKey,
|
|
2042
2195
|
config3.apiSecret,
|
|
2043
|
-
config3.wsUrl ?? "wss://wss.block-auth.io"
|
|
2196
|
+
config3.wsUrl ?? "wss://wss.block-auth.io",
|
|
2197
|
+
{ apiUrl: config3.apiUrl }
|
|
2044
2198
|
);
|
|
2045
2199
|
const evm = createEvmWallet({
|
|
2046
2200
|
walletConnectProjectId: config3.walletConnectProjectId,
|
|
@@ -2072,6 +2226,7 @@ function createBlockAuthClient(config3) {
|
|
|
2072
2226
|
}
|
|
2073
2227
|
if (config3.google) configureGoogle(config3.google);
|
|
2074
2228
|
if (config3.microsoft) configureMicrosoft(config3.microsoft);
|
|
2229
|
+
setApiKey(config3.apiKey);
|
|
2075
2230
|
function userToken() {
|
|
2076
2231
|
const s = browserBase.getSession();
|
|
2077
2232
|
const ext = browserBase.getExternalService();
|
|
@@ -2085,7 +2240,8 @@ function createBlockAuthClient(config3) {
|
|
|
2085
2240
|
$openModals,
|
|
2086
2241
|
$user
|
|
2087
2242
|
},
|
|
2088
|
-
|
|
2243
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
2244
|
+
apiKey: config3.apiKey,
|
|
2089
2245
|
connect: (...a) => provider.connect(...a),
|
|
2090
2246
|
disconnect: () => provider.disconnect(),
|
|
2091
2247
|
signin: () => provider.signin(),
|
|
@@ -2103,18 +2259,30 @@ function createBlockAuthClient(config3) {
|
|
|
2103
2259
|
web3: { signup: web3Signup },
|
|
2104
2260
|
// BlockAuth extension connect (sets blockauth as the active service);
|
|
2105
2261
|
// the txn-signing flow is driven by the caller via requestSignin.
|
|
2106
|
-
connectBlockAuth: () => connectBlockAuth(),
|
|
2262
|
+
connectBlockAuth: () => connectBlockAuth(config3.apiUrl),
|
|
2107
2263
|
// oauth
|
|
2108
2264
|
oauth: {
|
|
2109
2265
|
...oauth_exports,
|
|
2110
2266
|
signupGoogle: async (flow = "auth-code") => {
|
|
2111
2267
|
const res = await signInWithGoogle(flow);
|
|
2112
2268
|
const signature = res.code ?? res.accessToken;
|
|
2113
|
-
await emitOauthSignup({
|
|
2269
|
+
await emitOauthSignup({
|
|
2270
|
+
wsc: provider.wsc,
|
|
2271
|
+
providerSlug: "google",
|
|
2272
|
+
signature,
|
|
2273
|
+
mode: flow,
|
|
2274
|
+
getIp
|
|
2275
|
+
});
|
|
2114
2276
|
},
|
|
2115
2277
|
signupMicrosoft: async () => {
|
|
2116
2278
|
const res = await signInWithMicrosoft();
|
|
2117
|
-
await emitOauthSignup({
|
|
2279
|
+
await emitOauthSignup({
|
|
2280
|
+
wsc: provider.wsc,
|
|
2281
|
+
providerSlug: "microsoft",
|
|
2282
|
+
signature: res.accessToken,
|
|
2283
|
+
mode: "popup",
|
|
2284
|
+
getIp
|
|
2285
|
+
});
|
|
2118
2286
|
}
|
|
2119
2287
|
},
|
|
2120
2288
|
// signin transaction + logout
|
|
@@ -2125,18 +2293,29 @@ function createBlockAuthClient(config3) {
|
|
|
2125
2293
|
service: $ui.get().serviceName ?? "blockauth",
|
|
2126
2294
|
apiKey: config3.apiKey
|
|
2127
2295
|
}),
|
|
2128
|
-
cancelTxns: () => cancelTxns({
|
|
2296
|
+
cancelTxns: () => cancelTxns({
|
|
2297
|
+
wsc: provider.wsc,
|
|
2298
|
+
token: userToken()
|
|
2299
|
+
}),
|
|
2129
2300
|
// transaction accept/reject
|
|
2130
2301
|
txns: {
|
|
2131
|
-
reject: () => rejectTxn({
|
|
2302
|
+
reject: () => rejectTxn({
|
|
2303
|
+
wsc: provider.wsc,
|
|
2304
|
+
token: userToken()
|
|
2305
|
+
}),
|
|
2132
2306
|
accept: (opts) => acceptTxn({
|
|
2133
2307
|
wsc: provider.wsc,
|
|
2134
2308
|
token: userToken(),
|
|
2135
2309
|
lifetime: opts.lifetime,
|
|
2136
2310
|
providerSlug: $ui.get().serviceName ?? "blockauth",
|
|
2137
|
-
userParams: opts.userParams
|
|
2311
|
+
userParams: opts.userParams,
|
|
2312
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2138
2313
|
}),
|
|
2139
|
-
report: (slug) => reportTxn({
|
|
2314
|
+
report: (slug) => reportTxn({
|
|
2315
|
+
wsc: provider.wsc,
|
|
2316
|
+
token: userToken(),
|
|
2317
|
+
report: slug
|
|
2318
|
+
})
|
|
2140
2319
|
},
|
|
2141
2320
|
// account restore: resolves when the backend answers; expired
|
|
2142
2321
|
// restorations surface the restorationExpired modal, other failures
|
|
@@ -2145,7 +2324,13 @@ function createBlockAuthClient(config3) {
|
|
|
2145
2324
|
// full deletion-state teardown after the user confirms the
|
|
2146
2325
|
// restoration-expired modal.
|
|
2147
2326
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2148
|
-
logout: () => performLogout({
|
|
2327
|
+
logout: () => performLogout({
|
|
2328
|
+
wsc: provider.wsc,
|
|
2329
|
+
token: userToken(),
|
|
2330
|
+
apiKey: config3.apiKey,
|
|
2331
|
+
sessionId: browserBase.getDeviceSessionId(),
|
|
2332
|
+
apiUrl: config3.apiUrl
|
|
2333
|
+
}),
|
|
2149
2334
|
// modals (stack — supports overlaying signin on top of signup)
|
|
2150
2335
|
openModal,
|
|
2151
2336
|
closeModal,
|
|
@@ -2153,6 +2338,9 @@ function createBlockAuthClient(config3) {
|
|
|
2153
2338
|
// opciones del connect inicial del WS (las consume createAuthRuntime)
|
|
2154
2339
|
wsConnectTimeout: config3.wsConnectTimeout ?? 300,
|
|
2155
2340
|
wsConnectRetries: config3.wsConnectRetries ?? 3,
|
|
2341
|
+
// Backend base url for the session/logout/ws-ticket bridge (consumed by
|
|
2342
|
+
// createAuthRuntime and by browserBase.setSession callers here above).
|
|
2343
|
+
apiUrl: config3.apiUrl,
|
|
2156
2344
|
// acceso crudo al provider si hace falta
|
|
2157
2345
|
provider
|
|
2158
2346
|
};
|
|
@@ -2166,6 +2354,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2166
2354
|
isUnsignTimeoutSet: false,
|
|
2167
2355
|
isSigned: false,
|
|
2168
2356
|
isSigning: false,
|
|
2357
|
+
isLoggingOut: false,
|
|
2169
2358
|
lastCheckedDid: null,
|
|
2170
2359
|
signinAttemptId: 0
|
|
2171
2360
|
};
|
|
@@ -2200,33 +2389,101 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2200
2389
|
await onError();
|
|
2201
2390
|
};
|
|
2202
2391
|
const _onLogout = async () => {
|
|
2392
|
+
if (hookState.isLoggingOut) return applyLocalLogout();
|
|
2393
|
+
hookState.isLoggingOut = true;
|
|
2203
2394
|
resetUser();
|
|
2395
|
+
if (typeof localStorage !== "undefined") {
|
|
2396
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2397
|
+
}
|
|
2204
2398
|
try {
|
|
2205
2399
|
if (provider && provider.wsc) {
|
|
2206
2400
|
const s = browserBase.getSession();
|
|
2207
2401
|
const ext = browserBase.getExternalService();
|
|
2208
2402
|
const _token = ext.service !== "blockauth" ? ext.token : s.token;
|
|
2209
|
-
const _apiKey = $ws.get().apiKey;
|
|
2403
|
+
const _apiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2404
|
+
const _sessionId = browserBase.getDeviceSessionId();
|
|
2210
2405
|
await new Promise((resolve) => {
|
|
2211
2406
|
const timeout = setTimeout(resolve, 3e3);
|
|
2212
2407
|
wsc().ws.once("signout", () => {
|
|
2213
2408
|
clearTimeout(timeout);
|
|
2214
2409
|
resolve();
|
|
2215
2410
|
});
|
|
2216
|
-
wsc().emit("signout", {
|
|
2411
|
+
wsc().emit("signout", {
|
|
2412
|
+
token: _token,
|
|
2413
|
+
apiKey: _apiKey,
|
|
2414
|
+
sessionId: _sessionId
|
|
2415
|
+
});
|
|
2217
2416
|
});
|
|
2218
2417
|
}
|
|
2219
2418
|
} catch {
|
|
2220
2419
|
}
|
|
2221
|
-
|
|
2222
|
-
|
|
2420
|
+
await applyLocalLogout();
|
|
2421
|
+
};
|
|
2422
|
+
let logoutInFlight = null;
|
|
2423
|
+
const applyLocalLogout = async () => {
|
|
2424
|
+
if (logoutInFlight) return logoutInFlight;
|
|
2425
|
+
logoutInFlight = (async () => {
|
|
2426
|
+
hookState.isLoggingOut = true;
|
|
2427
|
+
if (unsignTimeout) {
|
|
2428
|
+
clearTimeout(unsignTimeout);
|
|
2429
|
+
unsignTimeout = null;
|
|
2430
|
+
}
|
|
2431
|
+
hookState.isUnsignTimeoutSet = false;
|
|
2432
|
+
if (signinTimeout) {
|
|
2433
|
+
clearTimeout(signinTimeout);
|
|
2434
|
+
signinTimeout = null;
|
|
2435
|
+
}
|
|
2436
|
+
if (getUserTxnsInterval) {
|
|
2437
|
+
clearInterval(getUserTxnsInterval);
|
|
2438
|
+
getUserTxnsInterval = null;
|
|
2439
|
+
}
|
|
2440
|
+
hookState.isSigning = false;
|
|
2441
|
+
hookState.isSigned = false;
|
|
2442
|
+
setIsSigned(false);
|
|
2443
|
+
setIsSigning(false);
|
|
2444
|
+
setIsSigningLoader(false);
|
|
2445
|
+
setIsAcceptingTxn(false);
|
|
2446
|
+
setIsSignedFirstCheck(true);
|
|
2447
|
+
resetUser();
|
|
2448
|
+
if (typeof localStorage !== "undefined") {
|
|
2449
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2450
|
+
}
|
|
2451
|
+
await clearSessionCookie(client.apiUrl);
|
|
2452
|
+
const serviceName = browserBase.getExternalService().service;
|
|
2453
|
+
setIsServiceConnected(false);
|
|
2454
|
+
setWaitingForSuccessLogInSince(0);
|
|
2455
|
+
setServiceIcon(imgUrlByService(serviceName));
|
|
2456
|
+
setServiceName(serviceName);
|
|
2457
|
+
try {
|
|
2458
|
+
await onLogout();
|
|
2459
|
+
} finally {
|
|
2460
|
+
hookState.isLoggingOut = false;
|
|
2461
|
+
}
|
|
2462
|
+
})().finally(() => {
|
|
2463
|
+
logoutInFlight = null;
|
|
2464
|
+
});
|
|
2465
|
+
return logoutInFlight;
|
|
2466
|
+
};
|
|
2467
|
+
const onStorageLogout = (event) => {
|
|
2468
|
+
if (event.key !== "blockauth-logged-out" || event.newValue !== "true") {
|
|
2469
|
+
return;
|
|
2223
2470
|
}
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2471
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2472
|
+
void applyLocalLogout().catch((err) => {
|
|
2473
|
+
console.error("[sdk] applyLocalLogout failed (storage event):", err);
|
|
2474
|
+
});
|
|
2475
|
+
};
|
|
2476
|
+
const onExtensionSessionBridged = (event) => {
|
|
2477
|
+
const did = event.detail?.did;
|
|
2478
|
+
if (did) {
|
|
2479
|
+
void browserBase.setSession({
|
|
2480
|
+
did,
|
|
2481
|
+
serviceName: "blockauth",
|
|
2482
|
+
apiUrl: client.apiUrl
|
|
2483
|
+
});
|
|
2484
|
+
setDid(did);
|
|
2485
|
+
}
|
|
2486
|
+
void wsc().refreshAuth();
|
|
2230
2487
|
};
|
|
2231
2488
|
const deletion = createDeletionService({ provider });
|
|
2232
2489
|
const handleUserDeleted = (forAddress = null) => {
|
|
@@ -2248,10 +2505,11 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2248
2505
|
if (service === "blockauth" || prevDid && prevDid.toLowerCase() !== String(did).toLowerCase()) {
|
|
2249
2506
|
browserBase.clearBrowserState({ keepExtensionInstalled: keepInstalled });
|
|
2250
2507
|
}
|
|
2251
|
-
browserBase.setSession({
|
|
2508
|
+
void browserBase.setSession({
|
|
2252
2509
|
...result,
|
|
2253
|
-
serviceName: service
|
|
2254
|
-
|
|
2510
|
+
serviceName: service,
|
|
2511
|
+
apiUrl: client.apiUrl
|
|
2512
|
+
}).then(() => wsc().refreshAuth());
|
|
2255
2513
|
setDid(did);
|
|
2256
2514
|
setToken(token);
|
|
2257
2515
|
setServiceIcon(imgUrlByService(service));
|
|
@@ -2318,10 +2576,6 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2318
2576
|
const signinUnsigned = async () => {
|
|
2319
2577
|
setIsSignedFirstCheck(true);
|
|
2320
2578
|
if (!hookState.isSigned) {
|
|
2321
|
-
const ext = browserBase.getExternalService();
|
|
2322
|
-
if (ext.connected && typeof localStorage !== "undefined") {
|
|
2323
|
-
localStorage.setItem("blockauth-logged-out", "true");
|
|
2324
|
-
}
|
|
2325
2579
|
await _onError();
|
|
2326
2580
|
}
|
|
2327
2581
|
};
|
|
@@ -2503,6 +2757,21 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2503
2757
|
const onSdkSignRefresh = () => {
|
|
2504
2758
|
void isUserSignedIn();
|
|
2505
2759
|
};
|
|
2760
|
+
const onSdkSessionInvalidated = (data) => {
|
|
2761
|
+
const apiKey = data?.apiKey ?? data?.message?.apiKey;
|
|
2762
|
+
const clientApiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2763
|
+
if (!apiKey || !clientApiKey || apiKey !== clientApiKey) return;
|
|
2764
|
+
const sessionId = data?.sessionId ?? data?.message?.sessionId;
|
|
2765
|
+
if (sessionId != null && String(sessionId) !== String(browserBase.getDeviceSessionId()))
|
|
2766
|
+
return;
|
|
2767
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2768
|
+
void applyLocalLogout().catch((err) => {
|
|
2769
|
+
console.error(
|
|
2770
|
+
"[sdk] applyLocalLogout failed (session invalidated):",
|
|
2771
|
+
err
|
|
2772
|
+
);
|
|
2773
|
+
});
|
|
2774
|
+
};
|
|
2506
2775
|
const onSdkUserRolesChanged = async (data) => {
|
|
2507
2776
|
const { success, message } = data || {};
|
|
2508
2777
|
if (!success || !message?.address) return;
|
|
@@ -2513,7 +2782,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2513
2782
|
if (!activeDid || activeDid !== changedAddress) return;
|
|
2514
2783
|
await fetchUserProfile(provider, activeDid);
|
|
2515
2784
|
};
|
|
2516
|
-
const onSdkRestore = (data) => {
|
|
2785
|
+
const onSdkRestore = async (data) => {
|
|
2517
2786
|
if (data.success) {
|
|
2518
2787
|
deletion.resetHandlingState();
|
|
2519
2788
|
setIsOnDeletion(false);
|
|
@@ -2524,15 +2793,17 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2524
2793
|
browserBase.clearBrowserState({
|
|
2525
2794
|
keepExtensionInstalled: keepInstalled
|
|
2526
2795
|
});
|
|
2527
|
-
browserBase.setSession({
|
|
2796
|
+
await browserBase.setSession({
|
|
2528
2797
|
did: session.did,
|
|
2529
2798
|
token: session.token,
|
|
2530
|
-
serviceName: "blockauth"
|
|
2799
|
+
serviceName: "blockauth",
|
|
2800
|
+
apiUrl: client.apiUrl
|
|
2531
2801
|
});
|
|
2532
2802
|
if (session.serviceName && session.serviceName !== "blockauth") {
|
|
2533
|
-
browserBase.setSession({
|
|
2803
|
+
await browserBase.setSession({
|
|
2534
2804
|
...session,
|
|
2535
|
-
serviceName: session.serviceName
|
|
2805
|
+
serviceName: session.serviceName,
|
|
2806
|
+
apiUrl: client.apiUrl
|
|
2536
2807
|
});
|
|
2537
2808
|
}
|
|
2538
2809
|
}
|
|
@@ -2558,8 +2829,9 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2558
2829
|
}
|
|
2559
2830
|
setDid(_did);
|
|
2560
2831
|
setToken(_token);
|
|
2561
|
-
setApiKey($ws.get().apiKey);
|
|
2562
|
-
|
|
2832
|
+
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
2833
|
+
const isLoggedOut = typeof localStorage !== "undefined" && localStorage.getItem("blockauth-logged-out") === "true";
|
|
2834
|
+
setIsServiceConnected(isLoggedOut ? false : externalService.connected);
|
|
2563
2835
|
setServiceName(externalService.service);
|
|
2564
2836
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
2565
2837
|
await provider.connect(
|
|
@@ -2597,8 +2869,18 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2597
2869
|
wsc().on("signin:unsigned", onSigninUnsigned);
|
|
2598
2870
|
wsc().on("signin:onDeletion", onSigninOnDeletion);
|
|
2599
2871
|
wsc().on("sdk:sign:refresh", onSdkSignRefresh);
|
|
2872
|
+
wsc().on("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2600
2873
|
wsc().on("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2601
2874
|
wsc().on("sdk:restore", onSdkRestore);
|
|
2875
|
+
if (typeof window !== "undefined") {
|
|
2876
|
+
window.addEventListener("storage", onStorageLogout);
|
|
2877
|
+
}
|
|
2878
|
+
if (typeof document !== "undefined") {
|
|
2879
|
+
document.addEventListener(
|
|
2880
|
+
"blockauth:session-bridged",
|
|
2881
|
+
onExtensionSessionBridged
|
|
2882
|
+
);
|
|
2883
|
+
}
|
|
2602
2884
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
2603
2885
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
2604
2886
|
setReady(true);
|
|
@@ -2634,9 +2916,19 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2634
2916
|
w.off("signin:unsigned", onSigninUnsigned);
|
|
2635
2917
|
w.off("signin:onDeletion", onSigninOnDeletion);
|
|
2636
2918
|
w.off("sdk:sign:refresh", onSdkSignRefresh);
|
|
2919
|
+
w.off("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2637
2920
|
w.off("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2638
2921
|
w.off("sdk:restore", onSdkRestore);
|
|
2639
2922
|
}
|
|
2923
|
+
if (typeof window !== "undefined") {
|
|
2924
|
+
window.removeEventListener("storage", onStorageLogout);
|
|
2925
|
+
}
|
|
2926
|
+
if (typeof document !== "undefined") {
|
|
2927
|
+
document.removeEventListener(
|
|
2928
|
+
"blockauth:session-bridged",
|
|
2929
|
+
onExtensionSessionBridged
|
|
2930
|
+
);
|
|
2931
|
+
}
|
|
2640
2932
|
provider.disconnect();
|
|
2641
2933
|
};
|
|
2642
2934
|
const logout = async () => {
|