@block-auth.io/core 0.6.0 → 0.7.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 +82 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -5
- package/dist/index.d.ts +38 -5
- package/dist/index.js +82 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -71,6 +71,34 @@ async function fetchWsTicket(apiUrl) {
|
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
+
var DEFAULT_WS_URL = "wss://wss.block-auth.io";
|
|
75
|
+
function buildWsServerApiUrl(path, wsUrl) {
|
|
76
|
+
const url = new URL(wsUrl || DEFAULT_WS_URL);
|
|
77
|
+
url.protocol = url.protocol === "wss:" ? "https:" : "http:";
|
|
78
|
+
url.pathname = "/api";
|
|
79
|
+
url.search = "";
|
|
80
|
+
url.hash = "";
|
|
81
|
+
return `${url.toString().replace(/\/+$/, "")}${path}`;
|
|
82
|
+
}
|
|
83
|
+
async function fetchBrowserToken(apiKey, wsUrl) {
|
|
84
|
+
if (!apiKey || typeof fetch === "undefined") return null;
|
|
85
|
+
try {
|
|
86
|
+
const response = await fetch(
|
|
87
|
+
buildWsServerApiUrl("/browser-token", wsUrl),
|
|
88
|
+
{
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { "Content-Type": "application/json" },
|
|
91
|
+
body: JSON.stringify({ apiKey })
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
const payload = await response.json();
|
|
95
|
+
if (!response.ok || !payload?.success) return null;
|
|
96
|
+
return payload.message?.accessToken || null;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.warn("[block-auth] failed to fetch browser token", error);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
74
102
|
|
|
75
103
|
// src/protocol/WsClient.ts
|
|
76
104
|
var WsClient = class {
|
|
@@ -173,7 +201,7 @@ var WsClient = class {
|
|
|
173
201
|
this.on("reconnect_error", (e) => this.onReconnectError(e));
|
|
174
202
|
this.on("reconnect_attempt", (n) => this.onReconnectAttempt(n));
|
|
175
203
|
this.on("disconnect", (r) => this.onDisconnect(r));
|
|
176
|
-
this.on("sdk:signin", this.onSdkSignin);
|
|
204
|
+
this.on("sdk:signin", (d) => this.onSdkSignin(d));
|
|
177
205
|
this.on("sdk:refresh", this.onSdkRefresh);
|
|
178
206
|
this.on("sdk:app", this.onSdkApp);
|
|
179
207
|
this.on("sdk:user", this.onSdkUser);
|
|
@@ -320,7 +348,10 @@ var WsClient = class {
|
|
|
320
348
|
console.log("[block-auth][WARNING] App not active");
|
|
321
349
|
return message;
|
|
322
350
|
}
|
|
323
|
-
if (!success)
|
|
351
|
+
if (!success) {
|
|
352
|
+
console.log("[block-auth][WARNING] signin failed: ", message);
|
|
353
|
+
return message;
|
|
354
|
+
}
|
|
324
355
|
const { accessToken, refreshToken } = message;
|
|
325
356
|
this._accessToken = accessToken;
|
|
326
357
|
this._refreshToken = refreshToken;
|
|
@@ -612,6 +643,19 @@ var BlockAuthProvider = class {
|
|
|
612
643
|
this._wsc = new WsClient(wsUrl, true, options.apiUrl);
|
|
613
644
|
this._apiKey = apiKey;
|
|
614
645
|
this._apiSecret = apiSecret;
|
|
646
|
+
this._browserToken = options.browserToken || "";
|
|
647
|
+
this._wsUrl = wsUrl;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Browser token used instead of the apiSecret on signin().
|
|
651
|
+
* Can be updated at runtime (e.g. after fetching a fresh one
|
|
652
|
+
* from your backend when the previous one expired).
|
|
653
|
+
*/
|
|
654
|
+
set browserToken(token) {
|
|
655
|
+
this._browserToken = token || "";
|
|
656
|
+
}
|
|
657
|
+
get browserToken() {
|
|
658
|
+
return this._browserToken;
|
|
615
659
|
}
|
|
616
660
|
/**
|
|
617
661
|
* Allow you to connect to the WebSocket API
|
|
@@ -639,19 +683,23 @@ var BlockAuthProvider = class {
|
|
|
639
683
|
return this._wsc.isConnected();
|
|
640
684
|
}
|
|
641
685
|
/**
|
|
642
|
-
* Allow to Signin
|
|
643
|
-
* to get the access token and refresh token.
|
|
686
|
+
* Allow to Signin to get the access token and refresh token.
|
|
644
687
|
* The access token is valid for 1 hour.
|
|
645
688
|
* The refresh token is valid for 2 hours.
|
|
646
689
|
* You can use the refresh token to get a new
|
|
647
690
|
* access token when the access token is expired using
|
|
648
691
|
* the method `refresh()`.
|
|
649
|
-
*
|
|
650
|
-
*
|
|
692
|
+
*
|
|
693
|
+
* Authenticates with the browser token when one was provided
|
|
694
|
+
* (options.browserToken or the browserToken setter); otherwise
|
|
695
|
+
* falls back to the API Key + API Secret pair. In browsers,
|
|
696
|
+
* always prefer the browser token: the apiSecret must never
|
|
697
|
+
* ship to the client.
|
|
651
698
|
* @returns Promise<any>
|
|
652
699
|
*/
|
|
653
700
|
async signin() {
|
|
654
701
|
if (!this.isConnected()) throw new Error("Not connected");
|
|
702
|
+
await this.resolveBrowserToken();
|
|
655
703
|
return new Promise((resolve) => {
|
|
656
704
|
this._wsc.ws.once("sdk:signin", (d) => {
|
|
657
705
|
const { success, message } = d;
|
|
@@ -659,12 +707,29 @@ var BlockAuthProvider = class {
|
|
|
659
707
|
if (!success) return;
|
|
660
708
|
this.wsc.onSdkSignin(d);
|
|
661
709
|
});
|
|
662
|
-
this.
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
})
|
|
710
|
+
const credentials = { apiKey: this._apiKey };
|
|
711
|
+
if (this._browserToken) {
|
|
712
|
+
credentials.browserToken = this._browserToken;
|
|
713
|
+
} else if (this._apiSecret) {
|
|
714
|
+
credentials.apiSecret = cryptoJs.SHA256(this._apiSecret).toString();
|
|
715
|
+
}
|
|
716
|
+
this._wsc.emit("sdk:signin", credentials);
|
|
666
717
|
});
|
|
667
718
|
}
|
|
719
|
+
/**
|
|
720
|
+
* Last-resort credential for a browser: trade the publishable apiKey for a
|
|
721
|
+
* browser token. No-op when a credential is already configured - an
|
|
722
|
+
* explicit browserToken (from the app's own backend) or an apiSecret (a
|
|
723
|
+
* backend caller) always wins, and neither must be spent on a network
|
|
724
|
+
* round trip. Failure is silent (fetchBrowserToken resolves null), so a
|
|
725
|
+
* third-party app with no credential still reaches the server and gets its
|
|
726
|
+
* 'Credentials required' answer.
|
|
727
|
+
*/
|
|
728
|
+
async resolveBrowserToken() {
|
|
729
|
+
if (this._browserToken || this._apiSecret) return;
|
|
730
|
+
const token = await fetchBrowserToken(this._apiKey, this._wsUrl);
|
|
731
|
+
if (token) this._browserToken = token;
|
|
732
|
+
}
|
|
668
733
|
/**
|
|
669
734
|
* Allow to refresh the access token when it's expired.
|
|
670
735
|
* @param refreshToken - your refresh token
|
|
@@ -2194,7 +2259,7 @@ function createBlockAuthClient(config3) {
|
|
|
2194
2259
|
config3.apiKey,
|
|
2195
2260
|
config3.apiSecret,
|
|
2196
2261
|
config3.wsUrl ?? "wss://wss.block-auth.io",
|
|
2197
|
-
{ apiUrl: config3.apiUrl }
|
|
2262
|
+
{ apiUrl: config3.apiUrl, browserToken: config3.browserToken }
|
|
2198
2263
|
);
|
|
2199
2264
|
const evm = createEvmWallet({
|
|
2200
2265
|
walletConnectProjectId: config3.walletConnectProjectId,
|
|
@@ -2245,6 +2310,11 @@ function createBlockAuthClient(config3) {
|
|
|
2245
2310
|
connect: (...a) => provider.connect(...a),
|
|
2246
2311
|
disconnect: () => provider.disconnect(),
|
|
2247
2312
|
signin: () => provider.signin(),
|
|
2313
|
+
// Actualiza el browserToken en runtime (p.ej. tras pedir uno fresco al
|
|
2314
|
+
// backend cuando el anterior caducó).
|
|
2315
|
+
setBrowserToken: (token) => {
|
|
2316
|
+
provider.browserToken = token;
|
|
2317
|
+
},
|
|
2248
2318
|
refresh: (t2) => provider.refresh(t2),
|
|
2249
2319
|
app: (t2) => provider.app(t2),
|
|
2250
2320
|
user: (addr, t2) => provider.user(addr, t2),
|
|
@@ -2614,6 +2684,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2614
2684
|
};
|
|
2615
2685
|
const signinUnsigned = async () => {
|
|
2616
2686
|
setIsSignedFirstCheck(true);
|
|
2687
|
+
if (hookState.isSigning) return;
|
|
2617
2688
|
if (!hookState.isSigned) {
|
|
2618
2689
|
await _onError();
|
|
2619
2690
|
}
|