@streamlayer/sdk-web-anonymous-auth 1.1.21 → 1.1.23

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/lib/cjs/index.js CHANGED
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
2
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
6
  require("@streamlayer/sl-eslib/interactive/interactive.common_pb");
4
7
  require("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb");
@@ -736,6 +739,50 @@ function createContextKey(defaultValue, options) {
736
739
  return { id: Symbol(options === null || options === void 0 ? void 0 : options.description), defaultValue };
737
740
  }
738
741
  window.dispatchEvent(new CustomEvent("grpc_devtools_loaded"));
742
+ function isQuotaExceededError(err) {
743
+ return err instanceof DOMException && // everything except Firefox
744
+ (err.code === 22 || // Firefox
745
+ err.code === 1014 || // test name field too, because code might not be present
746
+ // everything except Firefox
747
+ err.name === "QuotaExceededError" || // Firefox
748
+ err.name === "NS_ERROR_DOM_QUOTA_REACHED");
749
+ }
750
+ class Storage {
751
+ constructor(prefix = "main", storage2 = window.localStorage) {
752
+ __publicField(this, "delimiter", ":");
753
+ __publicField(this, "prefix");
754
+ __publicField(this, "storage");
755
+ __publicField(this, "clear", () => {
756
+ for (const key in window.localStorage) {
757
+ if (key.startsWith(this.prefix)) {
758
+ this.storage.removeItem(key);
759
+ }
760
+ }
761
+ });
762
+ __publicField(this, "generateKey", (keyParts) => `${this.prefix}${this.delimiter}${keyParts.join(this.delimiter)}`);
763
+ __publicField(this, "write", (...keyParts) => {
764
+ const value = keyParts.pop() || "";
765
+ const key = this.generateKey(keyParts);
766
+ try {
767
+ this.storage.setItem(key, value);
768
+ } catch (err) {
769
+ if (isQuotaExceededError(err) && this.storage === window.sessionStorage) {
770
+ window.sessionStorage.removeItem("slstreamlogs");
771
+ this.storage.setItem(key, value);
772
+ }
773
+ }
774
+ });
775
+ __publicField(this, "read", (...keyParts) => {
776
+ const value = this.storage.getItem(this.generateKey(keyParts));
777
+ return value === null ? void 0 : value;
778
+ });
779
+ __publicField(this, "remove", (...keyParts) => {
780
+ this.storage.removeItem(this.generateKey(keyParts));
781
+ });
782
+ this.prefix = `sl-sdk${this.delimiter}${prefix}`;
783
+ this.storage = storage2;
784
+ }
785
+ }
739
786
  var ServerStreamSubscriptionStatus;
740
787
  (function(ServerStreamSubscriptionStatus2) {
741
788
  ServerStreamSubscriptionStatus2["Init"] = "init";
@@ -767,7 +814,53 @@ const bypassAuth = (transport, params) => {
767
814
  const contextValues = createRequestOptions({ retryAttempts: 0 });
768
815
  return client.bypassAuth(params, { contextValues });
769
816
  };
770
- const anonymousAuth = async (instance, opts = {}) => {
817
+ var KEY_PREFIX;
818
+ (function(KEY_PREFIX2) {
819
+ KEY_PREFIX2["SCHEMA"] = "schema";
820
+ KEY_PREFIX2["EXTERNAL_TOKEN"] = "eToken";
821
+ KEY_PREFIX2["TOKEN"] = "token";
822
+ })(KEY_PREFIX || (KEY_PREFIX = {}));
823
+ class UserStorage extends Storage {
824
+ constructor() {
825
+ super("user");
826
+ // Schema
827
+ __publicField(this, "setSchema", (value) => {
828
+ this.write(KEY_PREFIX.SCHEMA, value);
829
+ });
830
+ __publicField(this, "getSchema", () => {
831
+ return this.read(KEY_PREFIX.SCHEMA);
832
+ });
833
+ // Token
834
+ __publicField(this, "setToken", (value) => {
835
+ this.write(KEY_PREFIX.TOKEN, value);
836
+ });
837
+ __publicField(this, "getToken", () => {
838
+ return this.read(KEY_PREFIX.TOKEN);
839
+ });
840
+ // External Token
841
+ __publicField(this, "setExternalToken", (value) => {
842
+ this.write(KEY_PREFIX.EXTERNAL_TOKEN, value);
843
+ });
844
+ __publicField(this, "getExternalToken", () => {
845
+ return this.read(KEY_PREFIX.EXTERNAL_TOKEN);
846
+ });
847
+ __publicField(this, "removeToken", () => {
848
+ this.remove(KEY_PREFIX.TOKEN);
849
+ });
850
+ }
851
+ }
852
+ var CoreStatus;
853
+ (function(CoreStatus2) {
854
+ CoreStatus2["DISABLED"] = "disabled";
855
+ CoreStatus2["INITIALIZATION"] = "initialization";
856
+ CoreStatus2["READY"] = "ready";
857
+ CoreStatus2["FAILED"] = "failed";
858
+ CoreStatus2["SUSPENDED"] = "suspended";
859
+ })(CoreStatus || (CoreStatus = {}));
860
+ createLogger("deep_link");
861
+ createLogger("bypass");
862
+ const storage = new UserStorage();
863
+ const login = async (instance, opts = {}) => {
771
864
  var _a, _b;
772
865
  const { schemaName, issuer } = {
773
866
  schemaName: "slra",
@@ -787,7 +880,41 @@ const anonymousAuth = async (instance, opts = {}) => {
787
880
  const pub = await jose.importJWK({ ...pubKey });
788
881
  const deviceId = window.crypto.randomUUID();
789
882
  const jwe = await new jose.EncryptJWT({ ["device-id"]: deviceId, totp: token }).setProtectedHeader({ alg: pubKey.alg || "", enc: "A256CBC-HS512", kid: pubKey.kid }).setIssuedAt().setIssuer(issuer).setAudience(organization.id).setExpirationTime("2m").encrypt(pub);
790
- return instance.auth.login(schema, jwe);
883
+ await instance.auth.login(schema, jwe);
884
+ storage.setSchema("streamlayer:streamlayer");
885
+ storage.setExternalToken(jwe);
886
+ };
887
+ const reLogin = async (instance, opts = {}) => {
888
+ try {
889
+ const prevUser = await instance.auth.reLogin({ skipLogin: true });
890
+ if (!prevUser) {
891
+ await login(instance, opts).catch((e) => {
892
+ console.error(e, "fail anon login");
893
+ });
894
+ }
895
+ } catch (e) {
896
+ console.log(e, "fail anon re-login");
897
+ await login(instance, opts);
898
+ }
899
+ };
900
+ const anonymousAuth = (instance, opts = {}) => {
901
+ return new Promise((resolve, reject) => {
902
+ const timeout = setTimeout(() => {
903
+ reject(new Error("timeout"));
904
+ }, 3e4);
905
+ instance.stores.status.subscribe(async (status) => {
906
+ if (status === "ready") {
907
+ try {
908
+ await reLogin(instance, opts);
909
+ resolve(true);
910
+ } catch (e) {
911
+ reject(e);
912
+ } finally {
913
+ clearTimeout(timeout);
914
+ }
915
+ }
916
+ });
917
+ });
791
918
  };
792
919
  const anonymous = (instance, opts, done) => {
793
920
  instance.sdk.anonymousAuthorization = async () => {
package/lib/es/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1
4
  import "@streamlayer/sl-eslib/interactive/interactive.common_pb";
2
5
  import "@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb";
3
6
  import { SdkOverlayType } from "@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb";
@@ -734,6 +737,50 @@ function createContextKey(defaultValue, options) {
734
737
  return { id: Symbol(options === null || options === void 0 ? void 0 : options.description), defaultValue };
735
738
  }
736
739
  window.dispatchEvent(new CustomEvent("grpc_devtools_loaded"));
740
+ function isQuotaExceededError(err) {
741
+ return err instanceof DOMException && // everything except Firefox
742
+ (err.code === 22 || // Firefox
743
+ err.code === 1014 || // test name field too, because code might not be present
744
+ // everything except Firefox
745
+ err.name === "QuotaExceededError" || // Firefox
746
+ err.name === "NS_ERROR_DOM_QUOTA_REACHED");
747
+ }
748
+ class Storage {
749
+ constructor(prefix = "main", storage2 = window.localStorage) {
750
+ __publicField(this, "delimiter", ":");
751
+ __publicField(this, "prefix");
752
+ __publicField(this, "storage");
753
+ __publicField(this, "clear", () => {
754
+ for (const key in window.localStorage) {
755
+ if (key.startsWith(this.prefix)) {
756
+ this.storage.removeItem(key);
757
+ }
758
+ }
759
+ });
760
+ __publicField(this, "generateKey", (keyParts) => `${this.prefix}${this.delimiter}${keyParts.join(this.delimiter)}`);
761
+ __publicField(this, "write", (...keyParts) => {
762
+ const value = keyParts.pop() || "";
763
+ const key = this.generateKey(keyParts);
764
+ try {
765
+ this.storage.setItem(key, value);
766
+ } catch (err) {
767
+ if (isQuotaExceededError(err) && this.storage === window.sessionStorage) {
768
+ window.sessionStorage.removeItem("slstreamlogs");
769
+ this.storage.setItem(key, value);
770
+ }
771
+ }
772
+ });
773
+ __publicField(this, "read", (...keyParts) => {
774
+ const value = this.storage.getItem(this.generateKey(keyParts));
775
+ return value === null ? void 0 : value;
776
+ });
777
+ __publicField(this, "remove", (...keyParts) => {
778
+ this.storage.removeItem(this.generateKey(keyParts));
779
+ });
780
+ this.prefix = `sl-sdk${this.delimiter}${prefix}`;
781
+ this.storage = storage2;
782
+ }
783
+ }
737
784
  var ServerStreamSubscriptionStatus;
738
785
  (function(ServerStreamSubscriptionStatus2) {
739
786
  ServerStreamSubscriptionStatus2["Init"] = "init";
@@ -765,7 +812,53 @@ const bypassAuth = (transport, params) => {
765
812
  const contextValues = createRequestOptions({ retryAttempts: 0 });
766
813
  return client.bypassAuth(params, { contextValues });
767
814
  };
768
- const anonymousAuth = async (instance, opts = {}) => {
815
+ var KEY_PREFIX;
816
+ (function(KEY_PREFIX2) {
817
+ KEY_PREFIX2["SCHEMA"] = "schema";
818
+ KEY_PREFIX2["EXTERNAL_TOKEN"] = "eToken";
819
+ KEY_PREFIX2["TOKEN"] = "token";
820
+ })(KEY_PREFIX || (KEY_PREFIX = {}));
821
+ class UserStorage extends Storage {
822
+ constructor() {
823
+ super("user");
824
+ // Schema
825
+ __publicField(this, "setSchema", (value) => {
826
+ this.write(KEY_PREFIX.SCHEMA, value);
827
+ });
828
+ __publicField(this, "getSchema", () => {
829
+ return this.read(KEY_PREFIX.SCHEMA);
830
+ });
831
+ // Token
832
+ __publicField(this, "setToken", (value) => {
833
+ this.write(KEY_PREFIX.TOKEN, value);
834
+ });
835
+ __publicField(this, "getToken", () => {
836
+ return this.read(KEY_PREFIX.TOKEN);
837
+ });
838
+ // External Token
839
+ __publicField(this, "setExternalToken", (value) => {
840
+ this.write(KEY_PREFIX.EXTERNAL_TOKEN, value);
841
+ });
842
+ __publicField(this, "getExternalToken", () => {
843
+ return this.read(KEY_PREFIX.EXTERNAL_TOKEN);
844
+ });
845
+ __publicField(this, "removeToken", () => {
846
+ this.remove(KEY_PREFIX.TOKEN);
847
+ });
848
+ }
849
+ }
850
+ var CoreStatus;
851
+ (function(CoreStatus2) {
852
+ CoreStatus2["DISABLED"] = "disabled";
853
+ CoreStatus2["INITIALIZATION"] = "initialization";
854
+ CoreStatus2["READY"] = "ready";
855
+ CoreStatus2["FAILED"] = "failed";
856
+ CoreStatus2["SUSPENDED"] = "suspended";
857
+ })(CoreStatus || (CoreStatus = {}));
858
+ createLogger("deep_link");
859
+ createLogger("bypass");
860
+ const storage = new UserStorage();
861
+ const login = async (instance, opts = {}) => {
769
862
  var _a, _b;
770
863
  const { schemaName, issuer } = {
771
864
  schemaName: "slra",
@@ -785,7 +878,41 @@ const anonymousAuth = async (instance, opts = {}) => {
785
878
  const pub = await importJWK({ ...pubKey });
786
879
  const deviceId = window.crypto.randomUUID();
787
880
  const jwe = await new EncryptJWT({ ["device-id"]: deviceId, totp: token }).setProtectedHeader({ alg: pubKey.alg || "", enc: "A256CBC-HS512", kid: pubKey.kid }).setIssuedAt().setIssuer(issuer).setAudience(organization.id).setExpirationTime("2m").encrypt(pub);
788
- return instance.auth.login(schema, jwe);
881
+ await instance.auth.login(schema, jwe);
882
+ storage.setSchema("streamlayer:streamlayer");
883
+ storage.setExternalToken(jwe);
884
+ };
885
+ const reLogin = async (instance, opts = {}) => {
886
+ try {
887
+ const prevUser = await instance.auth.reLogin({ skipLogin: true });
888
+ if (!prevUser) {
889
+ await login(instance, opts).catch((e) => {
890
+ console.error(e, "fail anon login");
891
+ });
892
+ }
893
+ } catch (e) {
894
+ console.log(e, "fail anon re-login");
895
+ await login(instance, opts);
896
+ }
897
+ };
898
+ const anonymousAuth = (instance, opts = {}) => {
899
+ return new Promise((resolve, reject) => {
900
+ const timeout = setTimeout(() => {
901
+ reject(new Error("timeout"));
902
+ }, 3e4);
903
+ instance.stores.status.subscribe(async (status) => {
904
+ if (status === "ready") {
905
+ try {
906
+ await reLogin(instance, opts);
907
+ resolve(true);
908
+ } catch (e) {
909
+ reject(e);
910
+ } finally {
911
+ clearTimeout(timeout);
912
+ }
913
+ }
914
+ });
915
+ });
789
916
  };
790
917
  const anonymous = (instance, opts, done) => {
791
918
  instance.sdk.anonymousAuthorization = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamlayer/sdk-web-anonymous-auth",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "author": "StreamLayer, Inc (https://streamlayer.io)",
5
5
  "maintainers": [
6
6
  {
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "peerDependencies": {
26
26
  "jose": "^5.9.3",
27
- "@streamlayer/sdk-web": "^1.7.4"
27
+ "@streamlayer/sdk-web": "^1.8.1"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@nx/devkit": "20.2.1",
@@ -39,8 +39,8 @@
39
39
  "vite-plugin-node-polyfills": "^0.22.0",
40
40
  "vite-tsconfig-paths": "^5.0.1",
41
41
  "webpack": "^5.95.0",
42
- "@streamlayer/sdk-web-api": "^1.6.21",
43
- "@streamlayer/sdk-web-core": "^1.9.4",
44
- "@streamlayer/sdk-web-interfaces": "^1.4.8"
42
+ "@streamlayer/sdk-web-api": "^1.7.1",
43
+ "@streamlayer/sdk-web-core": "^1.10.1",
44
+ "@streamlayer/sdk-web-interfaces": "^1.4.10"
45
45
  }
46
46
  }