@dfns/sdk 0.6.12 → 0.7.0

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.
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertAuthTokenIsSameOrg = exports.JWT_CUSTOM_DATA_CLAIM = void 0;
4
+ const base64_1 = require("./base64");
5
+ exports.JWT_CUSTOM_DATA_CLAIM = 'https://custom/app_metadata';
6
+ const assertAuthTokenIsSameOrg = ({ authToken, orgId }) => {
7
+ const tokenBody = authToken.split('.')?.[1] || '';
8
+ let decoded;
9
+ try {
10
+ decoded = JSON.parse((0, base64_1.fromBase64)(tokenBody).toString('utf-8'));
11
+ }
12
+ catch (error) {
13
+ throw new Error('Provided auth token could not be properly parsed');
14
+ }
15
+ const tokenOrgId = decoded?.[exports.JWT_CUSTOM_DATA_CLAIM]?.['orgId'];
16
+ if (tokenOrgId !== orgId) {
17
+ throw new Error(`Provided auth token is not scoped to org ID ${orgId}`);
18
+ }
19
+ };
20
+ exports.assertAuthTokenIsSameOrg = assertAuthTokenIsSameOrg;
package/utils/fetch.js CHANGED
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.simpleFetch = exports.dfnsAuth = exports.catchPolicyPending = exports.errorHandler = exports.jsonSerializer = exports.fullUrl = void 0;
4
4
  const cross_fetch_1 = require("cross-fetch");
5
- const nonce_1 = require("./nonce");
6
5
  const dfnsError_1 = require("../dfnsError");
7
6
  const DEFAULT_DFNS_BASE_URL = 'https://api.dfns.io';
8
7
  const package_json_1 = require("../package.json");
8
+ const authToken_1 = require("./authToken");
9
9
  const fullUrl = (fetch) => {
10
10
  return async (resource, options) => {
11
11
  const baseUrl = options.apiOptions.baseUrl || DEFAULT_DFNS_BASE_URL;
@@ -60,22 +60,17 @@ const catchPolicyPending = (fetch) => {
60
60
  exports.catchPolicyPending = catchPolicyPending;
61
61
  const dfnsAuth = (fetch) => {
62
62
  return async (resource, options) => {
63
- const { appId, appSecret, authToken } = options.apiOptions;
63
+ const { orgId, authToken } = options.apiOptions;
64
+ if (authToken && orgId) {
65
+ (0, authToken_1.assertAuthTokenIsSameOrg)({ orgId, authToken });
66
+ }
64
67
  const authorization = authToken
65
68
  ? {
66
69
  authorization: `Bearer ${authToken}`,
67
70
  }
68
71
  : {};
69
- const dfnsAppSecret = appSecret
70
- ? {
71
- 'x-dfns-appsecret': appSecret,
72
- }
73
- : {};
74
72
  options.headers = {
75
- 'x-dfns-appid': appId,
76
- 'x-dfns-nonce': (0, nonce_1.generateNonce)(),
77
73
  'x-dfns-sdk-version': package_json_1.version,
78
- ...dfnsAppSecret,
79
74
  ...authorization,
80
75
  ...(options.headers ?? {}),
81
76
  };
package/utils/index.d.ts CHANGED
@@ -2,7 +2,6 @@ export * from './base64';
2
2
  export * from './bigint';
3
3
  export * from './crypto';
4
4
  export * from './fetch';
5
- export * from './nonce';
6
5
  export * from './string';
7
6
  export * from './url';
8
7
  export declare const generateRandom: (size: number) => Uint8Array;
package/utils/index.js CHANGED
@@ -19,7 +19,6 @@ __exportStar(require("./base64"), exports);
19
19
  __exportStar(require("./bigint"), exports);
20
20
  __exportStar(require("./crypto"), exports);
21
21
  __exportStar(require("./fetch"), exports);
22
- __exportStar(require("./nonce"), exports);
23
22
  __exportStar(require("./string"), exports);
24
23
  __exportStar(require("./url"), exports);
25
24
  const generateRandom = (size) => {
package/utils/nonce.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const generateNonce: () => string;
package/utils/nonce.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateNonce = void 0;
4
- const uuid_1 = require("uuid");
5
- const base64_1 = require("./base64");
6
- const generateNonce = () => {
7
- return (0, base64_1.toBase64Url)(JSON.stringify({
8
- uuid: (0, uuid_1.v4)(),
9
- date: new Date().toISOString(),
10
- }));
11
- };
12
- exports.generateNonce = generateNonce;