@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.js
CHANGED
|
@@ -13,8 +13,70 @@ var __export = (target, all) => {
|
|
|
13
13
|
for (var name in all)
|
|
14
14
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
15
|
};
|
|
16
|
+
|
|
17
|
+
// src/utils/apiBridge.ts
|
|
18
|
+
var DEFAULT_API_URL = "https://api.block-auth.io";
|
|
19
|
+
function buildApiUrl(path, apiUrl) {
|
|
20
|
+
const base = (apiUrl || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
21
|
+
return `${base}${path}`;
|
|
22
|
+
}
|
|
23
|
+
async function bridgeSessionCookie(token, apiUrl) {
|
|
24
|
+
if (!token || typeof fetch === "undefined") return false;
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(buildApiUrl("/auth/session", apiUrl), {
|
|
27
|
+
method: "POST",
|
|
28
|
+
credentials: "include",
|
|
29
|
+
headers: { "Content-Type": "application/json" },
|
|
30
|
+
body: JSON.stringify({ token })
|
|
31
|
+
});
|
|
32
|
+
return response.ok;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn("[block-auth] failed to bridge session cookie", error);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function clearSessionCookie(apiUrl) {
|
|
39
|
+
if (typeof fetch === "undefined") return false;
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(buildApiUrl("/auth/logout", apiUrl), {
|
|
42
|
+
method: "POST",
|
|
43
|
+
credentials: "include"
|
|
44
|
+
});
|
|
45
|
+
return response.ok;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.warn("[block-auth] failed to clear session cookie", error);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function fetchWsTicket(apiUrl) {
|
|
52
|
+
if (typeof fetch === "undefined") return null;
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(buildApiUrl("/auth/ws-ticket", apiUrl), {
|
|
55
|
+
method: "GET",
|
|
56
|
+
credentials: "include"
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) return null;
|
|
59
|
+
const data = await response.json();
|
|
60
|
+
return data?.ticket || null;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.warn("[block-auth] failed to fetch ws ticket", error);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/protocol/WsClient.ts
|
|
16
68
|
var WsClient = class {
|
|
17
|
-
|
|
69
|
+
/**
|
|
70
|
+
* @param url - the wss:// endpoint to connect to.
|
|
71
|
+
* @param reconnect - allow socket.io to auto-reconnect.
|
|
72
|
+
* @param apiUrl - backend base url used to fetch the short-lived WS
|
|
73
|
+
* connect ticket (GET /auth/ws-ticket) right before opening the socket.
|
|
74
|
+
* Defaults to https://api.block-auth.io. The cookie set by
|
|
75
|
+
* POST /auth/session is host-only to the API origin, so it can't reach
|
|
76
|
+
* this (different-host) WS endpoint directly — the ticket is what
|
|
77
|
+
* actually authenticates the connection now.
|
|
78
|
+
*/
|
|
79
|
+
constructor(url = "", reconnect = false, apiUrl) {
|
|
18
80
|
this._uiConfig = {};
|
|
19
81
|
this._accessToken = "";
|
|
20
82
|
this._refreshToken = "";
|
|
@@ -24,8 +86,13 @@ var WsClient = class {
|
|
|
24
86
|
this._logs = [];
|
|
25
87
|
this._kycIssuerByAddress = {};
|
|
26
88
|
this._wsUrl = url || "wss://wss.block-auth.io/";
|
|
89
|
+
this._apiUrl = apiUrl;
|
|
27
90
|
this._wsConfig = {
|
|
28
91
|
transports: ["websocket"],
|
|
92
|
+
// No longer load-bearing for auth (the session cookie can't reach
|
|
93
|
+
// this host), kept as a harmless default; see the ticket-based `auth`
|
|
94
|
+
// option added in connect() below for the auth that actually matters.
|
|
95
|
+
withCredentials: true,
|
|
29
96
|
reconnection: reconnect,
|
|
30
97
|
reconnectionDelay: 100,
|
|
31
98
|
reconnectionDelayMax: 3e3,
|
|
@@ -50,6 +117,9 @@ var WsClient = class {
|
|
|
50
117
|
*/
|
|
51
118
|
async connect(url = "", waitMs = 5e3, retries = 3) {
|
|
52
119
|
const _url = url || this._wsUrl;
|
|
120
|
+
this._wsConfig.auth = (done) => {
|
|
121
|
+
fetchWsTicket(this._apiUrl).then((ticket) => done(ticket ? { ticket } : {})).catch(() => done({}));
|
|
122
|
+
};
|
|
53
123
|
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
54
124
|
if (this._ws) {
|
|
55
125
|
this._ws.disconnect();
|
|
@@ -103,6 +173,24 @@ var WsClient = class {
|
|
|
103
173
|
this.on("sdk:roles", this.onSdkRoles);
|
|
104
174
|
this.on("sdk:logs", this.onSdkLogs);
|
|
105
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Re-authenticates the CURRENT connection in place: fetches a fresh
|
|
178
|
+
* ws-ticket and sends it over the already-open socket, so the server
|
|
179
|
+
* updates its view of who this connection belongs to without a
|
|
180
|
+
* disconnect/reconnect. A full reconnect would replace `this._ws` with a
|
|
181
|
+
* brand new socket.io instance, silently dropping every listener
|
|
182
|
+
* authRuntime registered on the old one (e.g. the response side of
|
|
183
|
+
* requestSignin -> user:txns:create -> sign) - this avoids that.
|
|
184
|
+
* No-op if there is no open connection yet (connect() will pick up a
|
|
185
|
+
* ticket on its own once one exists).
|
|
186
|
+
* @returns Promise<void>
|
|
187
|
+
*/
|
|
188
|
+
async refreshAuth() {
|
|
189
|
+
if (!this.isConnected()) return;
|
|
190
|
+
const ticket = await fetchWsTicket(this._apiUrl);
|
|
191
|
+
if (!ticket) return;
|
|
192
|
+
this._ws.emit("auth:refresh", ticket);
|
|
193
|
+
}
|
|
106
194
|
/**
|
|
107
195
|
* Allow you to subscribe to an event, and
|
|
108
196
|
* verify if the event is already subscribed
|
|
@@ -511,9 +599,9 @@ var hasAnyRoleBySlug = (user, roleSlugs) => {
|
|
|
511
599
|
|
|
512
600
|
// src/protocol/BlockAuthProvider.ts
|
|
513
601
|
var BlockAuthProvider = class {
|
|
514
|
-
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io") {
|
|
602
|
+
constructor(apiKey = "", apiSecret = "", wsUrl = "wss://wss.block-auth.io", options = {}) {
|
|
515
603
|
this._rpc = new RpcClient();
|
|
516
|
-
this._wsc = new WsClient(wsUrl, true);
|
|
604
|
+
this._wsc = new WsClient(wsUrl, true, options.apiUrl);
|
|
517
605
|
this._apiKey = apiKey;
|
|
518
606
|
this._apiSecret = apiSecret;
|
|
519
607
|
}
|
|
@@ -939,6 +1027,7 @@ var resetWeb3 = () => $web3.set({ ...initial6 });
|
|
|
939
1027
|
var UI = "blockauth-ui";
|
|
940
1028
|
var SESSION = "blockauth-session";
|
|
941
1029
|
var EXTERNAL_SERVICE = "blockauth-connected";
|
|
1030
|
+
var DEVICE_SESSION_ID = "blockauth-device-session-id";
|
|
942
1031
|
var BLOCKAUTH_STORAGE_KEYS = [
|
|
943
1032
|
SESSION,
|
|
944
1033
|
UI,
|
|
@@ -953,9 +1042,7 @@ var UI_CONFIG = {
|
|
|
953
1042
|
isLocked: false
|
|
954
1043
|
};
|
|
955
1044
|
var SESSION_CONFIG = {
|
|
956
|
-
did: ""
|
|
957
|
-
token: ""
|
|
958
|
-
};
|
|
1045
|
+
did: ""};
|
|
959
1046
|
var EXTERNAL_SERVICE_CONFIG = {
|
|
960
1047
|
connected: false,
|
|
961
1048
|
service: "blockauth",
|
|
@@ -973,6 +1060,11 @@ function getStorage() {
|
|
|
973
1060
|
return null;
|
|
974
1061
|
}
|
|
975
1062
|
var BrowserBase = class {
|
|
1063
|
+
constructor() {
|
|
1064
|
+
// In-memory only - see the SECURITY NOTE above. Never persisted.
|
|
1065
|
+
this._sessionToken = "";
|
|
1066
|
+
this._externalToken = "";
|
|
1067
|
+
}
|
|
976
1068
|
/**
|
|
977
1069
|
* Wipe every SDK-owned localStorage key and reseed the defaults. Unlike a
|
|
978
1070
|
* per-key reset it also clears the `blockauth-logged-out` /
|
|
@@ -991,6 +1083,8 @@ var BrowserBase = class {
|
|
|
991
1083
|
if (keepExtensionInstalled) {
|
|
992
1084
|
storage.setItem(UI, JSON.stringify({ ...UI_CONFIG, installed: true }));
|
|
993
1085
|
}
|
|
1086
|
+
this._sessionToken = "";
|
|
1087
|
+
this._externalToken = "";
|
|
994
1088
|
this.getUI();
|
|
995
1089
|
this.getSession();
|
|
996
1090
|
this.getExternalService();
|
|
@@ -1007,13 +1101,30 @@ var BrowserBase = class {
|
|
|
1007
1101
|
}
|
|
1008
1102
|
if (target === "session") {
|
|
1009
1103
|
storage?.removeItem(SESSION);
|
|
1104
|
+
this._sessionToken = "";
|
|
1010
1105
|
this.getSession();
|
|
1011
1106
|
}
|
|
1012
1107
|
if (target === "external") {
|
|
1013
1108
|
storage?.removeItem(EXTERNAL_SERVICE);
|
|
1109
|
+
this._externalToken = "";
|
|
1014
1110
|
this.getExternalService();
|
|
1015
1111
|
}
|
|
1016
1112
|
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1115
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1116
|
+
* server scope a signout to just this browser instead of every device
|
|
1117
|
+
* signed in with the same account.
|
|
1118
|
+
*/
|
|
1119
|
+
getDeviceSessionId() {
|
|
1120
|
+
const storage = getStorage();
|
|
1121
|
+
if (!storage) return "";
|
|
1122
|
+
const existing = storage.getItem(DEVICE_SESSION_ID);
|
|
1123
|
+
if (existing) return existing;
|
|
1124
|
+
const generated = typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
1125
|
+
storage.setItem(DEVICE_SESSION_ID, generated);
|
|
1126
|
+
return generated;
|
|
1127
|
+
}
|
|
1017
1128
|
/**
|
|
1018
1129
|
* Obtains the current UI configuration using localStorage as the
|
|
1019
1130
|
* source of truth, seeding the default if none is stored.
|
|
@@ -1055,33 +1166,55 @@ var BrowserBase = class {
|
|
|
1055
1166
|
})
|
|
1056
1167
|
);
|
|
1057
1168
|
}
|
|
1169
|
+
/**
|
|
1170
|
+
* `did` is read from localStorage; `token` is never persisted there (see
|
|
1171
|
+
* the SECURITY NOTE above) and comes from the in-memory field instead, so
|
|
1172
|
+
* it reads back as '' after a page reload even though `did` survives.
|
|
1173
|
+
*/
|
|
1058
1174
|
getSession() {
|
|
1059
1175
|
const storage = getStorage();
|
|
1060
|
-
if (!storage) return {
|
|
1176
|
+
if (!storage) return { did: "", token: this._sessionToken };
|
|
1061
1177
|
const currentConfig = storage.getItem(SESSION);
|
|
1062
|
-
if (currentConfig)
|
|
1063
|
-
|
|
1064
|
-
|
|
1178
|
+
if (currentConfig) {
|
|
1179
|
+
const { did } = JSON.parse(currentConfig);
|
|
1180
|
+
return { did: did || "", token: this._sessionToken };
|
|
1181
|
+
}
|
|
1182
|
+
storage.setItem(SESSION, JSON.stringify({ did: SESSION_CONFIG.did }));
|
|
1183
|
+
return { did: "", token: this._sessionToken };
|
|
1065
1184
|
}
|
|
1066
1185
|
/**
|
|
1067
|
-
* Obtains the configuration about the address and the service
|
|
1068
|
-
*
|
|
1186
|
+
* Obtains the configuration about the address and the service where the
|
|
1187
|
+
* user is connected. As with `getSession()`, `token` is never persisted
|
|
1188
|
+
* to localStorage - it's overlaid here from the in-memory field.
|
|
1069
1189
|
*/
|
|
1070
1190
|
getExternalService() {
|
|
1071
1191
|
const storage = getStorage();
|
|
1072
|
-
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG };
|
|
1192
|
+
if (!storage) return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1073
1193
|
const currentConfig = storage.getItem(EXTERNAL_SERVICE);
|
|
1074
|
-
if (currentConfig)
|
|
1075
|
-
|
|
1194
|
+
if (currentConfig) {
|
|
1195
|
+
const parsed = JSON.parse(currentConfig);
|
|
1196
|
+
return { ...parsed, token: this._externalToken };
|
|
1197
|
+
}
|
|
1076
1198
|
storage.setItem(EXTERNAL_SERVICE, JSON.stringify(EXTERNAL_SERVICE_CONFIG));
|
|
1077
|
-
return
|
|
1078
|
-
storage.getItem(EXTERNAL_SERVICE)
|
|
1079
|
-
);
|
|
1199
|
+
return { ...EXTERNAL_SERVICE_CONFIG, token: this._externalToken };
|
|
1080
1200
|
}
|
|
1081
1201
|
/**
|
|
1082
|
-
* Auxiliary method to update the SESSION configuration using websocket
|
|
1202
|
+
* Auxiliary method to update the SESSION configuration using websocket
|
|
1203
|
+
* events.
|
|
1204
|
+
*
|
|
1205
|
+
* IMPORTANT: the wallet JWT (`token`) is never persisted to localStorage
|
|
1206
|
+
* anymore (see the SECURITY NOTE at the top of this file). It's kept in
|
|
1207
|
+
* the `_sessionToken`/`_externalToken` in-memory fields instead, and -
|
|
1208
|
+
* when it's a fresh/changed `blockauth` token - bridged into an httpOnly
|
|
1209
|
+
* `blockauth-session` cookie via the `/auth/session` backend endpoint.
|
|
1210
|
+
* Only the (non-sensitive) `did` is persisted, for UI/reload purposes.
|
|
1211
|
+
*
|
|
1212
|
+
* Returns a Promise once the (best-effort) session-bridge call has
|
|
1213
|
+
* settled; callers that don't need to wait for it can keep calling this
|
|
1214
|
+
* without awaiting, same as before - every state mutation below happens
|
|
1215
|
+
* synchronously, before the `await`.
|
|
1083
1216
|
*/
|
|
1084
|
-
setSession({
|
|
1217
|
+
async setSession({
|
|
1085
1218
|
installed = void 0,
|
|
1086
1219
|
isUser = void 0,
|
|
1087
1220
|
isUserActive = void 0,
|
|
@@ -1089,7 +1222,8 @@ var BrowserBase = class {
|
|
|
1089
1222
|
did = void 0,
|
|
1090
1223
|
token = void 0,
|
|
1091
1224
|
externalService = void 0,
|
|
1092
|
-
serviceName = "blockauth"
|
|
1225
|
+
serviceName = "blockauth",
|
|
1226
|
+
apiUrl = void 0
|
|
1093
1227
|
} = {}) {
|
|
1094
1228
|
const storage = getStorage();
|
|
1095
1229
|
if (!storage) return;
|
|
@@ -1106,16 +1240,20 @@ var BrowserBase = class {
|
|
|
1106
1240
|
isLocked: isLockedValue
|
|
1107
1241
|
};
|
|
1108
1242
|
storage.setItem(UI, JSON.stringify(newCfgUI));
|
|
1243
|
+
let tokenToBridge;
|
|
1109
1244
|
if (serviceName === "blockauth") {
|
|
1110
|
-
const
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1245
|
+
const didValue = did !== void 0 ? did : cfgSession.did || "";
|
|
1246
|
+
const tokenValue = token !== void 0 ? token : cfgSession.token || "";
|
|
1247
|
+
storage.setItem(SESSION, JSON.stringify({ did: didValue }));
|
|
1248
|
+
if (tokenValue && tokenValue !== this._sessionToken) {
|
|
1249
|
+
tokenToBridge = tokenValue;
|
|
1250
|
+
}
|
|
1251
|
+
this._sessionToken = tokenValue;
|
|
1115
1252
|
}
|
|
1116
1253
|
if (externalService) {
|
|
1117
1254
|
const cfgExternalService = this.getExternalService();
|
|
1118
1255
|
const isBlockauth = serviceName === "blockauth";
|
|
1256
|
+
const prevExternalToken = this._externalToken;
|
|
1119
1257
|
const connected = externalService.connected;
|
|
1120
1258
|
const service = externalService.service;
|
|
1121
1259
|
const didEs = externalService.did;
|
|
@@ -1137,20 +1275,29 @@ var BrowserBase = class {
|
|
|
1137
1275
|
account: !isBlockauth ? accountValue : "",
|
|
1138
1276
|
networkId: !isBlockauth ? networkIdValue : ""
|
|
1139
1277
|
};
|
|
1140
|
-
storage.setItem(
|
|
1278
|
+
storage.setItem(
|
|
1279
|
+
EXTERNAL_SERVICE,
|
|
1280
|
+
JSON.stringify({ ...newCfgExternalService, token: "" })
|
|
1281
|
+
);
|
|
1282
|
+
this._externalToken = newCfgExternalService.token;
|
|
1141
1283
|
if (!isBlockauth) {
|
|
1142
1284
|
storage.setItem(
|
|
1143
1285
|
SESSION,
|
|
1144
|
-
JSON.stringify({
|
|
1145
|
-
did: newCfgExternalService.did,
|
|
1146
|
-
token: newCfgExternalService.token
|
|
1147
|
-
})
|
|
1286
|
+
JSON.stringify({ did: newCfgExternalService.did, token: "" })
|
|
1148
1287
|
);
|
|
1288
|
+
this._sessionToken = newCfgExternalService.token;
|
|
1289
|
+
if (newCfgExternalService.token && newCfgExternalService.token !== prevExternalToken) {
|
|
1290
|
+
tokenToBridge = newCfgExternalService.token;
|
|
1291
|
+
}
|
|
1149
1292
|
}
|
|
1150
1293
|
}
|
|
1294
|
+
if (tokenToBridge) {
|
|
1295
|
+
await bridgeSessionCookie(tokenToBridge, apiUrl);
|
|
1296
|
+
}
|
|
1151
1297
|
}
|
|
1152
1298
|
externalServiceDisconnect() {
|
|
1153
1299
|
const storage = getStorage();
|
|
1300
|
+
this._externalToken = "";
|
|
1154
1301
|
if (!storage) return;
|
|
1155
1302
|
storage.setItem(
|
|
1156
1303
|
EXTERNAL_SERVICE,
|
|
@@ -1329,13 +1476,14 @@ function rejectTxn(args) {
|
|
|
1329
1476
|
args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
|
|
1330
1477
|
}
|
|
1331
1478
|
function acceptTxn(args) {
|
|
1332
|
-
const { wsc, token, lifetime, providerSlug, userParams } = args;
|
|
1479
|
+
const { wsc, token, lifetime, providerSlug, userParams, sessionId } = args;
|
|
1333
1480
|
wsc.emit("provider:signin", {
|
|
1334
1481
|
token,
|
|
1335
1482
|
lifetime,
|
|
1336
1483
|
state: "approved",
|
|
1337
1484
|
providerSlug,
|
|
1338
|
-
userParams: userParams ?? {}
|
|
1485
|
+
userParams: userParams ?? {},
|
|
1486
|
+
sessionId
|
|
1339
1487
|
});
|
|
1340
1488
|
}
|
|
1341
1489
|
function reportTxn(args) {
|
|
@@ -1767,12 +1915,16 @@ function startExtensionSync() {
|
|
|
1767
1915
|
}
|
|
1768
1916
|
|
|
1769
1917
|
// src/auth/connectBlockAuth.ts
|
|
1770
|
-
function connectBlockAuth() {
|
|
1918
|
+
function connectBlockAuth(apiUrl) {
|
|
1771
1919
|
setServiceName("blockauth");
|
|
1772
|
-
|
|
1920
|
+
if (typeof localStorage !== "undefined") {
|
|
1921
|
+
localStorage.removeItem("blockauth-logged-out");
|
|
1922
|
+
}
|
|
1923
|
+
void browserBase.setSession({
|
|
1773
1924
|
...browserBase.getSession(),
|
|
1774
1925
|
externalService: { service: "blockauth", connected: true },
|
|
1775
|
-
serviceName: "blockauth"
|
|
1926
|
+
serviceName: "blockauth",
|
|
1927
|
+
apiUrl
|
|
1776
1928
|
});
|
|
1777
1929
|
setIsServiceConnected(true);
|
|
1778
1930
|
setWaitingForSuccessLogInSince(0);
|
|
@@ -1781,7 +1933,7 @@ function connectBlockAuth() {
|
|
|
1781
1933
|
|
|
1782
1934
|
// src/auth/logout.ts
|
|
1783
1935
|
async function performLogout(args) {
|
|
1784
|
-
const { wsc, token, apiKey } = args;
|
|
1936
|
+
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
1785
1937
|
await new Promise((resolve) => {
|
|
1786
1938
|
let done = false;
|
|
1787
1939
|
const finish = () => {
|
|
@@ -1791,12 +1943,13 @@ async function performLogout(args) {
|
|
|
1791
1943
|
}
|
|
1792
1944
|
};
|
|
1793
1945
|
wsc.ws?.once?.("signout", finish);
|
|
1794
|
-
wsc.emit("signout", { token, apiKey });
|
|
1946
|
+
wsc.emit("signout", { token, apiKey, sessionId });
|
|
1795
1947
|
setTimeout(finish, 1500);
|
|
1796
1948
|
});
|
|
1797
1949
|
if (typeof localStorage !== "undefined") {
|
|
1798
1950
|
localStorage.setItem("blockauth-logged-out", "true");
|
|
1799
1951
|
}
|
|
1952
|
+
await clearSessionCookie(apiUrl);
|
|
1800
1953
|
resetWs();
|
|
1801
1954
|
resetUi();
|
|
1802
1955
|
resetWallet();
|
|
@@ -2032,7 +2185,8 @@ function createBlockAuthClient(config3) {
|
|
|
2032
2185
|
const provider = new BlockAuthProvider(
|
|
2033
2186
|
config3.apiKey,
|
|
2034
2187
|
config3.apiSecret,
|
|
2035
|
-
config3.wsUrl ?? "wss://wss.block-auth.io"
|
|
2188
|
+
config3.wsUrl ?? "wss://wss.block-auth.io",
|
|
2189
|
+
{ apiUrl: config3.apiUrl }
|
|
2036
2190
|
);
|
|
2037
2191
|
const evm = createEvmWallet({
|
|
2038
2192
|
walletConnectProjectId: config3.walletConnectProjectId,
|
|
@@ -2064,6 +2218,7 @@ function createBlockAuthClient(config3) {
|
|
|
2064
2218
|
}
|
|
2065
2219
|
if (config3.google) configureGoogle(config3.google);
|
|
2066
2220
|
if (config3.microsoft) configureMicrosoft(config3.microsoft);
|
|
2221
|
+
setApiKey(config3.apiKey);
|
|
2067
2222
|
function userToken() {
|
|
2068
2223
|
const s = browserBase.getSession();
|
|
2069
2224
|
const ext = browserBase.getExternalService();
|
|
@@ -2077,7 +2232,8 @@ function createBlockAuthClient(config3) {
|
|
|
2077
2232
|
$openModals,
|
|
2078
2233
|
$user
|
|
2079
2234
|
},
|
|
2080
|
-
|
|
2235
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
2236
|
+
apiKey: config3.apiKey,
|
|
2081
2237
|
connect: (...a) => provider.connect(...a),
|
|
2082
2238
|
disconnect: () => provider.disconnect(),
|
|
2083
2239
|
signin: () => provider.signin(),
|
|
@@ -2095,18 +2251,30 @@ function createBlockAuthClient(config3) {
|
|
|
2095
2251
|
web3: { signup: web3Signup },
|
|
2096
2252
|
// BlockAuth extension connect (sets blockauth as the active service);
|
|
2097
2253
|
// the txn-signing flow is driven by the caller via requestSignin.
|
|
2098
|
-
connectBlockAuth: () => connectBlockAuth(),
|
|
2254
|
+
connectBlockAuth: () => connectBlockAuth(config3.apiUrl),
|
|
2099
2255
|
// oauth
|
|
2100
2256
|
oauth: {
|
|
2101
2257
|
...oauth_exports,
|
|
2102
2258
|
signupGoogle: async (flow = "auth-code") => {
|
|
2103
2259
|
const res = await signInWithGoogle(flow);
|
|
2104
2260
|
const signature = res.code ?? res.accessToken;
|
|
2105
|
-
await emitOauthSignup({
|
|
2261
|
+
await emitOauthSignup({
|
|
2262
|
+
wsc: provider.wsc,
|
|
2263
|
+
providerSlug: "google",
|
|
2264
|
+
signature,
|
|
2265
|
+
mode: flow,
|
|
2266
|
+
getIp
|
|
2267
|
+
});
|
|
2106
2268
|
},
|
|
2107
2269
|
signupMicrosoft: async () => {
|
|
2108
2270
|
const res = await signInWithMicrosoft();
|
|
2109
|
-
await emitOauthSignup({
|
|
2271
|
+
await emitOauthSignup({
|
|
2272
|
+
wsc: provider.wsc,
|
|
2273
|
+
providerSlug: "microsoft",
|
|
2274
|
+
signature: res.accessToken,
|
|
2275
|
+
mode: "popup",
|
|
2276
|
+
getIp
|
|
2277
|
+
});
|
|
2110
2278
|
}
|
|
2111
2279
|
},
|
|
2112
2280
|
// signin transaction + logout
|
|
@@ -2117,18 +2285,29 @@ function createBlockAuthClient(config3) {
|
|
|
2117
2285
|
service: $ui.get().serviceName ?? "blockauth",
|
|
2118
2286
|
apiKey: config3.apiKey
|
|
2119
2287
|
}),
|
|
2120
|
-
cancelTxns: () => cancelTxns({
|
|
2288
|
+
cancelTxns: () => cancelTxns({
|
|
2289
|
+
wsc: provider.wsc,
|
|
2290
|
+
token: userToken()
|
|
2291
|
+
}),
|
|
2121
2292
|
// transaction accept/reject
|
|
2122
2293
|
txns: {
|
|
2123
|
-
reject: () => rejectTxn({
|
|
2294
|
+
reject: () => rejectTxn({
|
|
2295
|
+
wsc: provider.wsc,
|
|
2296
|
+
token: userToken()
|
|
2297
|
+
}),
|
|
2124
2298
|
accept: (opts) => acceptTxn({
|
|
2125
2299
|
wsc: provider.wsc,
|
|
2126
2300
|
token: userToken(),
|
|
2127
2301
|
lifetime: opts.lifetime,
|
|
2128
2302
|
providerSlug: $ui.get().serviceName ?? "blockauth",
|
|
2129
|
-
userParams: opts.userParams
|
|
2303
|
+
userParams: opts.userParams,
|
|
2304
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2130
2305
|
}),
|
|
2131
|
-
report: (slug) => reportTxn({
|
|
2306
|
+
report: (slug) => reportTxn({
|
|
2307
|
+
wsc: provider.wsc,
|
|
2308
|
+
token: userToken(),
|
|
2309
|
+
report: slug
|
|
2310
|
+
})
|
|
2132
2311
|
},
|
|
2133
2312
|
// account restore: resolves when the backend answers; expired
|
|
2134
2313
|
// restorations surface the restorationExpired modal, other failures
|
|
@@ -2137,7 +2316,13 @@ function createBlockAuthClient(config3) {
|
|
|
2137
2316
|
// full deletion-state teardown after the user confirms the
|
|
2138
2317
|
// restoration-expired modal.
|
|
2139
2318
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2140
|
-
logout: () => performLogout({
|
|
2319
|
+
logout: () => performLogout({
|
|
2320
|
+
wsc: provider.wsc,
|
|
2321
|
+
token: userToken(),
|
|
2322
|
+
apiKey: config3.apiKey,
|
|
2323
|
+
sessionId: browserBase.getDeviceSessionId(),
|
|
2324
|
+
apiUrl: config3.apiUrl
|
|
2325
|
+
}),
|
|
2141
2326
|
// modals (stack — supports overlaying signin on top of signup)
|
|
2142
2327
|
openModal,
|
|
2143
2328
|
closeModal,
|
|
@@ -2145,6 +2330,9 @@ function createBlockAuthClient(config3) {
|
|
|
2145
2330
|
// opciones del connect inicial del WS (las consume createAuthRuntime)
|
|
2146
2331
|
wsConnectTimeout: config3.wsConnectTimeout ?? 300,
|
|
2147
2332
|
wsConnectRetries: config3.wsConnectRetries ?? 3,
|
|
2333
|
+
// Backend base url for the session/logout/ws-ticket bridge (consumed by
|
|
2334
|
+
// createAuthRuntime and by browserBase.setSession callers here above).
|
|
2335
|
+
apiUrl: config3.apiUrl,
|
|
2148
2336
|
// acceso crudo al provider si hace falta
|
|
2149
2337
|
provider
|
|
2150
2338
|
};
|
|
@@ -2158,6 +2346,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2158
2346
|
isUnsignTimeoutSet: false,
|
|
2159
2347
|
isSigned: false,
|
|
2160
2348
|
isSigning: false,
|
|
2349
|
+
isLoggingOut: false,
|
|
2161
2350
|
lastCheckedDid: null,
|
|
2162
2351
|
signinAttemptId: 0
|
|
2163
2352
|
};
|
|
@@ -2192,33 +2381,101 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2192
2381
|
await onError();
|
|
2193
2382
|
};
|
|
2194
2383
|
const _onLogout = async () => {
|
|
2384
|
+
if (hookState.isLoggingOut) return applyLocalLogout();
|
|
2385
|
+
hookState.isLoggingOut = true;
|
|
2195
2386
|
resetUser();
|
|
2387
|
+
if (typeof localStorage !== "undefined") {
|
|
2388
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2389
|
+
}
|
|
2196
2390
|
try {
|
|
2197
2391
|
if (provider && provider.wsc) {
|
|
2198
2392
|
const s = browserBase.getSession();
|
|
2199
2393
|
const ext = browserBase.getExternalService();
|
|
2200
2394
|
const _token = ext.service !== "blockauth" ? ext.token : s.token;
|
|
2201
|
-
const _apiKey = $ws.get().apiKey;
|
|
2395
|
+
const _apiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2396
|
+
const _sessionId = browserBase.getDeviceSessionId();
|
|
2202
2397
|
await new Promise((resolve) => {
|
|
2203
2398
|
const timeout = setTimeout(resolve, 3e3);
|
|
2204
2399
|
wsc().ws.once("signout", () => {
|
|
2205
2400
|
clearTimeout(timeout);
|
|
2206
2401
|
resolve();
|
|
2207
2402
|
});
|
|
2208
|
-
wsc().emit("signout", {
|
|
2403
|
+
wsc().emit("signout", {
|
|
2404
|
+
token: _token,
|
|
2405
|
+
apiKey: _apiKey,
|
|
2406
|
+
sessionId: _sessionId
|
|
2407
|
+
});
|
|
2209
2408
|
});
|
|
2210
2409
|
}
|
|
2211
2410
|
} catch {
|
|
2212
2411
|
}
|
|
2213
|
-
|
|
2214
|
-
|
|
2412
|
+
await applyLocalLogout();
|
|
2413
|
+
};
|
|
2414
|
+
let logoutInFlight = null;
|
|
2415
|
+
const applyLocalLogout = async () => {
|
|
2416
|
+
if (logoutInFlight) return logoutInFlight;
|
|
2417
|
+
logoutInFlight = (async () => {
|
|
2418
|
+
hookState.isLoggingOut = true;
|
|
2419
|
+
if (unsignTimeout) {
|
|
2420
|
+
clearTimeout(unsignTimeout);
|
|
2421
|
+
unsignTimeout = null;
|
|
2422
|
+
}
|
|
2423
|
+
hookState.isUnsignTimeoutSet = false;
|
|
2424
|
+
if (signinTimeout) {
|
|
2425
|
+
clearTimeout(signinTimeout);
|
|
2426
|
+
signinTimeout = null;
|
|
2427
|
+
}
|
|
2428
|
+
if (getUserTxnsInterval) {
|
|
2429
|
+
clearInterval(getUserTxnsInterval);
|
|
2430
|
+
getUserTxnsInterval = null;
|
|
2431
|
+
}
|
|
2432
|
+
hookState.isSigning = false;
|
|
2433
|
+
hookState.isSigned = false;
|
|
2434
|
+
setIsSigned(false);
|
|
2435
|
+
setIsSigning(false);
|
|
2436
|
+
setIsSigningLoader(false);
|
|
2437
|
+
setIsAcceptingTxn(false);
|
|
2438
|
+
setIsSignedFirstCheck(true);
|
|
2439
|
+
resetUser();
|
|
2440
|
+
if (typeof localStorage !== "undefined") {
|
|
2441
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2442
|
+
}
|
|
2443
|
+
await clearSessionCookie(client.apiUrl);
|
|
2444
|
+
const serviceName = browserBase.getExternalService().service;
|
|
2445
|
+
setIsServiceConnected(false);
|
|
2446
|
+
setWaitingForSuccessLogInSince(0);
|
|
2447
|
+
setServiceIcon(imgUrlByService(serviceName));
|
|
2448
|
+
setServiceName(serviceName);
|
|
2449
|
+
try {
|
|
2450
|
+
await onLogout();
|
|
2451
|
+
} finally {
|
|
2452
|
+
hookState.isLoggingOut = false;
|
|
2453
|
+
}
|
|
2454
|
+
})().finally(() => {
|
|
2455
|
+
logoutInFlight = null;
|
|
2456
|
+
});
|
|
2457
|
+
return logoutInFlight;
|
|
2458
|
+
};
|
|
2459
|
+
const onStorageLogout = (event) => {
|
|
2460
|
+
if (event.key !== "blockauth-logged-out" || event.newValue !== "true") {
|
|
2461
|
+
return;
|
|
2215
2462
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2463
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2464
|
+
void applyLocalLogout().catch((err) => {
|
|
2465
|
+
console.error("[sdk] applyLocalLogout failed (storage event):", err);
|
|
2466
|
+
});
|
|
2467
|
+
};
|
|
2468
|
+
const onExtensionSessionBridged = (event) => {
|
|
2469
|
+
const did = event.detail?.did;
|
|
2470
|
+
if (did) {
|
|
2471
|
+
void browserBase.setSession({
|
|
2472
|
+
did,
|
|
2473
|
+
serviceName: "blockauth",
|
|
2474
|
+
apiUrl: client.apiUrl
|
|
2475
|
+
});
|
|
2476
|
+
setDid(did);
|
|
2477
|
+
}
|
|
2478
|
+
void wsc().refreshAuth();
|
|
2222
2479
|
};
|
|
2223
2480
|
const deletion = createDeletionService({ provider });
|
|
2224
2481
|
const handleUserDeleted = (forAddress = null) => {
|
|
@@ -2240,10 +2497,11 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2240
2497
|
if (service === "blockauth" || prevDid && prevDid.toLowerCase() !== String(did).toLowerCase()) {
|
|
2241
2498
|
browserBase.clearBrowserState({ keepExtensionInstalled: keepInstalled });
|
|
2242
2499
|
}
|
|
2243
|
-
browserBase.setSession({
|
|
2500
|
+
void browserBase.setSession({
|
|
2244
2501
|
...result,
|
|
2245
|
-
serviceName: service
|
|
2246
|
-
|
|
2502
|
+
serviceName: service,
|
|
2503
|
+
apiUrl: client.apiUrl
|
|
2504
|
+
}).then(() => wsc().refreshAuth());
|
|
2247
2505
|
setDid(did);
|
|
2248
2506
|
setToken(token);
|
|
2249
2507
|
setServiceIcon(imgUrlByService(service));
|
|
@@ -2310,10 +2568,6 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2310
2568
|
const signinUnsigned = async () => {
|
|
2311
2569
|
setIsSignedFirstCheck(true);
|
|
2312
2570
|
if (!hookState.isSigned) {
|
|
2313
|
-
const ext = browserBase.getExternalService();
|
|
2314
|
-
if (ext.connected && typeof localStorage !== "undefined") {
|
|
2315
|
-
localStorage.setItem("blockauth-logged-out", "true");
|
|
2316
|
-
}
|
|
2317
2571
|
await _onError();
|
|
2318
2572
|
}
|
|
2319
2573
|
};
|
|
@@ -2495,6 +2749,21 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2495
2749
|
const onSdkSignRefresh = () => {
|
|
2496
2750
|
void isUserSignedIn();
|
|
2497
2751
|
};
|
|
2752
|
+
const onSdkSessionInvalidated = (data) => {
|
|
2753
|
+
const apiKey = data?.apiKey ?? data?.message?.apiKey;
|
|
2754
|
+
const clientApiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2755
|
+
if (!apiKey || !clientApiKey || apiKey !== clientApiKey) return;
|
|
2756
|
+
const sessionId = data?.sessionId ?? data?.message?.sessionId;
|
|
2757
|
+
if (sessionId != null && String(sessionId) !== String(browserBase.getDeviceSessionId()))
|
|
2758
|
+
return;
|
|
2759
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2760
|
+
void applyLocalLogout().catch((err) => {
|
|
2761
|
+
console.error(
|
|
2762
|
+
"[sdk] applyLocalLogout failed (session invalidated):",
|
|
2763
|
+
err
|
|
2764
|
+
);
|
|
2765
|
+
});
|
|
2766
|
+
};
|
|
2498
2767
|
const onSdkUserRolesChanged = async (data) => {
|
|
2499
2768
|
const { success, message } = data || {};
|
|
2500
2769
|
if (!success || !message?.address) return;
|
|
@@ -2505,7 +2774,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2505
2774
|
if (!activeDid || activeDid !== changedAddress) return;
|
|
2506
2775
|
await fetchUserProfile(provider, activeDid);
|
|
2507
2776
|
};
|
|
2508
|
-
const onSdkRestore = (data) => {
|
|
2777
|
+
const onSdkRestore = async (data) => {
|
|
2509
2778
|
if (data.success) {
|
|
2510
2779
|
deletion.resetHandlingState();
|
|
2511
2780
|
setIsOnDeletion(false);
|
|
@@ -2516,15 +2785,17 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2516
2785
|
browserBase.clearBrowserState({
|
|
2517
2786
|
keepExtensionInstalled: keepInstalled
|
|
2518
2787
|
});
|
|
2519
|
-
browserBase.setSession({
|
|
2788
|
+
await browserBase.setSession({
|
|
2520
2789
|
did: session.did,
|
|
2521
2790
|
token: session.token,
|
|
2522
|
-
serviceName: "blockauth"
|
|
2791
|
+
serviceName: "blockauth",
|
|
2792
|
+
apiUrl: client.apiUrl
|
|
2523
2793
|
});
|
|
2524
2794
|
if (session.serviceName && session.serviceName !== "blockauth") {
|
|
2525
|
-
browserBase.setSession({
|
|
2795
|
+
await browserBase.setSession({
|
|
2526
2796
|
...session,
|
|
2527
|
-
serviceName: session.serviceName
|
|
2797
|
+
serviceName: session.serviceName,
|
|
2798
|
+
apiUrl: client.apiUrl
|
|
2528
2799
|
});
|
|
2529
2800
|
}
|
|
2530
2801
|
}
|
|
@@ -2550,8 +2821,9 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2550
2821
|
}
|
|
2551
2822
|
setDid(_did);
|
|
2552
2823
|
setToken(_token);
|
|
2553
|
-
setApiKey($ws.get().apiKey);
|
|
2554
|
-
|
|
2824
|
+
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
2825
|
+
const isLoggedOut = typeof localStorage !== "undefined" && localStorage.getItem("blockauth-logged-out") === "true";
|
|
2826
|
+
setIsServiceConnected(isLoggedOut ? false : externalService.connected);
|
|
2555
2827
|
setServiceName(externalService.service);
|
|
2556
2828
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
2557
2829
|
await provider.connect(
|
|
@@ -2589,8 +2861,18 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2589
2861
|
wsc().on("signin:unsigned", onSigninUnsigned);
|
|
2590
2862
|
wsc().on("signin:onDeletion", onSigninOnDeletion);
|
|
2591
2863
|
wsc().on("sdk:sign:refresh", onSdkSignRefresh);
|
|
2864
|
+
wsc().on("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2592
2865
|
wsc().on("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2593
2866
|
wsc().on("sdk:restore", onSdkRestore);
|
|
2867
|
+
if (typeof window !== "undefined") {
|
|
2868
|
+
window.addEventListener("storage", onStorageLogout);
|
|
2869
|
+
}
|
|
2870
|
+
if (typeof document !== "undefined") {
|
|
2871
|
+
document.addEventListener(
|
|
2872
|
+
"blockauth:session-bridged",
|
|
2873
|
+
onExtensionSessionBridged
|
|
2874
|
+
);
|
|
2875
|
+
}
|
|
2594
2876
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
2595
2877
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
2596
2878
|
setReady(true);
|
|
@@ -2626,9 +2908,19 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2626
2908
|
w.off("signin:unsigned", onSigninUnsigned);
|
|
2627
2909
|
w.off("signin:onDeletion", onSigninOnDeletion);
|
|
2628
2910
|
w.off("sdk:sign:refresh", onSdkSignRefresh);
|
|
2911
|
+
w.off("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2629
2912
|
w.off("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2630
2913
|
w.off("sdk:restore", onSdkRestore);
|
|
2631
2914
|
}
|
|
2915
|
+
if (typeof window !== "undefined") {
|
|
2916
|
+
window.removeEventListener("storage", onStorageLogout);
|
|
2917
|
+
}
|
|
2918
|
+
if (typeof document !== "undefined") {
|
|
2919
|
+
document.removeEventListener(
|
|
2920
|
+
"blockauth:session-bridged",
|
|
2921
|
+
onExtensionSessionBridged
|
|
2922
|
+
);
|
|
2923
|
+
}
|
|
2632
2924
|
provider.disconnect();
|
|
2633
2925
|
};
|
|
2634
2926
|
const logout = async () => {
|