box-node-sdk 1.37.0 → 1.39.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.
Files changed (174) hide show
  1. package/CHANGELOG.md +38 -2
  2. package/README.md +2 -0
  3. package/lib/api-request-manager.d.ts +38 -0
  4. package/lib/api-request-manager.js +48 -55
  5. package/lib/api-request-manager.js.map +1 -0
  6. package/lib/api-request.d.ts +137 -0
  7. package/lib/api-request.js +202 -281
  8. package/lib/api-request.js.map +1 -0
  9. package/lib/box-client.d.ts +271 -0
  10. package/lib/box-client.js +580 -713
  11. package/lib/box-client.js.map +1 -0
  12. package/lib/box-node-sdk.d.ts +216 -0
  13. package/lib/box-node-sdk.js +317 -352
  14. package/lib/box-node-sdk.js.map +1 -0
  15. package/lib/chunked-uploader.d.ts +129 -0
  16. package/lib/chunked-uploader.js +287 -358
  17. package/lib/chunked-uploader.js.map +1 -0
  18. package/lib/enterprise-event-stream.d.ts +82 -0
  19. package/lib/enterprise-event-stream.js +189 -203
  20. package/lib/enterprise-event-stream.js.map +1 -0
  21. package/lib/event-stream.d.ts +92 -0
  22. package/lib/event-stream.js +274 -302
  23. package/lib/event-stream.js.map +1 -0
  24. package/lib/managers/collaboration-allowlist.d.ts +137 -0
  25. package/lib/managers/collaboration-allowlist.js +200 -0
  26. package/lib/managers/collaboration-allowlist.js.map +1 -0
  27. package/lib/managers/collaboration-whitelist.d.ts +3 -0
  28. package/lib/managers/collaboration-whitelist.js +8 -222
  29. package/lib/managers/collaboration-whitelist.js.map +1 -0
  30. package/lib/managers/collaborations.d.ts +166 -0
  31. package/lib/managers/collaborations.js +225 -258
  32. package/lib/managers/collaborations.js.map +1 -0
  33. package/lib/managers/collections.d.ts +42 -0
  34. package/lib/managers/collections.js +45 -50
  35. package/lib/managers/collections.js.map +1 -0
  36. package/lib/managers/comments.d.ts +103 -0
  37. package/lib/managers/comments.js +158 -173
  38. package/lib/managers/comments.js.map +1 -0
  39. package/lib/managers/device-pins.d.ts +52 -0
  40. package/lib/managers/device-pins.js +75 -88
  41. package/lib/managers/device-pins.js.map +1 -0
  42. package/lib/managers/enterprise.d.ts +162 -0
  43. package/lib/managers/enterprise.js +168 -199
  44. package/lib/managers/enterprise.js.map +1 -0
  45. package/lib/managers/events.d.ts +179 -0
  46. package/lib/managers/events.js +232 -254
  47. package/lib/managers/events.js.map +1 -0
  48. package/lib/managers/files.d.ts +773 -0
  49. package/lib/managers/files.js +1401 -1602
  50. package/lib/managers/files.js.map +1 -0
  51. package/lib/managers/folders.d.ts +347 -0
  52. package/lib/managers/folders.js +551 -630
  53. package/lib/managers/folders.js.map +1 -0
  54. package/lib/managers/groups.d.ts +202 -0
  55. package/lib/managers/groups.js +238 -288
  56. package/lib/managers/groups.js.map +1 -0
  57. package/lib/managers/legal-hold-policies.d.ts +190 -0
  58. package/lib/managers/legal-hold-policies.js +228 -272
  59. package/lib/managers/legal-hold-policies.js.map +1 -0
  60. package/lib/managers/metadata.d.ts +228 -0
  61. package/lib/managers/metadata.js +265 -329
  62. package/lib/managers/metadata.js.map +1 -0
  63. package/lib/managers/recent-items.d.ts +38 -0
  64. package/lib/managers/recent-items.js +32 -39
  65. package/lib/managers/recent-items.js.map +1 -0
  66. package/lib/managers/retention-policies.d.ts +251 -0
  67. package/lib/managers/retention-policies.js +278 -281
  68. package/lib/managers/retention-policies.js.map +1 -0
  69. package/lib/managers/search.d.ts +82 -0
  70. package/lib/managers/search.js +68 -88
  71. package/lib/managers/search.js.map +1 -0
  72. package/lib/managers/shared-items.d.ts +33 -0
  73. package/lib/managers/shared-items.js +54 -62
  74. package/lib/managers/shared-items.js.map +1 -0
  75. package/lib/managers/sign-requests.generated.d.ts +92 -0
  76. package/lib/managers/sign-requests.generated.js +97 -0
  77. package/lib/managers/sign-requests.generated.js.map +1 -0
  78. package/lib/managers/storage-policies.d.ts +86 -0
  79. package/lib/managers/storage-policies.js +108 -142
  80. package/lib/managers/storage-policies.js.map +1 -0
  81. package/lib/managers/tasks.d.ts +161 -0
  82. package/lib/managers/tasks.js +219 -260
  83. package/lib/managers/tasks.js.map +1 -0
  84. package/lib/managers/terms-of-service.d.ts +161 -0
  85. package/lib/managers/terms-of-service.js +250 -273
  86. package/lib/managers/terms-of-service.js.map +1 -0
  87. package/lib/managers/trash.d.ts +30 -0
  88. package/lib/managers/trash.js +30 -41
  89. package/lib/managers/trash.js.map +1 -0
  90. package/lib/managers/users.d.ts +130 -0
  91. package/lib/managers/users.js +160 -203
  92. package/lib/managers/users.js.map +1 -0
  93. package/lib/managers/web-links.d.ts +127 -0
  94. package/lib/managers/web-links.js +183 -209
  95. package/lib/managers/web-links.js.map +1 -0
  96. package/lib/managers/webhooks.d.ts +166 -0
  97. package/lib/managers/webhooks.js +303 -308
  98. package/lib/managers/webhooks.js.map +1 -0
  99. package/lib/schemas/file-base.generated.d.ts +32 -0
  100. package/lib/schemas/file-base.generated.js +3 -0
  101. package/lib/schemas/file-base.generated.js.map +1 -0
  102. package/lib/schemas/file-mini.generated.d.ts +41 -0
  103. package/lib/schemas/file-mini.generated.js +3 -0
  104. package/lib/schemas/file-mini.generated.js.map +1 -0
  105. package/lib/schemas/file-version-base.generated.d.ts +19 -0
  106. package/lib/schemas/file-version-base.generated.js +3 -0
  107. package/lib/schemas/file-version-base.generated.js.map +1 -0
  108. package/lib/schemas/file-version-mini.generated.d.ts +14 -0
  109. package/lib/schemas/file-version-mini.generated.js +3 -0
  110. package/lib/schemas/file-version-mini.generated.js.map +1 -0
  111. package/lib/schemas/folder-base.generated.d.ts +32 -0
  112. package/lib/schemas/folder-base.generated.js +3 -0
  113. package/lib/schemas/folder-base.generated.js.map +1 -0
  114. package/lib/schemas/folder-mini.generated.d.ts +31 -0
  115. package/lib/schemas/folder-mini.generated.js +3 -0
  116. package/lib/schemas/folder-mini.generated.js.map +1 -0
  117. package/lib/schemas/index.d.ts +13 -0
  118. package/lib/schemas/index.js +26 -0
  119. package/lib/schemas/index.js.map +1 -0
  120. package/lib/schemas/sign-request-create-request.generated.d.ts +62 -0
  121. package/lib/schemas/sign-request-create-request.generated.js +3 -0
  122. package/lib/schemas/sign-request-create-request.generated.js.map +1 -0
  123. package/lib/schemas/sign-request-create-signer.generated.d.ts +38 -0
  124. package/lib/schemas/sign-request-create-signer.generated.js +3 -0
  125. package/lib/schemas/sign-request-create-signer.generated.js.map +1 -0
  126. package/lib/schemas/sign-request-prefill-tag.generated.d.ts +28 -0
  127. package/lib/schemas/sign-request-prefill-tag.generated.js +3 -0
  128. package/lib/schemas/sign-request-prefill-tag.generated.js.map +1 -0
  129. package/lib/schemas/sign-request-signer-input.generated.d.ts +18 -0
  130. package/lib/schemas/sign-request-signer-input.generated.js +3 -0
  131. package/lib/schemas/sign-request-signer-input.generated.js.map +1 -0
  132. package/lib/schemas/sign-request-signer.generated.d.ts +23 -0
  133. package/lib/schemas/sign-request-signer.generated.js +3 -0
  134. package/lib/schemas/sign-request-signer.generated.js.map +1 -0
  135. package/lib/schemas/sign-request.generated.d.ts +50 -0
  136. package/lib/schemas/sign-request.generated.js +3 -0
  137. package/lib/schemas/sign-request.generated.js.map +1 -0
  138. package/lib/schemas/sign-requests.generated.d.ts +27 -0
  139. package/lib/schemas/sign-requests.generated.js +3 -0
  140. package/lib/schemas/sign-requests.generated.js.map +1 -0
  141. package/lib/sessions/anonymous-session.d.ts +69 -0
  142. package/lib/sessions/anonymous-session.js +88 -102
  143. package/lib/sessions/anonymous-session.js.map +1 -0
  144. package/lib/sessions/app-auth-session.d.ts +92 -0
  145. package/lib/sessions/app-auth-session.js +140 -160
  146. package/lib/sessions/app-auth-session.js.map +1 -0
  147. package/lib/sessions/basic-session.d.ts +56 -0
  148. package/lib/sessions/basic-session.js +40 -50
  149. package/lib/sessions/basic-session.js.map +1 -0
  150. package/lib/sessions/persistent-session.d.ts +96 -0
  151. package/lib/sessions/persistent-session.js +191 -211
  152. package/lib/sessions/persistent-session.js.map +1 -0
  153. package/lib/token-manager.d.ts +191 -0
  154. package/lib/token-manager.js +390 -465
  155. package/lib/token-manager.js.map +1 -0
  156. package/lib/util/config.d.ts +86 -0
  157. package/lib/util/config.js +146 -152
  158. package/lib/util/config.js.map +1 -0
  159. package/lib/util/errors.d.ts +50 -0
  160. package/lib/util/errors.js +134 -145
  161. package/lib/util/errors.js.map +1 -0
  162. package/lib/util/exponential-backoff.d.ts +11 -0
  163. package/lib/util/exponential-backoff.js +10 -22
  164. package/lib/util/exponential-backoff.js.map +1 -0
  165. package/lib/util/paging-iterator.d.ts +53 -0
  166. package/lib/util/paging-iterator.js +202 -218
  167. package/lib/util/paging-iterator.js.map +1 -0
  168. package/lib/util/serializable.d.ts +10 -0
  169. package/lib/util/serializable.js +18 -0
  170. package/lib/util/serializable.js.map +1 -0
  171. package/lib/util/url-path.d.ts +16 -0
  172. package/lib/util/url-path.js +20 -35
  173. package/lib/util/url-path.js.map +1 -0
  174. package/package.json +21 -2
@@ -1,34 +1,68 @@
1
+ "use strict";
1
2
  /**
2
3
  * @fileoverview Manager for the Box Webhooks resource
3
4
  */
4
- 'use strict';
5
-
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ // -----------------------------------------------------------------------------
9
+ // Requirements
10
+ // -----------------------------------------------------------------------------
11
+ var url_path_1 = __importDefault(require("../util/url-path"));
12
+ var crypto_1 = __importDefault(require("crypto"));
6
13
  // -----------------------------------------------------------------------------
7
14
  // Typedefs
8
15
  // -----------------------------------------------------------------------------
9
-
10
16
  /**
11
17
  * A webhook trigger type constant
12
18
  * @typedef {string} WebhookTriggerType
13
19
  */
14
-
15
- // -----------------------------------------------------------------------------
16
- // Requirements
17
- // -----------------------------------------------------------------------------
18
-
19
- var urlPath = require('../util/url-path');
20
- const crypto = require('crypto');
21
-
20
+ var WebhookTriggerType;
21
+ (function (WebhookTriggerType) {
22
+ WebhookTriggerType["FILE_UPLOADED"] = "FILE.UPLOADED";
23
+ WebhookTriggerType["FILE_PREVIEWED"] = "FILE.PREVIEWED";
24
+ WebhookTriggerType["FILE_DOWNLOADED"] = "FILE.DOWNLOADED";
25
+ WebhookTriggerType["FILE_TRASHED"] = "FILE.TRASHED";
26
+ WebhookTriggerType["FILE_DELETED"] = "FILE.DELETED";
27
+ WebhookTriggerType["FILE_RESTORED"] = "FILE.RESTORED";
28
+ WebhookTriggerType["FILE_COPIED"] = "FILE.COPIED";
29
+ WebhookTriggerType["FILE_MOVED"] = "FILE.MOVED";
30
+ WebhookTriggerType["FILE_LOCKED"] = "FILE.LOCKED";
31
+ WebhookTriggerType["FILE_UNLOCKED"] = "FILE.UNLOCKED";
32
+ WebhookTriggerType["FILE_RENAMED"] = "FILE.RENAMED";
33
+ WebhookTriggerType["COMMENT_CREATED"] = "COMMENT.CREATED";
34
+ WebhookTriggerType["COMMENT_UPDATED"] = "COMMENT.UPDATED";
35
+ WebhookTriggerType["COMMENT_DELETED"] = "COMMENT.DELETED";
36
+ WebhookTriggerType["TASK_ASSIGNMENT_CREATED"] = "TASK_ASSIGNMENT.CREATED";
37
+ WebhookTriggerType["TASK_ASSIGNMENT_UPDATED"] = "TASK_ASSIGNMENT.UPDATED";
38
+ WebhookTriggerType["METADATA_INSTANCE_CREATED"] = "METADATA_INSTANCE.CREATED";
39
+ WebhookTriggerType["METADATA_INSTANCE_UPDATED"] = "METADATA_INSTANCE.UPDATED";
40
+ WebhookTriggerType["METADATA_INSTANCE_DELETED"] = "METADATA_INSTANCE.DELETED";
41
+ WebhookTriggerType["FOLDER_CREATED"] = "FOLDER.CREATED";
42
+ WebhookTriggerType["FOLDER_DOWNLOADED"] = "FOLDER.DOWNLOADED";
43
+ WebhookTriggerType["FOLDER_RESTORED"] = "FOLDER.RESTORED";
44
+ WebhookTriggerType["FOLDER_DELETED"] = "FOLDER.DELETED";
45
+ WebhookTriggerType["FOLDER_COPIED"] = "FOLDER.COPIED";
46
+ WebhookTriggerType["FOLDER_MOVED"] = "FOLDER.MOVED";
47
+ WebhookTriggerType["FOLDER_TRASHED"] = "FOLDER.TRASHED";
48
+ WebhookTriggerType["FOLDER_RENAMED"] = "FOLDER.RENAMED";
49
+ WebhookTriggerType["WEBHOOK_DELETED"] = "WEBHOOK.DELETED";
50
+ WebhookTriggerType["COLLABORATION_CREATED"] = "COLLABORATION.CREATED";
51
+ WebhookTriggerType["COLLABORATION_ACCEPTED"] = "COLLABORATION.ACCEPTED";
52
+ WebhookTriggerType["COLLABORATION_REJECTED"] = "COLLABORATION.REJECTED";
53
+ WebhookTriggerType["COLLABORATION_REMOVED"] = "COLLABORATION.REMOVED";
54
+ WebhookTriggerType["COLLABORATION_UPDATED"] = "COLLABORATION.UPDATED";
55
+ WebhookTriggerType["SHARED_LINK_DELETED"] = "SHARED_LINK.DELETED";
56
+ WebhookTriggerType["SHARED_LINK_CREATED"] = "SHARED_LINK.CREATED";
57
+ WebhookTriggerType["SHARED_LINK_UPDATED"] = "SHARED_LINK.UPDATED";
58
+ })(WebhookTriggerType || (WebhookTriggerType = {}));
22
59
  // -----------------------------------------------------------------------------
23
60
  // Private
24
61
  // -----------------------------------------------------------------------------
25
-
26
62
  // Base path for all webhooks endpoints
27
63
  var BASE_PATH = '/webhooks';
28
-
29
64
  // This prevents replay attacks
30
- const MAX_MESSAGE_AGE = 10 * 60; // 10 minutes
31
-
65
+ var MAX_MESSAGE_AGE = 10 * 60; // 10 minutes
32
66
  /**
33
67
  * Compute the message signature
34
68
  * @see {@Link https://developer.box.com/en/guides/webhooks/handle/setup-signatures/}
@@ -40,56 +74,47 @@ const MAX_MESSAGE_AGE = 10 * 60; // 10 minutes
40
74
  * @private
41
75
  */
42
76
  function computeSignature(body, headers, signatureKey) {
43
- if (!signatureKey) {
44
- return null;
45
- }
46
-
47
- if (headers['box-signature-version'] !== '1') {
48
- return null;
49
- }
50
-
51
- if (headers['box-signature-algorithm'] !== 'HmacSHA256') {
52
- return null;
53
- }
54
-
55
- let hmac = crypto.createHmac('sha256', signatureKey);
56
- hmac.update(body);
57
- hmac.update(headers['box-delivery-timestamp']);
58
-
59
- const signature = hmac.digest('base64');
60
-
61
- return signature;
77
+ if (!signatureKey) {
78
+ return null;
79
+ }
80
+ if (headers['box-signature-version'] !== '1') {
81
+ return null;
82
+ }
83
+ if (headers['box-signature-algorithm'] !== 'HmacSHA256') {
84
+ return null;
85
+ }
86
+ var hmac = crypto_1.default.createHmac('sha256', signatureKey);
87
+ hmac.update(body);
88
+ hmac.update(headers['box-delivery-timestamp']);
89
+ var signature = hmac.digest('base64');
90
+ return signature;
62
91
  }
63
-
64
92
  /**
65
93
  * Validate the message signature
66
94
  * @see {@Link https://developer.box.com/en/guides/webhooks/handle/verify-signatures/}
67
95
  *
68
96
  * @param {string} body - The request body of the webhook message
69
97
  * @param {Object} headers - The request headers of the webhook message
70
- * @param {string} primarySignatureKey - The primary signature to verify the message with
98
+ * @param {string} [primarySignatureKey] - The primary signature to verify the message with
71
99
  * @param {string} [secondarySignatureKey] - The secondary signature to verify the message with
72
100
  * @returns {boolean} - True or false
73
101
  * @private
74
102
  */
75
103
  function validateSignature(body, headers, primarySignatureKey, secondarySignatureKey) {
76
- // Either the primary or secondary signature must match the corresponding signature from Box
77
- // (The use of two signatures allows the signing keys to be rotated one at a time)
78
- const primarySignature = computeSignature(body, headers, primarySignatureKey);
79
-
80
- if (primarySignature && primarySignature === headers['box-signature-primary']) {
81
- return true;
82
- }
83
-
84
- const secondarySignature = computeSignature(body, headers, secondarySignatureKey);
85
-
86
- if (secondarySignature && secondarySignature === headers['box-signature-secondary']) {
87
- return true;
88
- }
89
-
90
- return false;
104
+ // Either the primary or secondary signature must match the corresponding signature from Box
105
+ // (The use of two signatures allows the signing keys to be rotated one at a time)
106
+ var primarySignature = computeSignature(body, headers, primarySignatureKey);
107
+ if (primarySignature &&
108
+ primarySignature === headers['box-signature-primary']) {
109
+ return true;
110
+ }
111
+ var secondarySignature = computeSignature(body, headers, secondarySignatureKey);
112
+ if (secondarySignature &&
113
+ secondarySignature === headers['box-signature-secondary']) {
114
+ return true;
115
+ }
116
+ return false;
91
117
  }
92
-
93
118
  /**
94
119
  * Validate that the delivery timestamp is not too far in the past (to prevent replay attacks)
95
120
  *
@@ -99,17 +124,14 @@ function validateSignature(body, headers, primarySignatureKey, secondarySignatur
99
124
  * @private
100
125
  */
101
126
  function validateDeliveryTimestamp(headers, maxMessageAge) {
102
- const deliveryTime = Date.parse(headers['box-delivery-timestamp']);
103
- const currentTime = Date.now();
104
- const messageAge = (currentTime - deliveryTime) / 1000;
105
-
106
- if (messageAge > maxMessageAge) {
107
- return false;
108
- }
109
-
110
- return true;
127
+ var deliveryTime = Date.parse(headers['box-delivery-timestamp']);
128
+ var currentTime = Date.now();
129
+ var messageAge = (currentTime - deliveryTime) / 1000;
130
+ if (messageAge > maxMessageAge) {
131
+ return false;
132
+ }
133
+ return true;
111
134
  }
112
-
113
135
  /**
114
136
  * Stringify JSON with escaped multibyte Unicode characters to ensure computed signatures match PHP's default behavior
115
137
  *
@@ -118,39 +140,182 @@ function validateDeliveryTimestamp(headers, maxMessageAge) {
118
140
  * @private
119
141
  */
120
142
  function jsonStringifyWithEscapedUnicode(body) {
121
- return JSON.stringify(body).replace(/[\u007f-\uffff]/g, char => `\\u${`0000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
143
+ return JSON.stringify(body).replace(/[\u007f-\uffff]/g, function (char) { return "\\u" + ("0000" + char.charCodeAt(0).toString(16)).slice(-4); });
122
144
  }
123
-
124
145
  // -----------------------------------------------------------------------------
125
146
  // Public
126
147
  // -----------------------------------------------------------------------------
127
-
128
148
  /**
129
149
  * Simple manager for interacting with all 'Webhooks' endpoints and actions.
130
150
  *
131
151
  * @param {BoxClient} client The Box API Client that is responsible for making calls to the API
132
152
  * @constructor
133
153
  */
134
- function Webhooks(client) {
135
- // Attach the client, for making API calls
136
- this.client = client;
137
- }
138
-
139
- /**
140
- * Primary signature key to protect webhooks against attacks.
141
- * @static
142
- * @type {?string}
143
- */
144
- Webhooks.primarySignatureKey = null;
145
-
146
- /**
147
- * Secondary signature key to protect webhooks against attacks.
148
- * @static
149
- * @type {?string}
150
- */
151
- Webhooks.secondarySignatureKey = null;
152
-
153
-
154
+ var Webhooks = /** @class */ (function () {
155
+ function Webhooks(client) {
156
+ // Attach the client, for making API calls
157
+ this.client = client;
158
+ }
159
+ /**
160
+ * Sets primary and secondary signatures that are used to verify the Webhooks messages
161
+ *
162
+ * @param {string} primaryKey - The primary signature to verify the message with
163
+ * @param {string} [secondaryKey] - The secondary signature to verify the message with
164
+ * @returns {void}
165
+ */
166
+ Webhooks.setSignatureKeys = function (primaryKey, secondaryKey) {
167
+ Webhooks.primarySignatureKey = primaryKey;
168
+ if (typeof secondaryKey === 'string') {
169
+ Webhooks.secondarySignatureKey = secondaryKey;
170
+ }
171
+ };
172
+ /**
173
+ * Validate a webhook message by verifying the signature and the delivery timestamp
174
+ *
175
+ * @param {string|Object} body - The request body of the webhook message
176
+ * @param {Object} headers - The request headers of the webhook message
177
+ * @param {string} [primaryKey] - The primary signature to verify the message with. If it is sent as a parameter,
178
+ it overrides the static variable primarySignatureKey
179
+ * @param {string} [secondaryKey] - The secondary signature to verify the message with. If it is sent as a parameter,
180
+ it overrides the static variable primarySignatureKey
181
+ * @param {int} [maxMessageAge] - The maximum message age (in seconds). Defaults to 10 minutes
182
+ * @returns {boolean} - True or false
183
+ */
184
+ Webhooks.validateMessage = function (body, headers, primaryKey, secondaryKey, maxMessageAge) {
185
+ if (!primaryKey && Webhooks.primarySignatureKey) {
186
+ primaryKey = Webhooks.primarySignatureKey;
187
+ }
188
+ if (!secondaryKey && Webhooks.secondarySignatureKey) {
189
+ secondaryKey = Webhooks.secondarySignatureKey;
190
+ }
191
+ if (typeof maxMessageAge !== 'number') {
192
+ maxMessageAge = MAX_MESSAGE_AGE;
193
+ }
194
+ // For frameworks like Express that automatically parse JSON
195
+ // bodies into Objects, re-stringify for signature testing
196
+ if (typeof body === 'object') {
197
+ // Escape forward slashes to ensure a matching signature
198
+ body = jsonStringifyWithEscapedUnicode(body).replace(/\//g, '\\/');
199
+ }
200
+ if (!validateSignature(body, headers, primaryKey, secondaryKey)) {
201
+ return false;
202
+ }
203
+ if (!validateDeliveryTimestamp(headers, maxMessageAge)) {
204
+ return false;
205
+ }
206
+ return true;
207
+ };
208
+ /**
209
+ * Create a new webhook on a given Box object, specified by type and ID.
210
+ *
211
+ * API Endpoint: '/webhooks'
212
+ * Method: POST
213
+ *
214
+ * @param {string} targetID - Box ID of the item to create webhook on
215
+ * @param {ItemType} targetType - Type of item the webhook will be created on
216
+ * @param {string} notificationURL - The URL of your application where Box will notify you of events triggers
217
+ * @param {WebhookTriggerType[]} triggerTypes - Array of event types that trigger notification for the target
218
+ * @param {Function} [callback] - Passed the new webhook information if it was acquired successfully
219
+ * @returns {Promise<Object>} A promise resolving to the new webhook object
220
+ */
221
+ Webhooks.prototype.create = function (targetID, targetType, notificationURL, triggerTypes, callback) {
222
+ var params = {
223
+ body: {
224
+ target: {
225
+ id: targetID,
226
+ type: targetType,
227
+ },
228
+ address: notificationURL,
229
+ triggers: triggerTypes,
230
+ },
231
+ };
232
+ var apiPath = url_path_1.default(BASE_PATH);
233
+ return this.client.wrapWithDefaultHandler(this.client.post)(apiPath, params, callback);
234
+ };
235
+ /**
236
+ * Returns a webhook object with the specified Webhook ID
237
+ *
238
+ * API Endpoint: '/webhooks/:webhookID'
239
+ * Method: GET
240
+ *
241
+ * @param {string} webhookID - ID of the webhook to retrieve
242
+ * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
243
+ * @param {Function} [callback] - Passed the webhook information if it was acquired successfully
244
+ * @returns {Promise<Object>} A promise resolving to the webhook object
245
+ */
246
+ Webhooks.prototype.get = function (webhookID, options, callback) {
247
+ var params = {
248
+ qs: options,
249
+ };
250
+ var apiPath = url_path_1.default(BASE_PATH, webhookID);
251
+ return this.client.wrapWithDefaultHandler(this.client.get)(apiPath, params, callback);
252
+ };
253
+ /**
254
+ * Get a list of webhooks that are active for the current application and user.
255
+ *
256
+ * API Endpoint: '/webhooks'
257
+ * Method: GET
258
+ *
259
+ * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
260
+ * @param {int} [options.limit=100] - The number of webhooks to return
261
+ * @param {string} [options.marker] - Pagination marker
262
+ * @param {Function} [callback] - Passed the list of webhooks if successful, error otherwise
263
+ * @returns {Promise<Object>} A promise resolving to the collection of webhooks
264
+ */
265
+ Webhooks.prototype.getAll = function (options, callback) {
266
+ var params = {
267
+ qs: options,
268
+ };
269
+ var apiPath = url_path_1.default(BASE_PATH);
270
+ return this.client.wrapWithDefaultHandler(this.client.get)(apiPath, params, callback);
271
+ };
272
+ /**
273
+ * Update a webhook
274
+ *
275
+ * API Endpoint: '/webhooks/:webhookID'
276
+ * Method: PUT
277
+ *
278
+ * @param {string} webhookID - The ID of the webhook to be updated
279
+ * @param {Object} updates - Webhook fields to update
280
+ * @param {string} [updates.address] - The new URL used by Box to send a notification when webhook is triggered
281
+ * @param {WebhookTriggerType[]} [updates.triggers] - The new events that triggers a notification
282
+ * @param {Function} [callback] - Passed the updated webhook information if successful, error otherwise
283
+ * @returns {Promise<Object>} A promise resolving to the updated webhook object
284
+ */
285
+ Webhooks.prototype.update = function (webhookID, updates, callback) {
286
+ var apiPath = url_path_1.default(BASE_PATH, webhookID), params = {
287
+ body: updates,
288
+ };
289
+ return this.client.wrapWithDefaultHandler(this.client.put)(apiPath, params, callback);
290
+ };
291
+ /**
292
+ * Delete a specified webhook by ID
293
+ *
294
+ * API Endpoint: '/webhooks/:webhookID'
295
+ * Method: DELETE
296
+ *
297
+ * @param {string} webhookID - ID of webhook to be deleted
298
+ * @param {Function} [callback] - Empty response body passed if successful.
299
+ * @returns {Promise<void>} A promise resolving to nothing
300
+ */
301
+ Webhooks.prototype.delete = function (webhookID, callback) {
302
+ var apiPath = url_path_1.default(BASE_PATH, webhookID);
303
+ return this.client.wrapWithDefaultHandler(this.client.del)(apiPath, null, callback);
304
+ };
305
+ /**
306
+ * Primary signature key to protect webhooks against attacks.
307
+ * @static
308
+ * @type {?string}
309
+ */
310
+ Webhooks.primarySignatureKey = null;
311
+ /**
312
+ * Secondary signature key to protect webhooks against attacks.
313
+ * @static
314
+ * @type {?string}
315
+ */
316
+ Webhooks.secondarySignatureKey = null;
317
+ return Webhooks;
318
+ }());
154
319
  /**
155
320
  * Enum of valid webhooks event triggers
156
321
  *
@@ -158,229 +323,59 @@ Webhooks.secondarySignatureKey = null;
158
323
  * @enum {WebhookTriggerType}
159
324
  */
160
325
  Webhooks.prototype.triggerTypes = {
161
- FILE: {
162
- UPLOADED: 'FILE.UPLOADED',
163
- PREVIEWED: 'FILE.PREVIEWED',
164
- DOWNLOADED: 'FILE.DOWNLOADED',
165
- TRASHED: 'FILE.TRASHED',
166
- DELETED: 'FILE.DELETED',
167
- RESTORED: 'FILE.RESTORED',
168
- COPIED: 'FILE.COPIED',
169
- MOVED: 'FILE.MOVED',
170
- LOCKED: 'FILE.LOCKED',
171
- UNLOCKED: 'FILE.UNLOCKED',
172
- RENAMED: 'FILE.RENAMED'
173
- },
174
- COMMENT: {
175
- CREATED: 'COMMENT.CREATED',
176
- UPDATED: 'COMMENT.UPDATED',
177
- DELETED: 'COMMENT.DELETED'
178
- },
179
- TASK_ASSIGNMENT: {
180
- CREATED: 'TASK_ASSIGNMENT.CREATED',
181
- UPDATED: 'TASK_ASSIGNMENT.UPDATED'
182
- },
183
- METADATA_INSTANCE: {
184
- CREATED: 'METADATA_INSTANCE.CREATED',
185
- UPDATED: 'METADATA_INSTANCE.UPDATED',
186
- DELETED: 'METADATA_INSTANCE.DELETED'
187
- },
188
- FOLDER: {
189
- CREATED: 'FOLDER.CREATED',
190
- DOWNLOADED: 'FOLDER.DOWNLOADED',
191
- RESTORED: 'FOLDER.RESTORED',
192
- DELETED: 'FOLDER.DELETED',
193
- COPIED: 'FOLDER.COPIED',
194
- MOVED: 'FOLDER.MOVED',
195
- TRASHED: 'FOLDER.TRASHED',
196
- RENAMED: 'FOLDER.RENAMED'
197
- },
198
- WEBHOOK: {
199
- DELETED: 'WEBHOOK.DELETED'
200
- },
201
- COLLABORATION: {
202
- CREATED: 'COLLABORATION.CREATED',
203
- ACCEPTED: 'COLLABORATION.ACCEPTED',
204
- REJECTED: 'COLLABORATION.REJECTED',
205
- REMOVED: 'COLLABORATION.REMOVED',
206
- UPDATED: 'COLLABORATION.UPDATED'
207
- },
208
- SHARED_LINK: {
209
- DELETED: 'SHARED_LINK.DELETED',
210
- CREATED: 'SHARED_LINK.CREATED',
211
- UPDATED: 'SHARED_LINK.UPDATED'
212
- }
326
+ FILE: {
327
+ UPLOADED: WebhookTriggerType.FILE_UPLOADED,
328
+ PREVIEWED: WebhookTriggerType.FILE_PREVIEWED,
329
+ DOWNLOADED: WebhookTriggerType.FILE_DOWNLOADED,
330
+ TRASHED: WebhookTriggerType.FILE_TRASHED,
331
+ DELETED: WebhookTriggerType.FILE_DELETED,
332
+ RESTORED: WebhookTriggerType.FILE_RESTORED,
333
+ COPIED: WebhookTriggerType.FILE_COPIED,
334
+ MOVED: WebhookTriggerType.FILE_MOVED,
335
+ LOCKED: WebhookTriggerType.FILE_LOCKED,
336
+ UNLOCKED: WebhookTriggerType.FILE_UNLOCKED,
337
+ RENAMED: WebhookTriggerType.FILE_RENAMED,
338
+ },
339
+ COMMENT: {
340
+ CREATED: WebhookTriggerType.COMMENT_CREATED,
341
+ UPDATED: WebhookTriggerType.COMMENT_UPDATED,
342
+ DELETED: WebhookTriggerType.COMMENT_DELETED,
343
+ },
344
+ TASK_ASSIGNMENT: {
345
+ CREATED: WebhookTriggerType.TASK_ASSIGNMENT_CREATED,
346
+ UPDATED: WebhookTriggerType.TASK_ASSIGNMENT_UPDATED,
347
+ },
348
+ METADATA_INSTANCE: {
349
+ CREATED: WebhookTriggerType.METADATA_INSTANCE_CREATED,
350
+ UPDATED: WebhookTriggerType.METADATA_INSTANCE_UPDATED,
351
+ DELETED: WebhookTriggerType.METADATA_INSTANCE_DELETED,
352
+ },
353
+ FOLDER: {
354
+ CREATED: WebhookTriggerType.FOLDER_CREATED,
355
+ DOWNLOADED: WebhookTriggerType.FOLDER_DOWNLOADED,
356
+ RESTORED: WebhookTriggerType.FOLDER_RESTORED,
357
+ DELETED: WebhookTriggerType.FOLDER_DELETED,
358
+ COPIED: WebhookTriggerType.FOLDER_COPIED,
359
+ MOVED: WebhookTriggerType.FOLDER_MOVED,
360
+ TRASHED: WebhookTriggerType.FOLDER_TRASHED,
361
+ RENAMED: WebhookTriggerType.FOLDER_RENAMED,
362
+ },
363
+ WEBHOOK: {
364
+ DELETED: WebhookTriggerType.WEBHOOK_DELETED,
365
+ },
366
+ COLLABORATION: {
367
+ CREATED: WebhookTriggerType.COLLABORATION_CREATED,
368
+ ACCEPTED: WebhookTriggerType.COLLABORATION_ACCEPTED,
369
+ REJECTED: WebhookTriggerType.COLLABORATION_REJECTED,
370
+ REMOVED: WebhookTriggerType.COLLABORATION_REMOVED,
371
+ UPDATED: WebhookTriggerType.COLLABORATION_UPDATED,
372
+ },
373
+ SHARED_LINK: {
374
+ DELETED: WebhookTriggerType.SHARED_LINK_DELETED,
375
+ CREATED: WebhookTriggerType.SHARED_LINK_CREATED,
376
+ UPDATED: WebhookTriggerType.SHARED_LINK_UPDATED,
377
+ },
213
378
  };
214
-
215
- /**
216
- * Create a new webhook on a given Box object, specified by type and ID.
217
- *
218
- * API Endpoint: '/webhooks'
219
- * Method: POST
220
- *
221
- * @param {string} targetID - Box ID of the item to create webhook on
222
- * @param {ItemType} targetType - Type of item the webhook will be created on
223
- * @param {string} notificationURL - The URL of your application where Box will notify you of events triggers
224
- * @param {WebhookTriggerType[]} triggerTypes - Array of event types that trigger notification for the target
225
- * @param {Function} [callback] - Passed the new webhook information if it was acquired successfully
226
- * @returns {Promise<Object>} A promise resolving to the new webhook object
227
- */
228
- Webhooks.prototype.create = function(targetID, targetType, notificationURL, triggerTypes, callback) {
229
- var params = {
230
- body: {
231
- target: {
232
- id: targetID,
233
- type: targetType
234
- },
235
- address: notificationURL,
236
- triggers: triggerTypes
237
- }
238
- };
239
-
240
- var apiPath = urlPath(BASE_PATH);
241
- return this.client.wrapWithDefaultHandler(this.client.post)(apiPath, params, callback);
242
- };
243
-
244
- /**
245
- * Returns a webhook object with the specified Webhook ID
246
- *
247
- * API Endpoint: '/webhooks/:webhookID'
248
- * Method: GET
249
- *
250
- * @param {string} webhookID - ID of the webhook to retrieve
251
- * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
252
- * @param {Function} [callback] - Passed the webhook information if it was acquired successfully
253
- * @returns {Promise<Object>} A promise resolving to the webhook object
254
- */
255
- Webhooks.prototype.get = function(webhookID, options, callback) {
256
- var params = {
257
- qs: options
258
- };
259
-
260
- var apiPath = urlPath(BASE_PATH, webhookID);
261
- return this.client.wrapWithDefaultHandler(this.client.get)(apiPath, params, callback);
262
- };
263
-
264
- /**
265
- * Get a list of webhooks that are active for the current application and user.
266
- *
267
- * API Endpoint: '/webhooks'
268
- * Method: GET
269
- *
270
- * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
271
- * @param {int} [options.limit=100] - The number of webhooks to return
272
- * @param {string} [options.marker] - Pagination marker
273
- * @param {Function} [callback] - Passed the list of webhooks if successful, error otherwise
274
- * @returns {Promise<Object>} A promise resolving to the collection of webhooks
275
- */
276
- Webhooks.prototype.getAll = function(options, callback) {
277
- var params = {
278
- qs: options
279
- };
280
-
281
- var apiPath = urlPath(BASE_PATH);
282
- return this.client.wrapWithDefaultHandler(this.client.get)(apiPath, params, callback);
283
- };
284
-
285
- /**
286
- * Update a webhook
287
- *
288
- * API Endpoint: '/webhooks/:webhookID'
289
- * Method: PUT
290
- *
291
- * @param {string} webhookID - The ID of the webhook to be updated
292
- * @param {Object} updates - Webhook fields to update
293
- * @param {string} [updates.address] - The new URL used by Box to send a notification when webhook is triggered
294
- * @param {WebhookTriggerType[]} [updates.triggers] - The new events that triggers a notification
295
- * @param {Function} [callback] - Passed the updated webhook information if successful, error otherwise
296
- * @returns {Promise<Object>} A promise resolving to the updated webhook object
297
- */
298
- Webhooks.prototype.update = function(webhookID, updates, callback) {
299
- var apiPath = urlPath(BASE_PATH, webhookID),
300
- params = {
301
- body: updates
302
- };
303
-
304
- return this.client.wrapWithDefaultHandler(this.client.put)(apiPath, params, callback);
305
- };
306
-
307
- /**
308
- * Delete a specified webhook by ID
309
- *
310
- * API Endpoint: '/webhooks/:webhookID'
311
- * Method: DELETE
312
- *
313
- * @param {string} webhookID - ID of webhook to be deleted
314
- * @param {Function} [callback] - Empty response body passed if successful.
315
- * @returns {Promise<void>} A promise resolving to nothing
316
- */
317
- Webhooks.prototype.delete = function(webhookID, callback) {
318
- var apiPath = urlPath(BASE_PATH, webhookID);
319
- return this.client.wrapWithDefaultHandler(this.client.del)(apiPath, null, callback);
320
- };
321
-
322
- /**
323
- * Sets primary and secondary signatures that are used to verify the Webhooks messages
324
- *
325
- * @param {string} primaryKey - The primary signature to verify the message with
326
- * @param {string} [secondaryKey] - The secondary signature to verify the message with
327
- * @returns {void}
328
- */
329
- Webhooks.setSignatureKeys = function(primaryKey, secondaryKey) {
330
- Webhooks.primarySignatureKey = primaryKey;
331
-
332
- if (typeof secondaryKey === 'string') {
333
- Webhooks.secondarySignatureKey = secondaryKey;
334
- }
335
- };
336
-
337
- /**
338
- * Validate a webhook message by verifying the signature and the delivery timestamp
339
- *
340
- * @param {string|Object} body - The request body of the webhook message
341
- * @param {Object} headers - The request headers of the webhook message
342
- * @param {string} [primaryKey] - The primary signature to verify the message with. If it is sent as a parameter,
343
- it overrides the static variable primarySignatureKey
344
- * @param {string} [secondaryKey] - The secondary signature to verify the message with. If it is sent as a parameter,
345
- it overrides the static variable primarySignatureKey
346
- * @param {int} [maxMessageAge] - The maximum message age (in seconds). Defaults to 10 minutes
347
- * @returns {boolean} - True or false
348
- */
349
- Webhooks.validateMessage = function(body, headers, primaryKey, secondaryKey, maxMessageAge) {
350
- if (!primaryKey && Webhooks.primarySignatureKey) {
351
- primaryKey = Webhooks.primarySignatureKey;
352
- }
353
-
354
- if (!secondaryKey && Webhooks.secondarySignatureKey) {
355
- secondaryKey = Webhooks.secondarySignatureKey;
356
- }
357
-
358
- if (typeof maxMessageAge !== 'number') {
359
- maxMessageAge = MAX_MESSAGE_AGE;
360
- }
361
-
362
- // For frameworks like Express that automatically parse JSON
363
- // bodies into Objects, re-stringify for signature testing
364
- if (typeof body === 'object') {
365
- // Escape forward slashes to ensure a matching signature
366
- body = jsonStringifyWithEscapedUnicode(body).replace(/\//g, '\\/');
367
- }
368
-
369
- if (!validateSignature(body, headers, primaryKey, secondaryKey)) {
370
- return false;
371
- }
372
-
373
- if (!validateDeliveryTimestamp(headers, maxMessageAge)) {
374
- return false;
375
- }
376
-
377
- return true;
378
- };
379
-
380
379
  Webhooks.prototype.validateMessage = Webhooks.validateMessage;
381
-
382
- /**
383
- * @module box-node-sdk/lib/managers/webhooks
384
- * @see {@Link Webhooks}
385
- */
386
380
  module.exports = Webhooks;
381
+ //# sourceMappingURL=webhooks.js.map