@omni-co/embed 0.1.3 → 0.1.4

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.
@@ -5,6 +5,7 @@ type EmbedSsoProps = {
5
5
  name: string;
6
6
  nonce?: string;
7
7
  organizationName: string;
8
+ port?: number;
8
9
  secret: string;
9
10
  userAttributes?: Record<string, string>;
10
11
  };
package/lib/cjs/embed.js CHANGED
@@ -1,16 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.embedSsoDashboard = void 0;
7
- const crypto_1 = __importDefault(require("crypto"));
8
4
  const random_str_1 = require("./random-str");
9
- const hmacSign = (data, secret) => {
10
- const hmac = crypto_1.default.createHmac("sha256", secret);
11
- hmac.update(data);
12
- return hmac.digest("base64url");
13
- };
5
+ const signature_1 = require("./signature");
6
+ const defaultEmbedDomain = "embed-omniapp.co";
14
7
  const embedLoginRoute = "/embed/login";
15
8
  const embedDashboardPath = "/embed/dashboards";
16
9
  const validateProps = (props) => {
@@ -41,32 +34,32 @@ const validateProps = (props) => {
41
34
  };
42
35
  const embedSsoDashboard = (props) => {
43
36
  validateProps(props);
44
- let { contentId, domain, externalId, name, nonce, organizationName, secret, userAttributes, } = props;
37
+ let { contentId, domain, externalId, name, nonce, organizationName, port, secret, userAttributes: rawUserAttributes, } = props;
45
38
  // Handle defaults
46
39
  nonce = nonce ?? (0, random_str_1.random32ByteString)();
47
- domain = domain ?? "embed-omniapp.co";
48
- const encodedUserAttributes = userAttributes
49
- ? JSON.stringify(userAttributes)
50
- : "";
51
- const url = new URL(embedLoginRoute, `https://${organizationName.toLocaleLowerCase()}.${domain}`);
40
+ domain = 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
52
45
  const contentPath = `${embedDashboardPath}/${contentId}`;
53
- const validationStr = `
54
- ${url.hostname}${embedLoginRoute}
55
- ${contentPath}
56
- ${externalId}
57
- ${name}
58
- ${nonce}
59
- ${encodedUserAttributes}
60
- `
61
- .trim()
62
- .replace(/\n\s+/g, "\n");
63
- const signature = hmacSign(validationStr, secret);
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
+ });
56
+ // Build and return the signed url
64
57
  url.searchParams.append("contentPath", contentPath);
65
58
  url.searchParams.append("externalId", externalId);
66
59
  url.searchParams.append("name", name);
67
60
  url.searchParams.append("nonce", nonce);
68
61
  url.searchParams.append("signature", signature);
69
- url.searchParams.append("userAttributes", encodedUserAttributes);
62
+ userAttributes && url.searchParams.append("userAttributes", userAttributes);
70
63
  return url.toString();
71
64
  };
72
65
  exports.embedSsoDashboard = embedSsoDashboard;
@@ -1 +1,2 @@
1
1
  export * from "./embed";
2
+ export * from "./signature";
package/lib/cjs/index.js CHANGED
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./embed"), exports);
18
+ __exportStar(require("./signature"), exports);
@@ -0,0 +1,39 @@
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
+ export declare const getSignature: ({ loginUrl, contentPath, externalId, name, nonce, secret, userAttributes, }: SignatureProps) => string;
39
+ export {};
@@ -0,0 +1,27 @@
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.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 getSignature = ({ loginUrl, contentPath, externalId, name, nonce, secret, userAttributes, }) => {
15
+ const validationStr = `
16
+ ${loginUrl}
17
+ ${contentPath}
18
+ ${externalId}
19
+ ${name}
20
+ ${nonce}
21
+ ${userAttributes}
22
+ `
23
+ .trim()
24
+ .replace(/\n\s+/g, "\n");
25
+ return (0, exports.hmacSign)(validationStr, secret);
26
+ };
27
+ exports.getSignature = getSignature;
@@ -5,6 +5,7 @@ type EmbedSsoProps = {
5
5
  name: string;
6
6
  nonce?: string;
7
7
  organizationName: string;
8
+ port?: number;
8
9
  secret: string;
9
10
  userAttributes?: Record<string, string>;
10
11
  };
package/lib/esm/embed.js CHANGED
@@ -1,10 +1,6 @@
1
- import crypto from "crypto";
2
1
  import { random32ByteString } from "./random-str";
3
- const hmacSign = (data, secret) => {
4
- const hmac = crypto.createHmac("sha256", secret);
5
- hmac.update(data);
6
- return hmac.digest("base64url");
7
- };
2
+ import { getSignature } from "./signature";
3
+ const defaultEmbedDomain = "embed-omniapp.co";
8
4
  const embedLoginRoute = "/embed/login";
9
5
  const embedDashboardPath = "/embed/dashboards";
10
6
  const validateProps = (props) => {
@@ -35,31 +31,31 @@ const validateProps = (props) => {
35
31
  };
36
32
  export const embedSsoDashboard = (props) => {
37
33
  validateProps(props);
38
- let { contentId, domain, externalId, name, nonce, organizationName, secret, userAttributes, } = props;
34
+ let { contentId, domain, externalId, name, nonce, organizationName, port, secret, userAttributes: rawUserAttributes, } = props;
39
35
  // Handle defaults
40
36
  nonce = nonce ?? random32ByteString();
41
- domain = domain ?? "embed-omniapp.co";
42
- const encodedUserAttributes = userAttributes
43
- ? JSON.stringify(userAttributes)
44
- : "";
45
- const url = new URL(embedLoginRoute, `https://${organizationName.toLocaleLowerCase()}.${domain}`);
37
+ domain = domain ?? defaultEmbedDomain;
38
+ const userAttributes = rawUserAttributes && JSON.stringify(rawUserAttributes);
39
+ // Generate the url for the embed login route
40
+ const url = new URL(embedLoginRoute, `https://${organizationName.toLocaleLowerCase()}.${domain}${port ? `:${port}` : ""}`);
41
+ // Generate the content path
46
42
  const contentPath = `${embedDashboardPath}/${contentId}`;
47
- const validationStr = `
48
- ${url.hostname}${embedLoginRoute}
49
- ${contentPath}
50
- ${externalId}
51
- ${name}
52
- ${nonce}
53
- ${encodedUserAttributes}
54
- `
55
- .trim()
56
- .replace(/\n\s+/g, "\n");
57
- const signature = hmacSign(validationStr, secret);
43
+ // Get a signature for the request
44
+ const signature = getSignature({
45
+ loginUrl: url.toString(),
46
+ contentPath,
47
+ externalId,
48
+ name,
49
+ nonce,
50
+ secret,
51
+ userAttributes,
52
+ });
53
+ // Build and return the signed url
58
54
  url.searchParams.append("contentPath", contentPath);
59
55
  url.searchParams.append("externalId", externalId);
60
56
  url.searchParams.append("name", name);
61
57
  url.searchParams.append("nonce", nonce);
62
58
  url.searchParams.append("signature", signature);
63
- url.searchParams.append("userAttributes", encodedUserAttributes);
59
+ userAttributes && url.searchParams.append("userAttributes", userAttributes);
64
60
  return url.toString();
65
61
  };
@@ -1 +1,2 @@
1
1
  export * from "./embed";
2
+ export * from "./signature";
package/lib/esm/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./embed";
2
+ export * from "./signature";
@@ -0,0 +1,39 @@
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
+ export declare const getSignature: ({ loginUrl, contentPath, externalId, name, nonce, secret, userAttributes, }: SignatureProps) => string;
39
+ export {};
@@ -0,0 +1,19 @@
1
+ import crypto from "crypto";
2
+ export const hmacSign = (data, secret) => {
3
+ const hmac = crypto.createHmac("sha256", secret);
4
+ hmac.update(data);
5
+ return hmac.digest("base64url");
6
+ };
7
+ export const getSignature = ({ loginUrl, contentPath, externalId, name, nonce, secret, userAttributes, }) => {
8
+ const validationStr = `
9
+ ${loginUrl}
10
+ ${contentPath}
11
+ ${externalId}
12
+ ${name}
13
+ ${nonce}
14
+ ${userAttributes}
15
+ `
16
+ .trim()
17
+ .replace(/\n\s+/g, "\n");
18
+ return hmacSign(validationStr, secret);
19
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.3",
2
+ "version": "0.1.4",
3
3
  "license": "MIT",
4
4
  "name": "@omni-co/embed",
5
5
  "author": "Nate Agrin <nate@exploreomni.com>",