@omni-co/embed 0.3.0-pre.1 → 0.3.0-pre.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/README.md CHANGED
@@ -19,7 +19,7 @@ yarn add @omni-co/embed
19
19
 
20
20
  // This creates a signed embed sso link for a dashboard
21
21
  // in the omni account named Acme.
22
- const iframeUrl = embedSsoDashboard({
22
+ const iframeUrl = await embedSsoDashboard({
23
23
  contentId: "miU0hL6z",
24
24
  externalId: "wile.e@coyote.co",
25
25
  name: "Wile E",
@@ -35,7 +35,7 @@ yarn add @omni-co/embed
35
35
 
36
36
  // This creates a signed embed sso link for a dashboard
37
37
  // in the omni account named Acme.
38
- const iframeUrl = embedSsoDashboard({
38
+ const iframeUrl = await embedSsoDashboard({
39
39
  contentId: "miU0hL6z",
40
40
  externalId: "wile.e@coyote.co",
41
41
  entity: "cartoon",
@@ -12,5 +12,5 @@ type EmbedSsoProps = {
12
12
  nonce?: string;
13
13
  port?: number;
14
14
  };
15
- export declare const embedSsoDashboard: (props: EmbedSsoProps) => string;
15
+ export declare const embedSsoDashboard: (props: EmbedSsoProps) => Promise<string>;
16
16
  export {};
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.embedSsoDashboard = void 0;
4
+ const random_str_1 = require("./random-str");
5
+ const signature_1 = require("./signature");
6
+ const defaultEmbedDomain = "embed-omniapp.co";
7
+ const embedLoginRoute = "/embed/login";
8
+ const embedDashboardPath = "/embed/dashboards";
9
+ const validateProps = (props) => {
10
+ const { contentId, externalId, name, nonce, organizationName, secret, userAttributes, } = props;
11
+ if (!contentId) {
12
+ throw new Error("contentId is required");
13
+ }
14
+ if (!externalId) {
15
+ throw new Error("externalId is required");
16
+ }
17
+ if (!name) {
18
+ throw new Error("name is required");
19
+ }
20
+ if (!organizationName) {
21
+ throw new Error("organizationName is required");
22
+ }
23
+ if (!secret) {
24
+ throw new Error("secret is required");
25
+ }
26
+ if (nonce && nonce.length !== 32) {
27
+ throw new Error("nonce must be 32 characters");
28
+ }
29
+ if (typeof userAttributes !== "object") {
30
+ if (userAttributes) {
31
+ throw new Error("userAttributes must be an object");
32
+ }
33
+ }
34
+ };
35
+ const embedSsoDashboard = async (props) => {
36
+ validateProps(props);
37
+ let { contentId, domain, externalId, name, nonce, organizationName, port, secret, userAttributes: rawUserAttributes, entity, prefersDark, theme, } = props;
38
+ // Handle defaults
39
+ nonce = nonce !== null && nonce !== void 0 ? nonce : (await (0, random_str_1.random32ByteString)());
40
+ domain = domain !== null && domain !== void 0 ? domain : defaultEmbedDomain;
41
+ const userAttributes = rawUserAttributes && JSON.stringify(rawUserAttributes);
42
+ // Generate the url for the embed login route
43
+ const url = new URL(embedLoginRoute, `https://${organizationName.toLocaleLowerCase()}.${domain}${port ? `:${port}` : ""}`);
44
+ // Generate the content path
45
+ const contentPath = `${embedDashboardPath}/${contentId}`;
46
+ // Get a signature for the request
47
+ const signature = (0, signature_1.getSignature)({
48
+ loginUrl: url.toString(),
49
+ contentPath,
50
+ externalId,
51
+ name,
52
+ nonce,
53
+ secret,
54
+ userAttributes,
55
+ entity,
56
+ prefersDark,
57
+ theme,
58
+ });
59
+ // Build and return the signed url
60
+ url.searchParams.append("contentPath", contentPath);
61
+ url.searchParams.append("externalId", externalId);
62
+ url.searchParams.append("name", name);
63
+ url.searchParams.append("nonce", nonce);
64
+ url.searchParams.append("signature", signature);
65
+ userAttributes && url.searchParams.append("userAttributes", userAttributes);
66
+ entity && url.searchParams.append("entity", entity);
67
+ prefersDark && url.searchParams.append("prefersDark", prefersDark);
68
+ theme && url.searchParams.append("theme", theme);
69
+ return url.toString();
70
+ };
71
+ exports.embedSsoDashboard = embedSsoDashboard;
@@ -0,0 +1,2 @@
1
+ export * from "./embed";
2
+ export * from "./signature";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./embed"), exports);
18
+ __exportStar(require("./signature"), exports);
@@ -0,0 +1 @@
1
+ export declare const random32ByteString: () => Promise<string>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.random32ByteString = void 0;
4
+ const nanoid = import("nanoid");
5
+ const humanReadableByteSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
6
+ // Generates a random 32 byte, human readable string.
7
+ // Based on math at https://zelark.github.io/nano-id-cc/
8
+ // at 1000 Ids/s it would take more than 1 quadrillion years or
9
+ // 8,730,800,738,924,245T IDs, in order to have a 1% probability
10
+ // of at least one collision.
11
+ const random32ByteString = async () => {
12
+ const { customAlphabet } = await nanoid;
13
+ return customAlphabet(humanReadableByteSpace, 32)();
14
+ };
15
+ exports.random32ByteString = random32ByteString;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TEST_ONLY = exports.getSignature = exports.hmacSign = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
8
+ const hmacSign = (data, secret) => {
9
+ const hmac = crypto_1.default.createHmac("sha256", secret);
10
+ hmac.update(data);
11
+ return hmac.digest("base64url");
12
+ };
13
+ exports.hmacSign = hmacSign;
14
+ const generateStringForSignature = ({ loginUrl, contentPath, externalId, name, nonce, userAttributes, entity, prefersDark, theme, }) => {
15
+ const optionalParams = [];
16
+ entity && optionalParams.push(entity);
17
+ prefersDark && optionalParams.push(prefersDark);
18
+ theme && optionalParams.push(theme);
19
+ userAttributes && optionalParams.push(userAttributes);
20
+ return `
21
+ ${loginUrl}
22
+ ${contentPath}
23
+ ${externalId}
24
+ ${name}
25
+ ${nonce}
26
+ ${optionalParams.join("\n")}
27
+ `
28
+ .trim()
29
+ .replace(/\n\s+/g, "\n");
30
+ };
31
+ const getSignature = ({ secret, ...props }) => {
32
+ return (0, exports.hmacSign)(generateStringForSignature(props), secret);
33
+ };
34
+ exports.getSignature = getSignature;
35
+ exports.TEST_ONLY = {
36
+ generateStringForSignature,
37
+ };
@@ -0,0 +1,16 @@
1
+ type EmbedSsoProps = {
2
+ contentId: string;
3
+ externalId: string;
4
+ name: string;
5
+ organizationName: string;
6
+ secret: string;
7
+ entity?: string;
8
+ userAttributes?: Record<string, string>;
9
+ prefersDark?: string;
10
+ theme?: string;
11
+ domain?: string;
12
+ nonce?: string;
13
+ port?: number;
14
+ };
15
+ export declare const embedSsoDashboard: (props: EmbedSsoProps) => Promise<string>;
16
+ export {};
@@ -1,5 +1,5 @@
1
- import { random32ByteString } from "./random-str.js";
2
- import { getSignature } from "./signature.js";
1
+ import { random32ByteString } from "./random-str";
2
+ import { getSignature } from "./signature";
3
3
  const defaultEmbedDomain = "embed-omniapp.co";
4
4
  const embedLoginRoute = "/embed/login";
5
5
  const embedDashboardPath = "/embed/dashboards";
@@ -29,11 +29,11 @@ const validateProps = (props) => {
29
29
  }
30
30
  }
31
31
  };
32
- export const embedSsoDashboard = (props) => {
32
+ export const embedSsoDashboard = async (props) => {
33
33
  validateProps(props);
34
34
  let { contentId, domain, externalId, name, nonce, organizationName, port, secret, userAttributes: rawUserAttributes, entity, prefersDark, theme, } = props;
35
35
  // Handle defaults
36
- nonce = nonce !== null && nonce !== void 0 ? nonce : random32ByteString();
36
+ nonce = nonce !== null && nonce !== void 0 ? nonce : (await random32ByteString());
37
37
  domain = domain !== null && domain !== void 0 ? domain : defaultEmbedDomain;
38
38
  const userAttributes = rawUserAttributes && JSON.stringify(rawUserAttributes);
39
39
  // Generate the url for the embed login route
@@ -0,0 +1,2 @@
1
+ export * from "./embed";
2
+ export * from "./signature";
@@ -0,0 +1,2 @@
1
+ export * from "./embed";
2
+ export * from "./signature";
@@ -0,0 +1 @@
1
+ export declare const random32ByteString: () => Promise<string>;
@@ -1,8 +1,11 @@
1
- import { customAlphabet } from "nanoid";
1
+ const nanoid = import("nanoid");
2
2
  const humanReadableByteSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
3
3
  // Generates a random 32 byte, human readable string.
4
4
  // Based on math at https://zelark.github.io/nano-id-cc/
5
5
  // at 1000 Ids/s it would take more than 1 quadrillion years or
6
6
  // 8,730,800,738,924,245T IDs, in order to have a 1% probability
7
7
  // of at least one collision.
8
- export const random32ByteString = customAlphabet(humanReadableByteSpace, 32);
8
+ export const random32ByteString = async () => {
9
+ const { customAlphabet } = await nanoid;
10
+ return customAlphabet(humanReadableByteSpace, 32)();
11
+ };
@@ -0,0 +1,55 @@
1
+ export declare const hmacSign: (data: string, secret: string) => string;
2
+ type SignatureProps = {
3
+ /**
4
+ * The protocol, domain, optional port and /embed/login route of the request.
5
+ *
6
+ * Example: `https://example.embed-omniapp.com/embed/login`
7
+ */
8
+ loginUrl: string;
9
+ /**
10
+ * The path to the content being embedded.
11
+ *
12
+ * Example: /embed/dashboards/123abc
13
+ */
14
+ contentPath: string;
15
+ /**
16
+ * The external id of the user.
17
+ */
18
+ externalId: string;
19
+ /**
20
+ * The name of the user.
21
+ */
22
+ name: string;
23
+ /**
24
+ * The nonce of the request.
25
+ */
26
+ nonce: string;
27
+ /**
28
+ * The secret used to sign the request
29
+ */
30
+ secret: string;
31
+ /**
32
+ * The user attributes to be sent with the request. Expected to be a stringified JSON object.
33
+ *
34
+ * Example: '{ "team": "Lakers" }'
35
+ */
36
+ userAttributes?: string;
37
+ /**
38
+ * The entity that the created embed user will belong to. Practically speaking, the embed
39
+ * user will this value assigned to its omni_user_embed_entity user attribute.
40
+ */
41
+ entity?: string;
42
+ /**
43
+ * Whether the user prefers light, dark, or system theme.
44
+ */
45
+ prefersDark?: string;
46
+ /**
47
+ * The theme of the embed.
48
+ */
49
+ theme?: string;
50
+ };
51
+ export declare const getSignature: ({ secret, ...props }: SignatureProps) => string;
52
+ export declare const TEST_ONLY: {
53
+ generateStringForSignature: ({ loginUrl, contentPath, externalId, name, nonce, userAttributes, entity, prefersDark, theme, }: Omit<SignatureProps, "secret">) => string;
54
+ };
55
+ export {};
@@ -1,14 +1,3 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import crypto from "crypto";
13
2
  export const hmacSign = (data, secret) => {
14
3
  const hmac = crypto.createHmac("sha256", secret);
@@ -32,8 +21,7 @@ const generateStringForSignature = ({ loginUrl, contentPath, externalId, name, n
32
21
  .trim()
33
22
  .replace(/\n\s+/g, "\n");
34
23
  };
35
- export const getSignature = (_a) => {
36
- var { secret } = _a, props = __rest(_a, ["secret"]);
24
+ export const getSignature = ({ secret, ...props }) => {
37
25
  return hmacSign(generateStringForSignature(props), secret);
38
26
  };
39
27
  export const TEST_ONLY = {
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.0-pre.1",
2
+ "version": "0.3.0-pre.3",
3
3
  "license": "MIT",
4
4
  "name": "@omni-co/embed",
5
5
  "author": "Nate Agrin <nate@exploreomni.com>",
@@ -12,7 +12,7 @@
12
12
  "vitest": "^0.34.6"
13
13
  },
14
14
  "scripts": {
15
- "build": "npm run clean && tsc -p tsconfig.json",
15
+ "build": "npm run clean && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
16
16
  "clean": "rm -rf lib",
17
17
  "prepublishOnly": "npm run build",
18
18
  "test": "vitest",
@@ -21,7 +21,19 @@
21
21
  "dependencies": {
22
22
  "nanoid": "^5.0.2"
23
23
  },
24
- "exports": "./lib/index.js",
25
- "types": "./lib/index.d.ts",
26
- "type": "module"
24
+ "engines": {
25
+ "node": ">=16"
26
+ },
27
+ "engineStrict": true,
28
+ "types": "./lib/esm/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "import": "./lib/esm/index.js",
32
+ "require": "./lib/cjs/index.js"
33
+ },
34
+ "./lib/esm/index.js": "./lib/esm/index.js",
35
+ "./lib/cjs/index.js": "./lib/cjs/index.js",
36
+ "./lib/esm": "./lib/esm/index.js",
37
+ "./lib/cjs": "./lib/cjs/index.js"
38
+ }
27
39
  }
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./embed.js";
2
- export * from "./signature.js";
package/lib/index.js DELETED
@@ -1,2 +0,0 @@
1
- export * from "./embed.js";
2
- export * from "./signature.js";
@@ -1 +0,0 @@
1
- export declare const random32ByteString: (size?: number | undefined) => string;
File without changes