@ollaid/native-sso 2.7.2 → 2.7.3

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 CHANGED
@@ -7108,6 +7108,27 @@ function isRawStorageKey(key) {
7108
7108
  function getLegacyStorageKey(key) {
7109
7109
  return LEGACY_STORAGE_KEYS[key] || null;
7110
7110
  }
7111
+ function normalizeStorageValue(value) {
7112
+ if (typeof value === "string") return value;
7113
+ if (value === null || value === void 0) return "";
7114
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
7115
+ return String(value);
7116
+ }
7117
+ try {
7118
+ const serialized = JSON.stringify(value);
7119
+ return serialized ?? "";
7120
+ } catch {
7121
+ return String(value);
7122
+ }
7123
+ }
7124
+ function safeJSONStringify(value) {
7125
+ try {
7126
+ const serialized = JSON.stringify(value);
7127
+ return serialized ?? "null";
7128
+ } catch {
7129
+ return "null";
7130
+ }
7131
+ }
7111
7132
  function migrateLegacyValue(base, key, legacyKey, rawValue) {
7112
7133
  base.setItem(key, encryptStorageValue(rawValue));
7113
7134
  base.removeItem(legacyKey);
@@ -7116,31 +7137,44 @@ function migrateLegacyValue(base, key, legacyKey, rawValue) {
7116
7137
  function getStorageEncryptionSecret() {
7117
7138
  var _a;
7118
7139
  const storage = rawStorageAdapter;
7119
- let seed = storage.getItem("native_sso_storage_seed");
7140
+ let seed = storage.getItem(STORAGE_SEED_KEY);
7120
7141
  if (!seed) {
7121
7142
  seed = `seed_${Date.now()}_${Math.random().toString(36).substring(2, 15)}_${Math.random().toString(36).substring(2, 15)}`;
7122
- storage.setItem("native_sso_storage_seed", seed);
7143
+ storage.setItem(STORAGE_SEED_KEY, seed);
7123
7144
  }
7124
7145
  const origin = typeof window !== "undefined" && ((_a = window.location) == null ? void 0 : _a.origin) ? window.location.origin : "unknown-origin";
7125
7146
  const prefix = config.configPrefix || "iam";
7126
7147
  return `${STORAGE_ENCRYPTION_SECRET_PREFIX}::${origin}::${prefix}::${seed}`;
7127
7148
  }
7128
7149
  function encryptStorageValue(value) {
7129
- const secret = getStorageEncryptionSecret();
7130
- const ciphertext = cryptoJsExports.AES.encrypt(value, secret).toString();
7131
- return `${STORAGE_ENCRYPTION_PREFIX}${ciphertext}`;
7150
+ const normalizedValue = normalizeStorageValue(value);
7151
+ try {
7152
+ const secret = getStorageEncryptionSecret();
7153
+ const ciphertext = cryptoJsExports.AES.encrypt(normalizedValue, secret).toString();
7154
+ return `${STORAGE_ENCRYPTION_PREFIX}${ciphertext}`;
7155
+ } catch (error) {
7156
+ if (isDebugMode()) {
7157
+ console.warn("⚠️ [native-sso] Encryption storage fallback to plain value", error);
7158
+ }
7159
+ return normalizedValue;
7160
+ }
7132
7161
  }
7133
7162
  function decryptStorageValue(value) {
7134
- if (!value.startsWith(STORAGE_ENCRYPTION_PREFIX)) {
7135
- return value;
7163
+ const normalizedValue = normalizeStorageValue(value);
7164
+ if (!normalizedValue.startsWith(STORAGE_ENCRYPTION_PREFIX)) {
7165
+ return normalizedValue;
7136
7166
  }
7137
- const secret = getStorageEncryptionSecret();
7138
- const ciphertext = value.slice(STORAGE_ENCRYPTION_PREFIX.length);
7167
+ const ciphertext = normalizedValue.slice(STORAGE_ENCRYPTION_PREFIX.length);
7168
+ if (!ciphertext) return null;
7139
7169
  try {
7170
+ const secret = getStorageEncryptionSecret();
7140
7171
  const bytes = cryptoJsExports.AES.decrypt(ciphertext, secret);
7141
7172
  const plaintext = bytes.toString(cryptoJsExports.enc.Utf8);
7142
7173
  return plaintext || null;
7143
- } catch {
7174
+ } catch (error) {
7175
+ if (isDebugMode()) {
7176
+ console.warn("⚠️ [native-sso] Failed to decrypt storage value, clearing corrupted entry", error);
7177
+ }
7144
7178
  return null;
7145
7179
  }
7146
7180
  }
@@ -7156,8 +7190,9 @@ function createEncryptedStorageAdapter(base) {
7156
7190
  if (decrypted !== null) {
7157
7191
  return decrypted;
7158
7192
  }
7159
- if (rawValue.length > 0) {
7160
- base.setItem(key, encryptStorageValue(rawValue));
7193
+ if (normalizeStorageValue(rawValue).startsWith(STORAGE_ENCRYPTION_PREFIX)) {
7194
+ base.removeItem(key);
7195
+ return null;
7161
7196
  }
7162
7197
  return rawValue;
7163
7198
  }
@@ -7178,7 +7213,7 @@ function createEncryptedStorageAdapter(base) {
7178
7213
  },
7179
7214
  setItem: (key, value) => {
7180
7215
  if (isRawStorageKey(key)) {
7181
- base.setItem(key, value);
7216
+ base.setItem(key, normalizeStorageValue(value));
7182
7217
  return;
7183
7218
  }
7184
7219
  base.setItem(key, encryptStorageValue(value));
@@ -7230,6 +7265,7 @@ const getIamApiBaseUrl = () => {
7230
7265
  };
7231
7266
  const DEVICE_ID_KEY = "sso_device_id";
7232
7267
  const SESSION_UUID_KEY = "sso_session_uuid";
7268
+ const STORAGE_SEED_KEY = "native_sso_storage_seed";
7233
7269
  function generateUuid() {
7234
7270
  const globalCrypto = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
7235
7271
  if (globalCrypto && typeof globalCrypto.randomUUID === "function") {
@@ -7337,8 +7373,26 @@ const PROFILE_STORAGE = {
7337
7373
  IMAGE_LAST_CHECK: "sso_image_last_check",
7338
7374
  IMAGE_RECHECK_AT: "sso_image_recheck_at"
7339
7375
  };
7376
+ const ALL_SESSION_STORAGE_KEYS = [
7377
+ STORAGE.AUTH_TOKEN,
7378
+ STORAGE.TOKEN,
7379
+ STORAGE.USER,
7380
+ STORAGE.ACCOUNT_TYPE,
7381
+ STORAGE.ALIAS_REFERENCE,
7382
+ STORAGE.APP_ACCESS_TOKEN_REF,
7383
+ STORAGE.REFRESH_TOKEN,
7384
+ STORAGE.TOKEN_EXPIRES_AT,
7385
+ STORAGE.REFRESH_EXPIRES_AT,
7386
+ PROFILE_STORAGE.IMAGE_LAST_STATUS,
7387
+ PROFILE_STORAGE.IMAGE_LAST_CHECK,
7388
+ PROFILE_STORAGE.IMAGE_RECHECK_AT,
7389
+ DEVICE_ID_KEY,
7390
+ SESSION_UUID_KEY,
7391
+ STORAGE_SEED_KEY
7392
+ ];
7340
7393
  const setAuthToken = (token) => {
7341
7394
  const storage = getNativeStorage();
7395
+ if (typeof token !== "string" || token.length === 0) return;
7342
7396
  storage.setItem(STORAGE.AUTH_TOKEN, token);
7343
7397
  };
7344
7398
  const getAuthToken = () => {
@@ -7357,13 +7411,45 @@ const clearAuthToken = () => {
7357
7411
  storage.removeItem(STORAGE.TOKEN_EXPIRES_AT);
7358
7412
  storage.removeItem(STORAGE.REFRESH_EXPIRES_AT);
7359
7413
  };
7414
+ const clearNativeSsoStorage = (options) => {
7415
+ const storage = getNativeStorage();
7416
+ const preserveDeviceIdentity = (options == null ? void 0 : options.preserveDeviceIdentity) === true;
7417
+ ALL_SESSION_STORAGE_KEYS.forEach((key) => {
7418
+ if (preserveDeviceIdentity && (key === DEVICE_ID_KEY || key === SESSION_UUID_KEY || key === STORAGE_SEED_KEY)) {
7419
+ return;
7420
+ }
7421
+ storage.removeItem(key);
7422
+ });
7423
+ };
7424
+ const repairNativeSsoStorage = () => {
7425
+ const storage = getNativeStorage();
7426
+ const authToken = getAuthToken();
7427
+ const userRaw = storage.getItem(STORAGE.USER);
7428
+ if (userRaw && !authToken) {
7429
+ clearNativeSsoStorage();
7430
+ return { cleaned: true, reason: "incomplete_session" };
7431
+ }
7432
+ if (authToken && !userRaw) {
7433
+ clearNativeSsoStorage();
7434
+ return { cleaned: true, reason: "incomplete_session" };
7435
+ }
7436
+ if (userRaw) {
7437
+ try {
7438
+ JSON.parse(userRaw);
7439
+ } catch {
7440
+ clearNativeSsoStorage();
7441
+ return { cleaned: true, reason: "invalid_user_json" };
7442
+ }
7443
+ }
7444
+ return { cleaned: false, reason: null };
7445
+ };
7360
7446
  const logout = async () => {
7361
7447
  const { nativeAuthService: nativeAuthService2 } = await Promise.resolve().then(() => nativeAuth);
7362
7448
  const token = getAuthToken();
7363
7449
  return nativeAuthService2.logout(token || void 0);
7364
7450
  };
7365
7451
  const setAuthUser = (user) => {
7366
- getNativeStorage().setItem(STORAGE.USER, JSON.stringify(user));
7452
+ getNativeStorage().setItem(STORAGE.USER, safeJSONStringify(user));
7367
7453
  };
7368
7454
  const getAuthUser = () => {
7369
7455
  const user = getNativeStorage().getItem(STORAGE.USER);
@@ -13288,6 +13374,16 @@ function NativeSSOPage({
13288
13374
  react.useEffect(() => {
13289
13375
  sessionRef.current = session;
13290
13376
  }, [session]);
13377
+ react.useEffect(() => {
13378
+ const repairResult = repairNativeSsoStorage();
13379
+ if (repairResult.cleaned) {
13380
+ setSession(null);
13381
+ const isDev = Boolean(false);
13382
+ if (isDev) {
13383
+ console.warn("🔧 [NativeSSOPage] Storage SSO réparé", repairResult.reason);
13384
+ }
13385
+ }
13386
+ }, []);
13291
13387
  react.useEffect(() => {
13292
13388
  if (!redirectingTarget) return;
13293
13389
  const timer = window.setTimeout(() => {
@@ -14035,6 +14131,7 @@ exports.PhoneInput = PhoneInput;
14035
14131
  exports.STORAGE_KEYS = STORAGE;
14036
14132
  exports.SignupModal = SignupModal;
14037
14133
  exports.clearAuthToken = clearAuthToken;
14134
+ exports.clearNativeSsoStorage = clearNativeSsoStorage;
14038
14135
  exports.getAccountType = getAccountType;
14039
14136
  exports.getAuthToken = getAuthToken;
14040
14137
  exports.getAuthUser = getAuthUser;
@@ -14055,6 +14152,7 @@ exports.mobilePasswordService = mobilePasswordService;
14055
14152
  exports.nativeAuthService = nativeAuthService;
14056
14153
  exports.profileChangeService = profileChangeService;
14057
14154
  exports.profileMediaService = profileMediaService;
14155
+ exports.repairNativeSsoStorage = repairNativeSsoStorage;
14058
14156
  exports.searchCountries = searchCountries;
14059
14157
  exports.setNativeAuthConfig = setNativeAuthConfig;
14060
14158
  exports.setNativeStorage = setNativeStorage;