eocene-auth-sdk 0.5.1 → 0.5.2
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.d.ts +1 -0
- package/dist/index.js +97 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -457,6 +457,7 @@ declare class AuthClient {
|
|
|
457
457
|
private localSessionNeedsRefresh;
|
|
458
458
|
restoreIfNeeded(): boolean;
|
|
459
459
|
restoreSession(): Promise<boolean>;
|
|
460
|
+
private bootstrapRefreshToken;
|
|
460
461
|
private performRestoreSession;
|
|
461
462
|
logout(): void;
|
|
462
463
|
setPassword(password: string, currentPassword?: string, recoveryBind?: {
|
package/dist/index.js
CHANGED
|
@@ -3078,24 +3078,67 @@ var WaasClient = class {
|
|
|
3078
3078
|
};
|
|
3079
3079
|
|
|
3080
3080
|
// src/client/storage.ts
|
|
3081
|
+
function safeGet(storage, key) {
|
|
3082
|
+
if (!storage) return null;
|
|
3083
|
+
try {
|
|
3084
|
+
return storage.getItem(key);
|
|
3085
|
+
} catch {
|
|
3086
|
+
return null;
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
function safeSet(storage, key, value) {
|
|
3090
|
+
if (!storage) return false;
|
|
3091
|
+
try {
|
|
3092
|
+
storage.setItem(key, value);
|
|
3093
|
+
return true;
|
|
3094
|
+
} catch {
|
|
3095
|
+
return false;
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
function safeRemove(storage, key) {
|
|
3099
|
+
if (!storage) return;
|
|
3100
|
+
try {
|
|
3101
|
+
storage.removeItem(key);
|
|
3102
|
+
} catch {
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
function localOrNull() {
|
|
3106
|
+
return typeof localStorage !== "undefined" ? localStorage : void 0;
|
|
3107
|
+
}
|
|
3108
|
+
function sessionOrNull() {
|
|
3109
|
+
return typeof sessionStorage !== "undefined" ? sessionStorage : void 0;
|
|
3110
|
+
}
|
|
3081
3111
|
function loadPersistedState(storageKey = DEFAULT_STORAGE_KEY) {
|
|
3082
|
-
|
|
3112
|
+
const localRaw = safeGet(localOrNull(), storageKey);
|
|
3113
|
+
if (localRaw) {
|
|
3114
|
+
try {
|
|
3115
|
+
return JSON.parse(localRaw);
|
|
3116
|
+
} catch {
|
|
3117
|
+
safeRemove(localOrNull(), storageKey);
|
|
3118
|
+
}
|
|
3119
|
+
}
|
|
3120
|
+
const sessionRaw = safeGet(sessionOrNull(), storageKey);
|
|
3121
|
+
if (!sessionRaw) return null;
|
|
3083
3122
|
try {
|
|
3084
|
-
const
|
|
3085
|
-
if (
|
|
3086
|
-
|
|
3123
|
+
const parsed = JSON.parse(sessionRaw);
|
|
3124
|
+
if (safeSet(localOrNull(), storageKey, sessionRaw)) {
|
|
3125
|
+
safeRemove(sessionOrNull(), storageKey);
|
|
3126
|
+
}
|
|
3127
|
+
return parsed;
|
|
3087
3128
|
} catch {
|
|
3129
|
+
safeRemove(sessionOrNull(), storageKey);
|
|
3088
3130
|
return null;
|
|
3089
3131
|
}
|
|
3090
3132
|
}
|
|
3091
|
-
function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY) {
|
|
3092
|
-
if (typeof sessionStorage === "undefined") return;
|
|
3133
|
+
function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY, opts) {
|
|
3093
3134
|
if (!state?.sessionToken) {
|
|
3094
|
-
|
|
3135
|
+
clearPersistedState(storageKey);
|
|
3095
3136
|
return;
|
|
3096
3137
|
}
|
|
3138
|
+
const existingRefresh = opts?.refreshToken !== void 0 ? opts.refreshToken : loadPersistedState(storageKey)?.refreshToken;
|
|
3097
3139
|
const payload = {
|
|
3098
3140
|
sessionToken: state.sessionToken,
|
|
3141
|
+
...existingRefresh ? { refreshToken: existingRefresh } : {},
|
|
3099
3142
|
principalId: state.principalId,
|
|
3100
3143
|
email: state.email,
|
|
3101
3144
|
walletId: state.walletId,
|
|
@@ -3103,11 +3146,16 @@ function persistDeviceState(state, storageKey = DEFAULT_STORAGE_KEY) {
|
|
|
3103
3146
|
epoch: state.epoch,
|
|
3104
3147
|
passwordEnabled: state.passwordEnabled
|
|
3105
3148
|
};
|
|
3106
|
-
|
|
3149
|
+
const serialized = JSON.stringify(payload);
|
|
3150
|
+
if (safeSet(localOrNull(), storageKey, serialized)) {
|
|
3151
|
+
safeRemove(sessionOrNull(), storageKey);
|
|
3152
|
+
} else {
|
|
3153
|
+
safeSet(sessionOrNull(), storageKey, serialized);
|
|
3154
|
+
}
|
|
3107
3155
|
}
|
|
3108
3156
|
function clearPersistedState(storageKey = DEFAULT_STORAGE_KEY) {
|
|
3109
|
-
|
|
3110
|
-
|
|
3157
|
+
safeRemove(localOrNull(), storageKey);
|
|
3158
|
+
safeRemove(sessionOrNull(), storageKey);
|
|
3111
3159
|
}
|
|
3112
3160
|
|
|
3113
3161
|
// src/client/telegramMiniApp.ts
|
|
@@ -3227,8 +3275,12 @@ var AuthClient = class {
|
|
|
3227
3275
|
getProvider: async () => this.getProvider()
|
|
3228
3276
|
};
|
|
3229
3277
|
}
|
|
3230
|
-
persist() {
|
|
3231
|
-
persistDeviceState(
|
|
3278
|
+
persist(refreshToken) {
|
|
3279
|
+
persistDeviceState(
|
|
3280
|
+
this.client.getState(),
|
|
3281
|
+
this.storageKey,
|
|
3282
|
+
refreshToken !== void 0 ? { refreshToken } : void 0
|
|
3283
|
+
);
|
|
3232
3284
|
}
|
|
3233
3285
|
async fetchOAuthProviders() {
|
|
3234
3286
|
const base = this.config.controlBaseUrl.replace(/\/$/, "");
|
|
@@ -3364,6 +3416,7 @@ var AuthClient = class {
|
|
|
3364
3416
|
this.persist();
|
|
3365
3417
|
this.active = true;
|
|
3366
3418
|
this.provider = null;
|
|
3419
|
+
this.bootstrapRefreshToken(decodeURIComponent(sessionToken));
|
|
3367
3420
|
if (typeof sessionStorage !== "undefined") {
|
|
3368
3421
|
sessionStorage.removeItem(this.oauthPendingKey);
|
|
3369
3422
|
}
|
|
@@ -3416,6 +3469,24 @@ var AuthClient = class {
|
|
|
3416
3469
|
});
|
|
3417
3470
|
return this.restorePromise;
|
|
3418
3471
|
}
|
|
3472
|
+
bootstrapRefreshToken(sessionToken) {
|
|
3473
|
+
if (!this.config.projectId || !sessionToken) return;
|
|
3474
|
+
const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
|
|
3475
|
+
void fetch(`${base}/auth/session/refresh`, {
|
|
3476
|
+
method: "POST",
|
|
3477
|
+
credentials: "include",
|
|
3478
|
+
headers: {
|
|
3479
|
+
"content-type": "application/json",
|
|
3480
|
+
authorization: `Bearer ${sessionToken}`
|
|
3481
|
+
},
|
|
3482
|
+
body: JSON.stringify({ projectId: this.config.projectId })
|
|
3483
|
+
}).then((res) => res.ok ? res.json() : null).then((result) => {
|
|
3484
|
+
if (result?.refreshToken && this.active) {
|
|
3485
|
+
this.persist(result.refreshToken);
|
|
3486
|
+
}
|
|
3487
|
+
}).catch(() => {
|
|
3488
|
+
});
|
|
3489
|
+
}
|
|
3419
3490
|
async performRestoreSession() {
|
|
3420
3491
|
const restoreLocalIfCurrent = () => this.restoreLocalSession() && !this.localSessionNeedsRefresh();
|
|
3421
3492
|
if (restoreLocalIfCurrent()) return true;
|
|
@@ -3424,21 +3495,26 @@ var AuthClient = class {
|
|
|
3424
3495
|
try {
|
|
3425
3496
|
const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
|
|
3426
3497
|
const url = `${base}/auth/session/refresh`;
|
|
3427
|
-
const
|
|
3498
|
+
const saved = loadPersistedState(this.storageKey);
|
|
3499
|
+
const storedRefresh = saved?.refreshToken;
|
|
3500
|
+
const body = JSON.stringify({
|
|
3501
|
+
projectId: this.config.projectId,
|
|
3502
|
+
...storedRefresh ? { refreshToken: storedRefresh } : {}
|
|
3503
|
+
});
|
|
3428
3504
|
let res = await fetch(url, {
|
|
3429
3505
|
method: "POST",
|
|
3430
3506
|
credentials: "include",
|
|
3431
3507
|
headers: { "content-type": "application/json" },
|
|
3432
3508
|
body
|
|
3433
3509
|
});
|
|
3434
|
-
const legacyToken =
|
|
3510
|
+
const legacyToken = saved?.sessionToken;
|
|
3435
3511
|
const legacyExpMs = legacyToken ? sessionTokenExpMs(legacyToken) : null;
|
|
3436
3512
|
if (res.status === 401 && legacyToken && legacyExpMs != null && legacyExpMs > Date.now()) {
|
|
3437
3513
|
res = await fetch(url, {
|
|
3438
3514
|
method: "POST",
|
|
3439
3515
|
credentials: "include",
|
|
3440
3516
|
headers: { "content-type": "application/json", authorization: `Bearer ${legacyToken}` },
|
|
3441
|
-
body
|
|
3517
|
+
body: JSON.stringify({ projectId: this.config.projectId })
|
|
3442
3518
|
});
|
|
3443
3519
|
}
|
|
3444
3520
|
const result = await res.json().catch(() => ({}));
|
|
@@ -3447,7 +3523,7 @@ var AuthClient = class {
|
|
|
3447
3523
|
await this.client.ensureWallet({
|
|
3448
3524
|
telegramInitData: getTelegramInitData() ?? void 0
|
|
3449
3525
|
});
|
|
3450
|
-
this.persist();
|
|
3526
|
+
this.persist(result.refreshToken);
|
|
3451
3527
|
this.active = true;
|
|
3452
3528
|
this.provider = null;
|
|
3453
3529
|
return true;
|
|
@@ -3463,13 +3539,17 @@ var AuthClient = class {
|
|
|
3463
3539
|
}
|
|
3464
3540
|
logout() {
|
|
3465
3541
|
const base = resolveControlAbsoluteUrl(this.config.controlBaseUrl);
|
|
3542
|
+
const storedRefresh = loadPersistedState(this.storageKey)?.refreshToken;
|
|
3466
3543
|
try {
|
|
3467
3544
|
void fetch(`${base}/auth/session/logout`, {
|
|
3468
3545
|
method: "POST",
|
|
3469
3546
|
credentials: "include",
|
|
3470
3547
|
keepalive: true,
|
|
3471
3548
|
headers: { "content-type": "application/json" },
|
|
3472
|
-
body: JSON.stringify({
|
|
3549
|
+
body: JSON.stringify({
|
|
3550
|
+
projectId: this.config.projectId,
|
|
3551
|
+
...storedRefresh ? { refreshToken: storedRefresh } : {}
|
|
3552
|
+
})
|
|
3473
3553
|
}).catch(() => {
|
|
3474
3554
|
});
|
|
3475
3555
|
} finally {
|