eocene-auth-sdk 0.3.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Added
6
+
7
+ - **Persistent session recovery.** `AuthClient.restoreSession()` and the matching
8
+ `useAuth()` method restore a usable local access token or use the project-managed
9
+ refresh cookie. Concurrent calls share one refresh flow. `<AuthProvider>` now
10
+ finishes startup recovery before setting `ready` to `true`.
11
+ - **Refresh-cookie logout.** `logout()` starts a credentialed request to revoke the
12
+ server-side refresh session before synchronously clearing local access state.
13
+
14
+ ### Changed
15
+
16
+ - Local tokens that are expired, malformed, missing `exp`, or within one minute of
17
+ expiry are refreshed rather than treated as a successful local restore.
18
+ - Session lifetime and refresh policy come from the server-side project
19
+ `sessionPolicy`; refresh and logout requests include cookies and `projectId`.
20
+
3
21
  ## 0.3.0
4
22
 
5
23
  ### Added
package/README.md CHANGED
@@ -68,17 +68,41 @@ read -rs NPM_TOKEN && export NPM_TOKEN && npm run publish:token
68
68
  The token is only exported to the npm subprocess; the temp `.npmrc` references
69
69
  `${NPM_TOKEN}`, never the literal value.
70
70
 
71
- ### Session expiry
71
+ ### Persistent sessions and startup recovery
72
72
 
73
- Sessions are short-lived and cannot be refreshed. Route the user back to login
74
- when one expires:
73
+ `AuthProvider` automatically restores the session on startup and sets `ready` to
74
+ `true` only after that attempt finishes. A local token with an address is used
75
+ when it is valid for more than one minute; otherwise the SDK calls
76
+ `POST {controlBaseUrl}/auth/session/refresh` with `credentials: 'include'` and the
77
+ configured `projectId`. A missing or rejected refresh cookie leaves the app signed
78
+ out without triggering a redirect. `restoreSession(): Promise<boolean>` is also
79
+ available from `useAuth()` for an explicit retry; concurrent calls share one flow.
80
+
81
+ ```tsx
82
+ const { ready, authenticated, restoreSession } = useAuth()
83
+ ```
84
+
85
+ Access tokens remain in `sessionStorage`. Refresh cookies are managed by Eocene
86
+ per project, and access/refresh lifetime and rotation are controlled by the
87
+ server-side project `sessionPolicy`, not by SDK configuration. The refresh and
88
+ logout endpoints require credentialed CORS: allow the exact application origin and
89
+ `Access-Control-Allow-Credentials` (a wildcard origin is insufficient). Cross-site
90
+ cookies also require browser-compatible `Secure` / `SameSite=None` settings and may
91
+ be blocked by third-party-cookie policy; a same-site proxy avoids that restriction.
92
+
93
+ ### Session expiry and logout
94
+
95
+ A 401 from an access-token request clears local access state and flips
96
+ `authenticated` to `false`, but does not revoke the refresh cookie. Route the user
97
+ back to login when appropriate:
75
98
 
76
99
  ```tsx
77
100
  <AuthProvider config={config} onSessionExpired={() => nav('/login')}>
78
101
  ```
79
102
 
80
- By the time `onSessionExpired` fires, local state is already cleared and
81
- `authenticated` has flipped to `false`.
103
+ `logout()` remains synchronous for local state. It starts the credentialed remote
104
+ revocation request first, then clears the access token and wallet state; the remote
105
+ request is best-effort and failures are not surfaced to the synchronous caller.
82
106
 
83
107
  ### Multi-chain
84
108
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ComponentType, ReactNode, SVGProps } from 'react';
3
3
 
4
- type WaasErrorCode = 'password_invalid' | 'password_locked' | 'password_required' | 'password_too_short' | 'plan_user_limit' | 'plan_project_limit' | 'session_expired' | 'rate_limited' | 'unknown';
4
+ type WaasErrorCode = 'password_invalid' | 'password_locked' | 'password_required' | 'password_too_short' | 'password_not_enabled' | 'recovery_contact_required' | 'invalid_recovery_email' | 'reset_cooldown' | 'reset_code_invalid' | 'reset_code_expired' | 'reset_code_locked' | 'reset_channel_unavailable' | 'reset_delivery_failed' | 'plan_user_limit' | 'plan_project_limit' | 'session_expired' | 'rate_limited' | 'unknown';
5
5
  type Json = Record<string, unknown>;
6
6
  declare class WaasHttpError extends Error {
7
7
  readonly status: number;
@@ -47,12 +47,48 @@ type WaasSignApprovalResult = boolean | {
47
47
  password?: string;
48
48
  };
49
49
 
50
+ type WaasPasswordResetChannel = 'email' | 'telegram';
51
+ type WaasWalletRecoveryStatus = {
52
+ hasRecovery: boolean;
53
+ channels: WaasPasswordResetChannel[];
54
+ defaultChannel: WaasPasswordResetChannel | null;
55
+ maskedEmail: string | null;
56
+ maskedTelegram: string | null;
57
+ };
58
+ type WaasPasswordResetRequestResult = {
59
+ ok: true;
60
+ channel: WaasPasswordResetChannel;
61
+ availableChannels?: WaasPasswordResetChannel[];
62
+ maskedTarget: string;
63
+ expiresInSec: number;
64
+ cooldownSec: number;
65
+ devCode?: string;
66
+ };
67
+ type WaasPasswordResetConfirmResult = {
68
+ ok: true;
69
+ passwordEnabled: boolean;
70
+ };
71
+ type WaasRecoveryEmailBindRequestResult = {
72
+ ok: true;
73
+ channel: 'email';
74
+ maskedTarget: string;
75
+ expiresInSec: number;
76
+ cooldownSec: number;
77
+ devCode?: string;
78
+ };
79
+ type WaasRecoveryEmailBindConfirmResult = {
80
+ ok: true;
81
+ recoveryEmail: string;
82
+ maskedEmail: string;
83
+ recovery?: WaasWalletRecoveryStatus;
84
+ };
50
85
  type WaasClientOptions = {
51
86
  controlBaseUrl: string;
52
87
  /**
53
- * Called when the control-plane rejects the session (401). Sessions expire
54
- * after an hour and cannot be refreshed, so the host has to send the user
55
- * through login again.
88
+ * Called when the control-plane rejects the session (401). Access tokens
89
+ * default to one hour. POST /auth/session/refresh can rotate the project
90
+ * refresh cookie or slide a still-valid Bearer token; an expired Bearer
91
+ * token cannot be refreshed.
56
92
  */
57
93
  onSessionExpired?: () => void;
58
94
  };
@@ -64,6 +100,7 @@ type DeviceState = {
64
100
  address?: string;
65
101
  epoch?: number;
66
102
  passwordEnabled?: boolean;
103
+ recovery?: WaasWalletRecoveryStatus;
67
104
  };
68
105
  /**
69
106
  * Browser/Node SDK for custodial eocene-worker.
@@ -96,15 +133,26 @@ declare class WaasClient {
96
133
  address: string;
97
134
  epoch: number;
98
135
  passwordEnabled: boolean;
136
+ recovery?: WaasWalletRecoveryStatus;
99
137
  } | null;
100
138
  }>;
101
- ensureWallet(): Promise<{
139
+ ensureWallet(opts?: {
140
+ telegramInitData?: string;
141
+ }): Promise<{
102
142
  walletId: string;
103
143
  address: string;
104
144
  passwordEnabled: boolean;
145
+ recovery?: WaasWalletRecoveryStatus;
105
146
  }>;
106
- setPassword(password: string, currentPassword?: string): Promise<void>;
147
+ setPassword(password: string, currentPassword?: string, recoveryBind?: {
148
+ recoveryEmail?: string;
149
+ recoveryCode?: string;
150
+ }): Promise<void>;
151
+ requestRecoveryEmailBind(email: string): Promise<WaasRecoveryEmailBindRequestResult>;
152
+ confirmRecoveryEmailBind(code: string, email?: string): Promise<WaasRecoveryEmailBindConfirmResult>;
107
153
  clearPassword(password: string): Promise<void>;
154
+ requestPasswordReset(channel?: WaasPasswordResetChannel): Promise<WaasPasswordResetRequestResult>;
155
+ confirmPasswordReset(code: string, newPassword?: string | null): Promise<WaasPasswordResetConfirmResult>;
108
156
  signMessage(message: string, opts?: {
109
157
  password?: string;
110
158
  }): Promise<{
@@ -174,6 +222,7 @@ type AuthSdkLabels = {
174
222
  signReject: string;
175
223
  signPasswordLabel: string;
176
224
  signPasswordPlaceholder: string;
225
+ signForgotPassword: string;
177
226
  signSetPasswordOptional: string;
178
227
  signSetPasswordButton: string;
179
228
  signPasswordIncorrect: string;
@@ -204,6 +253,29 @@ type AuthSdkLabels = {
204
253
  passwordSettingsDisabledHint: string;
205
254
  passwordSettingsChange: string;
206
255
  passwordSettingsClear: string;
256
+ passwordSettingsReset: string;
257
+ passwordSettingsResetHint: string;
258
+ passwordSettingsSendCode: string;
259
+ passwordSettingsSendingCode: string;
260
+ passwordSettingsResendCode: string;
261
+ passwordSettingsResendCooldown: string;
262
+ passwordSettingsCodeSentEmail: string;
263
+ passwordSettingsCodeSentTelegram: string;
264
+ passwordSettingsCodeLabel: string;
265
+ passwordSettingsCodePlaceholder: string;
266
+ passwordSettingsResetConfirm: string;
267
+ passwordSettingsResetClearConfirm: string;
268
+ passwordSettingsResetCodeInvalid: string;
269
+ passwordSettingsResetCodeExpired: string;
270
+ passwordSettingsResetCodeLocked: string;
271
+ passwordSettingsResetCooldown: string;
272
+ passwordSettingsResetChannelUnavailable: string;
273
+ passwordSettingsResetDeliveryFailed: string;
274
+ passwordSettingsBindEmailLabel: string;
275
+ passwordSettingsBindEmailPlaceholder: string;
276
+ passwordSettingsBindEmailHint: string;
277
+ passwordSettingsRecoveryRequired: string;
278
+ passwordSettingsInvalidRecoveryEmail: string;
207
279
  passwordSettingsCurrentLabel: string;
208
280
  passwordSettingsCurrentPlaceholder: string;
209
281
  passwordSettingsNewLabel: string;
@@ -233,15 +305,36 @@ type SignApprovalProps = {
233
305
  onReject: () => void;
234
306
  /** Shown when wallet has no password yet — opens SetPasswordModal. */
235
307
  onOpenSetPassword?: () => void;
308
+ /** Shown when wallet has a password — opens SetPasswordModal in reset mode. */
309
+ onOpenResetPassword?: () => void;
310
+ };
311
+ type PasswordSettingsMode = 'set' | 'clear' | 'reset';
312
+ type PasswordResetSentInfo = {
313
+ channel: WaasPasswordResetChannel;
314
+ maskedTarget: string;
315
+ cooldownSec: number;
316
+ sentAt: number;
236
317
  };
237
318
  type PasswordSettingsProps = {
238
319
  open: boolean;
239
320
  passwordEnabled: boolean;
321
+ initialMode?: PasswordSettingsMode;
322
+ recovery?: WaasWalletRecoveryStatus | null;
240
323
  labels: AuthSdkLabels;
241
324
  busy: boolean;
325
+ sendingResetCode?: boolean;
326
+ resetSent?: PasswordResetSentInfo | null;
327
+ sendingRecoveryBindCode?: boolean;
328
+ recoveryBindSent?: PasswordResetSentInfo | null;
242
329
  error: string | null;
243
- onSubmitSet: (password: string, currentPassword?: string) => Promise<void>;
330
+ onSubmitSet: (password: string, currentPassword?: string, recoveryBind?: {
331
+ recoveryEmail?: string;
332
+ recoveryCode?: string;
333
+ }) => Promise<void>;
244
334
  onSubmitClear: (password: string) => Promise<void>;
335
+ onRequestResetCode?: (channel?: WaasPasswordResetChannel) => Promise<void>;
336
+ onSubmitReset?: (code: string, newPassword?: string | null) => Promise<void>;
337
+ onRequestRecoveryEmailBind?: (email: string) => Promise<void>;
245
338
  onClose: () => void;
246
339
  };
247
340
  type LoginPanelProps = {
@@ -294,6 +387,7 @@ type AuthUser = {
294
387
  address?: string;
295
388
  walletId?: string;
296
389
  passwordEnabled?: boolean;
390
+ recovery?: WaasWalletRecoveryStatus;
297
391
  };
298
392
  type AuthLoginResult = {
299
393
  address: string;
@@ -308,8 +402,8 @@ type AuthClientOptions = {
308
402
  config: AuthSdkConfig;
309
403
  requestApproval?: (req: WaasSignApprovalRequest) => Promise<WaasSignApprovalResult>;
310
404
  /**
311
- * Called when the control-plane rejects the session (401). Sessions last one
312
- * hour and cannot be refreshed, so hosts should route the user back to login.
405
+ * Called when the control-plane rejects the session (401). Access-token TTL is
406
+ * project-configurable; hosts may restore through the persistent refresh cookie.
313
407
  */
314
408
  onSessionExpired?: () => void;
315
409
  /** Initial EVM chain for the EIP-1193 provider. Default 1 (Ethereum). */
@@ -323,6 +417,7 @@ declare class AuthClient {
323
417
  private provider;
324
418
  private chainId;
325
419
  private active;
420
+ private restorePromise;
326
421
  private requestApproval?;
327
422
  private onSessionExpired?;
328
423
  constructor(options: AuthClientOptions);
@@ -355,10 +450,21 @@ declare class AuthClient {
355
450
  completeOAuthFromHash(hashOrSearch: string): Promise<AuthLoginResult>;
356
451
  takeOAuthReturnPath(): string | null;
357
452
  markActive(on: boolean): void;
453
+ private restoreLocalSession;
454
+ private localSessionNeedsRefresh;
358
455
  restoreIfNeeded(): boolean;
456
+ restoreSession(): Promise<boolean>;
457
+ private performRestoreSession;
359
458
  logout(): void;
360
- setPassword(password: string, currentPassword?: string): Promise<void>;
459
+ setPassword(password: string, currentPassword?: string, recoveryBind?: {
460
+ recoveryEmail?: string;
461
+ recoveryCode?: string;
462
+ }): Promise<void>;
361
463
  clearPassword(password: string): Promise<void>;
464
+ requestRecoveryEmailBind(email: string): Promise<WaasRecoveryEmailBindRequestResult>;
465
+ confirmRecoveryEmailBind(code: string, email?: string): Promise<WaasRecoveryEmailBindConfirmResult>;
466
+ requestPasswordReset(channel?: WaasPasswordResetChannel): Promise<WaasPasswordResetRequestResult>;
467
+ confirmPasswordReset(code: string, newPassword?: string | null): Promise<WaasPasswordResetConfirmResult>;
362
468
  }
363
469
 
364
470
  /**
@@ -388,6 +494,8 @@ type AuthContextValue = {
388
494
  address: string | undefined;
389
495
  user: AuthUser | null;
390
496
  passwordEnabled: boolean;
497
+ /** Increments whenever setPassword, clearPassword, or confirmPasswordReset succeeds. */
498
+ passwordVersion: number;
391
499
  login: (args: {
392
500
  method: AuthSocialMethod;
393
501
  }) => Promise<AuthLoginResult>;
@@ -396,13 +504,21 @@ type AuthContextValue = {
396
504
  logout: () => void;
397
505
  markActive: (on: boolean) => void;
398
506
  restoreIfNeeded: () => boolean;
507
+ restoreSession: () => Promise<boolean>;
399
508
  completeOAuthFromHash: (hashOrSearch: string) => Promise<AuthLoginResult>;
400
509
  takeOAuthReturnPath: () => string | null;
401
510
  getProvider: () => WaasEip1193Provider;
402
511
  getConnectorShim: () => AuthConnectorShim;
403
- setPassword: (password: string, currentPassword?: string) => Promise<void>;
512
+ setPassword: (password: string, currentPassword?: string, recoveryBind?: {
513
+ recoveryEmail?: string;
514
+ recoveryCode?: string;
515
+ }) => Promise<void>;
404
516
  clearPassword: (password: string) => Promise<void>;
405
- openPasswordSettings: () => void;
517
+ requestPasswordReset: (channel?: WaasPasswordResetChannel) => Promise<WaasPasswordResetRequestResult>;
518
+ confirmPasswordReset: (code: string, newPassword?: string | null) => Promise<WaasPasswordResetConfirmResult>;
519
+ requestRecoveryEmailBind: (email: string) => Promise<WaasRecoveryEmailBindRequestResult>;
520
+ confirmRecoveryEmailBind: (code: string, email?: string) => Promise<WaasRecoveryEmailBindConfirmResult>;
521
+ openPasswordSettings: (initialMode?: PasswordSettingsMode) => void;
406
522
  closePasswordSettings: () => void;
407
523
  client: AuthClient;
408
524
  labels: AuthSdkLabels;
@@ -415,9 +531,9 @@ type AuthProviderProps = {
415
531
  components?: AuthSdkComponents;
416
532
  onRequestSignApproval?: (req: WaasSignApprovalRequest) => Promise<WaasSignApprovalResult>;
417
533
  /**
418
- * Called when the session expires (1h, not refreshable). The provider has
419
- * already cleared local state and `authenticated` has flipped to false; use
420
- * this to route the user back to your login screen.
534
+ * Called when the session is rejected (401). The provider has already cleared
535
+ * local access state. Hosts may restore through the persistent refresh cookie;
536
+ * only a failed restore requires the user to connect the signer again.
421
537
  */
422
538
  onSessionExpired?: () => void;
423
539
  children: ReactNode;
@@ -446,9 +562,9 @@ declare function AuthOAuthCallback({ hashOrSearch, onWalletReady, children, onBa
446
562
  /** Build AuthSdkConfig from Vite `VITE_EOCENE_*` (and unprefixed / legacy WAAS aliases). */
447
563
  declare function authConfigFromViteEnv(env?: object): AuthSdkConfig;
448
564
 
449
- declare function SignConfirmModal({ open, request, labels, password, busy, error, locked, onPasswordChange, onApprove, onReject, onOpenSetPassword, }: SignApprovalProps): react.JSX.Element | null;
565
+ declare function SignConfirmModal({ open, request, labels, password, busy, error, locked, onPasswordChange, onApprove, onReject, onOpenSetPassword, onOpenResetPassword, }: SignApprovalProps): react.JSX.Element | null;
450
566
 
451
- declare function SetPasswordModal({ open, passwordEnabled, labels, busy, error, onSubmitSet, onSubmitClear, onClose, }: PasswordSettingsProps): react.JSX.Element | null;
567
+ declare function SetPasswordModal({ open, passwordEnabled, initialMode, recovery, labels, busy, sendingResetCode, resetSent, sendingRecoveryBindCode, recoveryBindSent, error, onSubmitSet, onSubmitClear, onRequestResetCode, onSubmitReset, onRequestRecoveryEmailBind, onClose, }: PasswordSettingsProps): react.JSX.Element | null;
452
568
 
453
569
  /**
454
570
  * EOCENE Research mark and lockup. Inline SVG (no asset fetch); transparent
@@ -463,4 +579,4 @@ declare function EoceneWordmark({ className, markSize, }: {
463
579
  markSize?: number;
464
580
  }): react.JSX.Element;
465
581
 
466
- export { AuthClient, type AuthConnectorShim, type AuthContextValue, AuthLoginPanel, type AuthLoginResult, AuthOAuthCallback, type AuthOAuthCallbackProps, AuthProvider, type AuthProviderProps, type AuthSdkComponents, type AuthSdkConfig, type AuthSdkLabels, type AuthSocialMethod, type AuthUser, DEFAULT_CONTROL_BASE_URL, DEFAULT_LABELS, DEFAULT_OAUTH_PENDING_KEY, DEFAULT_STORAGE_KEY, EoceneMark, EoceneWordmark, type LoginPanelProps, type PasswordSettingsProps, SetPasswordModal, type SignApprovalProps, SignConfirmModal, type WaasEip1193Provider, WaasHttpError, type WaasSignApprovalRequest, type WaasSignApprovalResult, type WaasSignAuthError, authConfigFromViteEnv, createAuthEip1193Provider, getTelegramInitData, isTelegramMiniApp, isWaasHttpError, resolveControlAbsoluteUrl, useAuthContext as useAuth };
582
+ export { AuthClient, type AuthConnectorShim, type AuthContextValue, AuthLoginPanel, type AuthLoginResult, AuthOAuthCallback, type AuthOAuthCallbackProps, AuthProvider, type AuthProviderProps, type AuthSdkComponents, type AuthSdkConfig, type AuthSdkLabels, type AuthSocialMethod, type AuthUser, DEFAULT_CONTROL_BASE_URL, DEFAULT_LABELS, DEFAULT_OAUTH_PENDING_KEY, DEFAULT_STORAGE_KEY, EoceneMark, EoceneWordmark, type LoginPanelProps, type PasswordResetSentInfo, type PasswordSettingsMode, type PasswordSettingsProps, SetPasswordModal, type SignApprovalProps, SignConfirmModal, type WaasEip1193Provider, WaasHttpError, type WaasPasswordResetChannel, type WaasPasswordResetConfirmResult, type WaasPasswordResetRequestResult, type WaasRecoveryEmailBindConfirmResult, type WaasRecoveryEmailBindRequestResult, type WaasSignApprovalRequest, type WaasSignApprovalResult, type WaasSignAuthError, type WaasWalletRecoveryStatus, authConfigFromViteEnv, createAuthEip1193Provider, getTelegramInitData, isTelegramMiniApp, isWaasHttpError, resolveControlAbsoluteUrl, useAuthContext as useAuth };