@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.d.cts
CHANGED
|
@@ -421,13 +421,28 @@ interface BlockAuthProviderOptions {
|
|
|
421
421
|
* https://api.block-auth.io.
|
|
422
422
|
*/
|
|
423
423
|
apiUrl?: string;
|
|
424
|
+
/**
|
|
425
|
+
* Browser token issued by your backend via POST /api/browser-token.
|
|
426
|
+
* When provided, signin() authenticates with it and the apiSecret is
|
|
427
|
+
* never needed in the browser (leave apiSecret empty).
|
|
428
|
+
*/
|
|
429
|
+
browserToken?: string;
|
|
424
430
|
}
|
|
425
431
|
declare class BlockAuthProvider {
|
|
426
432
|
private _rpc;
|
|
427
433
|
private _wsc;
|
|
428
434
|
private _apiKey;
|
|
429
435
|
private _apiSecret;
|
|
436
|
+
private _browserToken;
|
|
437
|
+
private _wsUrl;
|
|
430
438
|
constructor(apiKey?: string, apiSecret?: string, wsUrl?: string, options?: BlockAuthProviderOptions);
|
|
439
|
+
/**
|
|
440
|
+
* Browser token used instead of the apiSecret on signin().
|
|
441
|
+
* Can be updated at runtime (e.g. after fetching a fresh one
|
|
442
|
+
* from your backend when the previous one expired).
|
|
443
|
+
*/
|
|
444
|
+
set browserToken(token: string);
|
|
445
|
+
get browserToken(): string;
|
|
431
446
|
/**
|
|
432
447
|
* Allow you to connect to the WebSocket API
|
|
433
448
|
* for Block-Auth service
|
|
@@ -448,18 +463,31 @@ declare class BlockAuthProvider {
|
|
|
448
463
|
*/
|
|
449
464
|
isConnected(): any;
|
|
450
465
|
/**
|
|
451
|
-
* Allow to Signin
|
|
452
|
-
* to get the access token and refresh token.
|
|
466
|
+
* Allow to Signin to get the access token and refresh token.
|
|
453
467
|
* The access token is valid for 1 hour.
|
|
454
468
|
* The refresh token is valid for 2 hours.
|
|
455
469
|
* You can use the refresh token to get a new
|
|
456
470
|
* access token when the access token is expired using
|
|
457
471
|
* the method `refresh()`.
|
|
458
|
-
*
|
|
459
|
-
*
|
|
472
|
+
*
|
|
473
|
+
* Authenticates with the browser token when one was provided
|
|
474
|
+
* (options.browserToken or the browserToken setter); otherwise
|
|
475
|
+
* falls back to the API Key + API Secret pair. In browsers,
|
|
476
|
+
* always prefer the browser token: the apiSecret must never
|
|
477
|
+
* ship to the client.
|
|
460
478
|
* @returns Promise<any>
|
|
461
479
|
*/
|
|
462
480
|
signin(): Promise<any>;
|
|
481
|
+
/**
|
|
482
|
+
* Last-resort credential for a browser: trade the publishable apiKey for a
|
|
483
|
+
* browser token. No-op when a credential is already configured - an
|
|
484
|
+
* explicit browserToken (from the app's own backend) or an apiSecret (a
|
|
485
|
+
* backend caller) always wins, and neither must be spent on a network
|
|
486
|
+
* round trip. Failure is silent (fetchBrowserToken resolves null), so a
|
|
487
|
+
* third-party app with no credential still reaches the server and gets its
|
|
488
|
+
* 'Credentials required' answer.
|
|
489
|
+
*/
|
|
490
|
+
private resolveBrowserToken;
|
|
463
491
|
/**
|
|
464
492
|
* Allow to refresh the access token when it's expired.
|
|
465
493
|
* @param refreshToken - your refresh token
|
|
@@ -778,6 +806,10 @@ declare namespace index$1 {
|
|
|
778
806
|
interface BlockAuthClientConfig {
|
|
779
807
|
apiKey: string;
|
|
780
808
|
apiSecret: string;
|
|
809
|
+
/** Opcional: token emitido por tu backend vía POST /api/browser-token.
|
|
810
|
+
* Si está presente, signin() lo usa en lugar del apiSecret — en navegador
|
|
811
|
+
* deja apiSecret vacío para que el secreto nunca llegue al cliente. */
|
|
812
|
+
browserToken?: string;
|
|
781
813
|
wsUrl?: string;
|
|
782
814
|
/** Backend base url used for the session-cookie bridge (POST
|
|
783
815
|
* /auth/session), logout (POST /auth/logout) and the WS connect ticket
|
|
@@ -817,6 +849,7 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
817
849
|
connect: (waitMs?: number | undefined, url?: string | undefined, retries?: number | undefined) => Promise<void>;
|
|
818
850
|
disconnect: () => Promise<void>;
|
|
819
851
|
signin: () => Promise<any>;
|
|
852
|
+
setBrowserToken: (token: string) => void;
|
|
820
853
|
refresh: (t?: string) => Promise<any>;
|
|
821
854
|
app: (t?: string) => Promise<any>;
|
|
822
855
|
user: (addr: string, t?: string) => Promise<any>;
|
|
@@ -1903,4 +1936,4 @@ declare const TEXTS: {
|
|
|
1903
1936
|
type TextKey = keyof typeof TEXTS;
|
|
1904
1937
|
declare function t(key: TextKey, locale?: Locale): string;
|
|
1905
1938
|
|
|
1906
|
-
export { $extension, $locale, $openModals, $ui, $user, $wallet, $web3, $ws, type AuthRuntime, type AuthRuntimeCallbacks, type BlockAuthClient, type BlockAuthClientConfig, BlockAuthProvider, type BlockAuthUserProfile, type BlockAuthUserRole, DISCOVERY_TIMEOUT_MS, type DeletionServiceDeps, type EvmOptions, type EvmWallet, type ExtensionState, type Locale, type ModalName, type RestoreResult, RpcClient, SDK_PAGE_MARKER, SYNC_TIMEOUT_MS, type SolanaWallet, TEXTS, type TextKey, type TxnRequest, type UiState, type UserProfileProvider, type UserState, type WalletKind, type WalletState, type Web3SignupState, WsClient, type WsState, appearsExtensionInstalled, clearExtensionDomMarkers, clearRemainingAuthData, clearSdkPresence, closeAllModals, closeModal, confirmExtensionPresence, createAuthRuntime, createBlockAuthClient, createDeletionService, createEvmWallet, createSolanaWallet, deepLinkFor, discoverExtensionOnce, fetchUserProfile, finalizeAfterRestorationExpired, getUserRoleNames, getUserRoleSlugs, getUserRoles, handleRestorationExpired, handleRestoreFailure, hasAnyRole, hasAnyRoleBySlug, hasRole, hasRoleBySlug, isExtensionPresent, isExtensionStateComplete, isModalOpen, isRestorationExpiredMessage, isSdkPage, isShowSignin, loadUserProfile, makeQrDataUrl, markSdkPresence, index$1 as oauth, onExtensionRemoved, onExtensionStatePublished, openModal, probeExtensionPresence, reconcileExtensionState, requestExtensionRefresh, requestExtensionStateSync, requestSdkRestore, resetExtension, resetExtensionAbsentState, resetExtensionSyncModuleState, resetUi, resetUser, resetWallet, resetWeb3, resetWs, setApiKey, setBackendMessage, setDeletionAddress, setDid, setExtensionInstalled, setExtensionIsLocked, setExtensionIsUser, setExtensionIsUserActive, setIsAcceptingTxn, setIsAppActive, setIsCheckingAppActive, setIsLoadingUser, setIsOnDeletion, setIsSdkSignedIn, setIsServiceConnected, setIsSigned, setIsSignedFirstCheck, setIsSigning, setIsSigningLoader, setLocale, setReady, setRequests, setServiceIcon, setServiceName, setSignature, setSignedBy, setToken, setUiConfig, setUser, setWaitingForSuccessLogInSince, startExtensionSync, syncExtensionStateFromStorage, t, index as utils };
|
|
1939
|
+
export { $extension, $locale, $openModals, $ui, $user, $wallet, $web3, $ws, type AuthRuntime, type AuthRuntimeCallbacks, type BlockAuthClient, type BlockAuthClientConfig, BlockAuthProvider, type BlockAuthProviderOptions, type BlockAuthUserProfile, type BlockAuthUserRole, DISCOVERY_TIMEOUT_MS, type DeletionServiceDeps, type EvmOptions, type EvmWallet, type ExtensionState, type Locale, type ModalName, type RestoreResult, RpcClient, SDK_PAGE_MARKER, SYNC_TIMEOUT_MS, type SolanaWallet, TEXTS, type TextKey, type TxnRequest, type UiState, type UserProfileProvider, type UserState, type WalletKind, type WalletState, type Web3SignupState, WsClient, type WsState, appearsExtensionInstalled, clearExtensionDomMarkers, clearRemainingAuthData, clearSdkPresence, closeAllModals, closeModal, confirmExtensionPresence, createAuthRuntime, createBlockAuthClient, createDeletionService, createEvmWallet, createSolanaWallet, deepLinkFor, discoverExtensionOnce, fetchUserProfile, finalizeAfterRestorationExpired, getUserRoleNames, getUserRoleSlugs, getUserRoles, handleRestorationExpired, handleRestoreFailure, hasAnyRole, hasAnyRoleBySlug, hasRole, hasRoleBySlug, isExtensionPresent, isExtensionStateComplete, isModalOpen, isRestorationExpiredMessage, isSdkPage, isShowSignin, loadUserProfile, makeQrDataUrl, markSdkPresence, index$1 as oauth, onExtensionRemoved, onExtensionStatePublished, openModal, probeExtensionPresence, reconcileExtensionState, requestExtensionRefresh, requestExtensionStateSync, requestSdkRestore, resetExtension, resetExtensionAbsentState, resetExtensionSyncModuleState, resetUi, resetUser, resetWallet, resetWeb3, resetWs, setApiKey, setBackendMessage, setDeletionAddress, setDid, setExtensionInstalled, setExtensionIsLocked, setExtensionIsUser, setExtensionIsUserActive, setIsAcceptingTxn, setIsAppActive, setIsCheckingAppActive, setIsLoadingUser, setIsOnDeletion, setIsSdkSignedIn, setIsServiceConnected, setIsSigned, setIsSignedFirstCheck, setIsSigning, setIsSigningLoader, setLocale, setReady, setRequests, setServiceIcon, setServiceName, setSignature, setSignedBy, setToken, setUiConfig, setUser, setWaitingForSuccessLogInSince, startExtensionSync, syncExtensionStateFromStorage, t, index as utils };
|
package/dist/index.d.ts
CHANGED
|
@@ -421,13 +421,28 @@ interface BlockAuthProviderOptions {
|
|
|
421
421
|
* https://api.block-auth.io.
|
|
422
422
|
*/
|
|
423
423
|
apiUrl?: string;
|
|
424
|
+
/**
|
|
425
|
+
* Browser token issued by your backend via POST /api/browser-token.
|
|
426
|
+
* When provided, signin() authenticates with it and the apiSecret is
|
|
427
|
+
* never needed in the browser (leave apiSecret empty).
|
|
428
|
+
*/
|
|
429
|
+
browserToken?: string;
|
|
424
430
|
}
|
|
425
431
|
declare class BlockAuthProvider {
|
|
426
432
|
private _rpc;
|
|
427
433
|
private _wsc;
|
|
428
434
|
private _apiKey;
|
|
429
435
|
private _apiSecret;
|
|
436
|
+
private _browserToken;
|
|
437
|
+
private _wsUrl;
|
|
430
438
|
constructor(apiKey?: string, apiSecret?: string, wsUrl?: string, options?: BlockAuthProviderOptions);
|
|
439
|
+
/**
|
|
440
|
+
* Browser token used instead of the apiSecret on signin().
|
|
441
|
+
* Can be updated at runtime (e.g. after fetching a fresh one
|
|
442
|
+
* from your backend when the previous one expired).
|
|
443
|
+
*/
|
|
444
|
+
set browserToken(token: string);
|
|
445
|
+
get browserToken(): string;
|
|
431
446
|
/**
|
|
432
447
|
* Allow you to connect to the WebSocket API
|
|
433
448
|
* for Block-Auth service
|
|
@@ -448,18 +463,31 @@ declare class BlockAuthProvider {
|
|
|
448
463
|
*/
|
|
449
464
|
isConnected(): any;
|
|
450
465
|
/**
|
|
451
|
-
* Allow to Signin
|
|
452
|
-
* to get the access token and refresh token.
|
|
466
|
+
* Allow to Signin to get the access token and refresh token.
|
|
453
467
|
* The access token is valid for 1 hour.
|
|
454
468
|
* The refresh token is valid for 2 hours.
|
|
455
469
|
* You can use the refresh token to get a new
|
|
456
470
|
* access token when the access token is expired using
|
|
457
471
|
* the method `refresh()`.
|
|
458
|
-
*
|
|
459
|
-
*
|
|
472
|
+
*
|
|
473
|
+
* Authenticates with the browser token when one was provided
|
|
474
|
+
* (options.browserToken or the browserToken setter); otherwise
|
|
475
|
+
* falls back to the API Key + API Secret pair. In browsers,
|
|
476
|
+
* always prefer the browser token: the apiSecret must never
|
|
477
|
+
* ship to the client.
|
|
460
478
|
* @returns Promise<any>
|
|
461
479
|
*/
|
|
462
480
|
signin(): Promise<any>;
|
|
481
|
+
/**
|
|
482
|
+
* Last-resort credential for a browser: trade the publishable apiKey for a
|
|
483
|
+
* browser token. No-op when a credential is already configured - an
|
|
484
|
+
* explicit browserToken (from the app's own backend) or an apiSecret (a
|
|
485
|
+
* backend caller) always wins, and neither must be spent on a network
|
|
486
|
+
* round trip. Failure is silent (fetchBrowserToken resolves null), so a
|
|
487
|
+
* third-party app with no credential still reaches the server and gets its
|
|
488
|
+
* 'Credentials required' answer.
|
|
489
|
+
*/
|
|
490
|
+
private resolveBrowserToken;
|
|
463
491
|
/**
|
|
464
492
|
* Allow to refresh the access token when it's expired.
|
|
465
493
|
* @param refreshToken - your refresh token
|
|
@@ -778,6 +806,10 @@ declare namespace index$1 {
|
|
|
778
806
|
interface BlockAuthClientConfig {
|
|
779
807
|
apiKey: string;
|
|
780
808
|
apiSecret: string;
|
|
809
|
+
/** Opcional: token emitido por tu backend vía POST /api/browser-token.
|
|
810
|
+
* Si está presente, signin() lo usa en lugar del apiSecret — en navegador
|
|
811
|
+
* deja apiSecret vacío para que el secreto nunca llegue al cliente. */
|
|
812
|
+
browserToken?: string;
|
|
781
813
|
wsUrl?: string;
|
|
782
814
|
/** Backend base url used for the session-cookie bridge (POST
|
|
783
815
|
* /auth/session), logout (POST /auth/logout) and the WS connect ticket
|
|
@@ -817,6 +849,7 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
817
849
|
connect: (waitMs?: number | undefined, url?: string | undefined, retries?: number | undefined) => Promise<void>;
|
|
818
850
|
disconnect: () => Promise<void>;
|
|
819
851
|
signin: () => Promise<any>;
|
|
852
|
+
setBrowserToken: (token: string) => void;
|
|
820
853
|
refresh: (t?: string) => Promise<any>;
|
|
821
854
|
app: (t?: string) => Promise<any>;
|
|
822
855
|
user: (addr: string, t?: string) => Promise<any>;
|
|
@@ -1903,4 +1936,4 @@ declare const TEXTS: {
|
|
|
1903
1936
|
type TextKey = keyof typeof TEXTS;
|
|
1904
1937
|
declare function t(key: TextKey, locale?: Locale): string;
|
|
1905
1938
|
|
|
1906
|
-
export { $extension, $locale, $openModals, $ui, $user, $wallet, $web3, $ws, type AuthRuntime, type AuthRuntimeCallbacks, type BlockAuthClient, type BlockAuthClientConfig, BlockAuthProvider, type BlockAuthUserProfile, type BlockAuthUserRole, DISCOVERY_TIMEOUT_MS, type DeletionServiceDeps, type EvmOptions, type EvmWallet, type ExtensionState, type Locale, type ModalName, type RestoreResult, RpcClient, SDK_PAGE_MARKER, SYNC_TIMEOUT_MS, type SolanaWallet, TEXTS, type TextKey, type TxnRequest, type UiState, type UserProfileProvider, type UserState, type WalletKind, type WalletState, type Web3SignupState, WsClient, type WsState, appearsExtensionInstalled, clearExtensionDomMarkers, clearRemainingAuthData, clearSdkPresence, closeAllModals, closeModal, confirmExtensionPresence, createAuthRuntime, createBlockAuthClient, createDeletionService, createEvmWallet, createSolanaWallet, deepLinkFor, discoverExtensionOnce, fetchUserProfile, finalizeAfterRestorationExpired, getUserRoleNames, getUserRoleSlugs, getUserRoles, handleRestorationExpired, handleRestoreFailure, hasAnyRole, hasAnyRoleBySlug, hasRole, hasRoleBySlug, isExtensionPresent, isExtensionStateComplete, isModalOpen, isRestorationExpiredMessage, isSdkPage, isShowSignin, loadUserProfile, makeQrDataUrl, markSdkPresence, index$1 as oauth, onExtensionRemoved, onExtensionStatePublished, openModal, probeExtensionPresence, reconcileExtensionState, requestExtensionRefresh, requestExtensionStateSync, requestSdkRestore, resetExtension, resetExtensionAbsentState, resetExtensionSyncModuleState, resetUi, resetUser, resetWallet, resetWeb3, resetWs, setApiKey, setBackendMessage, setDeletionAddress, setDid, setExtensionInstalled, setExtensionIsLocked, setExtensionIsUser, setExtensionIsUserActive, setIsAcceptingTxn, setIsAppActive, setIsCheckingAppActive, setIsLoadingUser, setIsOnDeletion, setIsSdkSignedIn, setIsServiceConnected, setIsSigned, setIsSignedFirstCheck, setIsSigning, setIsSigningLoader, setLocale, setReady, setRequests, setServiceIcon, setServiceName, setSignature, setSignedBy, setToken, setUiConfig, setUser, setWaitingForSuccessLogInSince, startExtensionSync, syncExtensionStateFromStorage, t, index as utils };
|
|
1939
|
+
export { $extension, $locale, $openModals, $ui, $user, $wallet, $web3, $ws, type AuthRuntime, type AuthRuntimeCallbacks, type BlockAuthClient, type BlockAuthClientConfig, BlockAuthProvider, type BlockAuthProviderOptions, type BlockAuthUserProfile, type BlockAuthUserRole, DISCOVERY_TIMEOUT_MS, type DeletionServiceDeps, type EvmOptions, type EvmWallet, type ExtensionState, type Locale, type ModalName, type RestoreResult, RpcClient, SDK_PAGE_MARKER, SYNC_TIMEOUT_MS, type SolanaWallet, TEXTS, type TextKey, type TxnRequest, type UiState, type UserProfileProvider, type UserState, type WalletKind, type WalletState, type Web3SignupState, WsClient, type WsState, appearsExtensionInstalled, clearExtensionDomMarkers, clearRemainingAuthData, clearSdkPresence, closeAllModals, closeModal, confirmExtensionPresence, createAuthRuntime, createBlockAuthClient, createDeletionService, createEvmWallet, createSolanaWallet, deepLinkFor, discoverExtensionOnce, fetchUserProfile, finalizeAfterRestorationExpired, getUserRoleNames, getUserRoleSlugs, getUserRoles, handleRestorationExpired, handleRestoreFailure, hasAnyRole, hasAnyRoleBySlug, hasRole, hasRoleBySlug, isExtensionPresent, isExtensionStateComplete, isModalOpen, isRestorationExpiredMessage, isSdkPage, isShowSignin, loadUserProfile, makeQrDataUrl, markSdkPresence, index$1 as oauth, onExtensionRemoved, onExtensionStatePublished, openModal, probeExtensionPresence, reconcileExtensionState, requestExtensionRefresh, requestExtensionStateSync, requestSdkRestore, resetExtension, resetExtensionAbsentState, resetExtensionSyncModuleState, resetUi, resetUser, resetWallet, resetWeb3, resetWs, setApiKey, setBackendMessage, setDeletionAddress, setDid, setExtensionInstalled, setExtensionIsLocked, setExtensionIsUser, setExtensionIsUserActive, setIsAcceptingTxn, setIsAppActive, setIsCheckingAppActive, setIsLoadingUser, setIsOnDeletion, setIsSdkSignedIn, setIsServiceConnected, setIsSigned, setIsSignedFirstCheck, setIsSigning, setIsSigningLoader, setLocale, setReady, setRequests, setServiceIcon, setServiceName, setSignature, setSignedBy, setToken, setUiConfig, setUser, setWaitingForSuccessLogInSince, startExtensionSync, syncExtensionStateFromStorage, t, index as utils };
|
package/dist/index.js
CHANGED
|
@@ -63,6 +63,34 @@ async function fetchWsTicket(apiUrl) {
|
|
|
63
63
|
return null;
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
+
var DEFAULT_WS_URL = "wss://wss.block-auth.io";
|
|
67
|
+
function buildWsServerApiUrl(path, wsUrl) {
|
|
68
|
+
const url = new URL(wsUrl || DEFAULT_WS_URL);
|
|
69
|
+
url.protocol = url.protocol === "wss:" ? "https:" : "http:";
|
|
70
|
+
url.pathname = "/api";
|
|
71
|
+
url.search = "";
|
|
72
|
+
url.hash = "";
|
|
73
|
+
return `${url.toString().replace(/\/+$/, "")}${path}`;
|
|
74
|
+
}
|
|
75
|
+
async function fetchBrowserToken(apiKey, wsUrl) {
|
|
76
|
+
if (!apiKey || typeof fetch === "undefined") return null;
|
|
77
|
+
try {
|
|
78
|
+
const response = await fetch(
|
|
79
|
+
buildWsServerApiUrl("/browser-token", wsUrl),
|
|
80
|
+
{
|
|
81
|
+
method: "POST",
|
|
82
|
+
headers: { "Content-Type": "application/json" },
|
|
83
|
+
body: JSON.stringify({ apiKey })
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
const payload = await response.json();
|
|
87
|
+
if (!response.ok || !payload?.success) return null;
|
|
88
|
+
return payload.message?.accessToken || null;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.warn("[block-auth] failed to fetch browser token", error);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
66
94
|
|
|
67
95
|
// src/protocol/WsClient.ts
|
|
68
96
|
var WsClient = class {
|
|
@@ -165,7 +193,7 @@ var WsClient = class {
|
|
|
165
193
|
this.on("reconnect_error", (e) => this.onReconnectError(e));
|
|
166
194
|
this.on("reconnect_attempt", (n) => this.onReconnectAttempt(n));
|
|
167
195
|
this.on("disconnect", (r) => this.onDisconnect(r));
|
|
168
|
-
this.on("sdk:signin", this.onSdkSignin);
|
|
196
|
+
this.on("sdk:signin", (d) => this.onSdkSignin(d));
|
|
169
197
|
this.on("sdk:refresh", this.onSdkRefresh);
|
|
170
198
|
this.on("sdk:app", this.onSdkApp);
|
|
171
199
|
this.on("sdk:user", this.onSdkUser);
|
|
@@ -312,7 +340,10 @@ var WsClient = class {
|
|
|
312
340
|
console.log("[block-auth][WARNING] App not active");
|
|
313
341
|
return message;
|
|
314
342
|
}
|
|
315
|
-
if (!success)
|
|
343
|
+
if (!success) {
|
|
344
|
+
console.log("[block-auth][WARNING] signin failed: ", message);
|
|
345
|
+
return message;
|
|
346
|
+
}
|
|
316
347
|
const { accessToken, refreshToken } = message;
|
|
317
348
|
this._accessToken = accessToken;
|
|
318
349
|
this._refreshToken = refreshToken;
|
|
@@ -604,6 +635,19 @@ var BlockAuthProvider = class {
|
|
|
604
635
|
this._wsc = new WsClient(wsUrl, true, options.apiUrl);
|
|
605
636
|
this._apiKey = apiKey;
|
|
606
637
|
this._apiSecret = apiSecret;
|
|
638
|
+
this._browserToken = options.browserToken || "";
|
|
639
|
+
this._wsUrl = wsUrl;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Browser token used instead of the apiSecret on signin().
|
|
643
|
+
* Can be updated at runtime (e.g. after fetching a fresh one
|
|
644
|
+
* from your backend when the previous one expired).
|
|
645
|
+
*/
|
|
646
|
+
set browserToken(token) {
|
|
647
|
+
this._browserToken = token || "";
|
|
648
|
+
}
|
|
649
|
+
get browserToken() {
|
|
650
|
+
return this._browserToken;
|
|
607
651
|
}
|
|
608
652
|
/**
|
|
609
653
|
* Allow you to connect to the WebSocket API
|
|
@@ -631,19 +675,23 @@ var BlockAuthProvider = class {
|
|
|
631
675
|
return this._wsc.isConnected();
|
|
632
676
|
}
|
|
633
677
|
/**
|
|
634
|
-
* Allow to Signin
|
|
635
|
-
* to get the access token and refresh token.
|
|
678
|
+
* Allow to Signin to get the access token and refresh token.
|
|
636
679
|
* The access token is valid for 1 hour.
|
|
637
680
|
* The refresh token is valid for 2 hours.
|
|
638
681
|
* You can use the refresh token to get a new
|
|
639
682
|
* access token when the access token is expired using
|
|
640
683
|
* the method `refresh()`.
|
|
641
|
-
*
|
|
642
|
-
*
|
|
684
|
+
*
|
|
685
|
+
* Authenticates with the browser token when one was provided
|
|
686
|
+
* (options.browserToken or the browserToken setter); otherwise
|
|
687
|
+
* falls back to the API Key + API Secret pair. In browsers,
|
|
688
|
+
* always prefer the browser token: the apiSecret must never
|
|
689
|
+
* ship to the client.
|
|
643
690
|
* @returns Promise<any>
|
|
644
691
|
*/
|
|
645
692
|
async signin() {
|
|
646
693
|
if (!this.isConnected()) throw new Error("Not connected");
|
|
694
|
+
await this.resolveBrowserToken();
|
|
647
695
|
return new Promise((resolve) => {
|
|
648
696
|
this._wsc.ws.once("sdk:signin", (d) => {
|
|
649
697
|
const { success, message } = d;
|
|
@@ -651,12 +699,29 @@ var BlockAuthProvider = class {
|
|
|
651
699
|
if (!success) return;
|
|
652
700
|
this.wsc.onSdkSignin(d);
|
|
653
701
|
});
|
|
654
|
-
this.
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
})
|
|
702
|
+
const credentials = { apiKey: this._apiKey };
|
|
703
|
+
if (this._browserToken) {
|
|
704
|
+
credentials.browserToken = this._browserToken;
|
|
705
|
+
} else if (this._apiSecret) {
|
|
706
|
+
credentials.apiSecret = SHA256(this._apiSecret).toString();
|
|
707
|
+
}
|
|
708
|
+
this._wsc.emit("sdk:signin", credentials);
|
|
658
709
|
});
|
|
659
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* Last-resort credential for a browser: trade the publishable apiKey for a
|
|
713
|
+
* browser token. No-op when a credential is already configured - an
|
|
714
|
+
* explicit browserToken (from the app's own backend) or an apiSecret (a
|
|
715
|
+
* backend caller) always wins, and neither must be spent on a network
|
|
716
|
+
* round trip. Failure is silent (fetchBrowserToken resolves null), so a
|
|
717
|
+
* third-party app with no credential still reaches the server and gets its
|
|
718
|
+
* 'Credentials required' answer.
|
|
719
|
+
*/
|
|
720
|
+
async resolveBrowserToken() {
|
|
721
|
+
if (this._browserToken || this._apiSecret) return;
|
|
722
|
+
const token = await fetchBrowserToken(this._apiKey, this._wsUrl);
|
|
723
|
+
if (token) this._browserToken = token;
|
|
724
|
+
}
|
|
660
725
|
/**
|
|
661
726
|
* Allow to refresh the access token when it's expired.
|
|
662
727
|
* @param refreshToken - your refresh token
|
|
@@ -2186,7 +2251,7 @@ function createBlockAuthClient(config3) {
|
|
|
2186
2251
|
config3.apiKey,
|
|
2187
2252
|
config3.apiSecret,
|
|
2188
2253
|
config3.wsUrl ?? "wss://wss.block-auth.io",
|
|
2189
|
-
{ apiUrl: config3.apiUrl }
|
|
2254
|
+
{ apiUrl: config3.apiUrl, browserToken: config3.browserToken }
|
|
2190
2255
|
);
|
|
2191
2256
|
const evm = createEvmWallet({
|
|
2192
2257
|
walletConnectProjectId: config3.walletConnectProjectId,
|
|
@@ -2237,6 +2302,11 @@ function createBlockAuthClient(config3) {
|
|
|
2237
2302
|
connect: (...a) => provider.connect(...a),
|
|
2238
2303
|
disconnect: () => provider.disconnect(),
|
|
2239
2304
|
signin: () => provider.signin(),
|
|
2305
|
+
// Actualiza el browserToken en runtime (p.ej. tras pedir uno fresco al
|
|
2306
|
+
// backend cuando el anterior caducó).
|
|
2307
|
+
setBrowserToken: (token) => {
|
|
2308
|
+
provider.browserToken = token;
|
|
2309
|
+
},
|
|
2240
2310
|
refresh: (t2) => provider.refresh(t2),
|
|
2241
2311
|
app: (t2) => provider.app(t2),
|
|
2242
2312
|
user: (addr, t2) => provider.user(addr, t2),
|
|
@@ -2606,6 +2676,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2606
2676
|
};
|
|
2607
2677
|
const signinUnsigned = async () => {
|
|
2608
2678
|
setIsSignedFirstCheck(true);
|
|
2679
|
+
if (hookState.isSigning) return;
|
|
2609
2680
|
if (!hookState.isSigned) {
|
|
2610
2681
|
await _onError();
|
|
2611
2682
|
}
|