auth0 2.36.0 → 2.38.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 (46) hide show
  1. package/CHANGELOG.md +554 -0
  2. package/README.md +33 -11
  3. package/package.json +22 -15
  4. package/src/Auth0RestClient.js +72 -71
  5. package/src/RetryRestClient.js +78 -81
  6. package/src/auth/DatabaseAuthenticator.js +242 -259
  7. package/src/auth/OAUthWithIDTokenValidation.js +99 -103
  8. package/src/auth/OAuthAuthenticator.js +325 -344
  9. package/src/auth/PasswordlessAuthenticator.js +237 -252
  10. package/src/auth/TokensManager.js +231 -246
  11. package/src/auth/UsersManager.js +149 -160
  12. package/src/auth/idToken.js +22 -19
  13. package/src/auth/index.js +510 -581
  14. package/src/errors.js +23 -23
  15. package/src/index.js +3 -2
  16. package/src/management/ActionsManager.js +503 -585
  17. package/src/management/BlacklistedTokensManager.js +77 -84
  18. package/src/management/BrandingManager.js +141 -163
  19. package/src/management/ClientGrantsManager.js +132 -145
  20. package/src/management/ClientsManager.js +145 -161
  21. package/src/management/ConnectionsManager.js +215 -204
  22. package/src/management/CustomDomainsManager.js +140 -157
  23. package/src/management/DeviceCredentialsManager.js +95 -111
  24. package/src/management/EmailProviderManager.js +110 -128
  25. package/src/management/EmailTemplatesManager.js +96 -112
  26. package/src/management/GrantsManager.js +94 -101
  27. package/src/management/GuardianManager.js +381 -455
  28. package/src/management/HooksManager.js +283 -325
  29. package/src/management/JobsManager.js +310 -343
  30. package/src/management/LogStreamsManager.js +142 -158
  31. package/src/management/LogsManager.js +92 -99
  32. package/src/management/ManagementTokenProvider.js +112 -120
  33. package/src/management/MigrationsManager.js +81 -90
  34. package/src/management/OrganizationsManager.js +686 -761
  35. package/src/management/PromptsManager.js +160 -178
  36. package/src/management/ResourceServersManager.js +143 -160
  37. package/src/management/RolesManager.js +323 -369
  38. package/src/management/RulesConfigsManager.js +94 -110
  39. package/src/management/RulesManager.js +154 -176
  40. package/src/management/StatsManager.js +87 -103
  41. package/src/management/TenantManager.js +81 -97
  42. package/src/management/TicketsManager.js +107 -115
  43. package/src/management/UserBlocksManager.js +135 -162
  44. package/src/management/UsersManager.js +729 -852
  45. package/src/management/index.js +3094 -3619
  46. package/src/utils.js +31 -60
@@ -1,257 +1,242 @@
1
- var extend = require('util')._extend;
2
- var axios = require('axios');
3
-
4
- var ArgumentError = require('rest-facade').ArgumentError;
1
+ const axios = require('axios');
2
+ const { ArgumentError } = require('rest-facade');
5
3
 
6
4
  /**
7
- * @class TokensManager
8
5
  * Provides methods for getting token data and exchanging tokens.
9
- * @constructor
10
- * @memberOf module:auth
11
- *
12
- * @param {Object} options Manager options.
13
- * @param {String} options.baseUrl The auth0 account URL.
14
- * @param {String} [options.headers] Default request headers.
15
- * @param {String} [options.clientId] Default client ID.
16
- * @param {String} [options.clientSecret] Default client Secret.
17
- */
18
- var TokensManager = function(options) {
19
- if (typeof options !== 'object') {
20
- throw new ArgumentError('Missing tokens manager options');
21
- }
22
-
23
- if (typeof options.baseUrl !== 'string') {
24
- throw new ArgumentError('baseUrl field is required');
25
- }
26
-
27
- this.baseUrl = options.baseUrl;
28
- this.headers = options.headers || {};
29
- this.clientId = options.clientId || '';
30
- this.clientSecret = options.clientSecret || '';
31
- };
32
-
33
- /**
34
- * Given an ID token get the user profile linked to it.
35
- *
36
- * @method getInfo
37
- * @memberOf module:auth.TokensManager.prototype
38
- *
39
- * @example <caption>
40
- * Validates a JSON Web Token (signature and expiration) and returns the user
41
- * information associated with the user id (sub property) of the token. Find
42
- * more information in the
43
- * <a href="https://auth0.com/docs/auth-api#!#post--tokeninfo">API Docs</a>.
44
- * </caption>
45
- *
46
- * auth0.tokens.getInfo(token, function (err, tokenInfo) {
47
- * if (err) {
48
- * // Handle error.
49
- * }
50
- *
51
- * console.log(tokenInfo);
52
- * });
53
- *
54
- * @param {String} idToken User ID token.
55
- * @param {Function} [cb] Method callback.
56
- *
57
- * @return {Promise|undefined}
58
6
  */
59
- TokensManager.prototype.getInfo = function(idToken, cb) {
60
- var headers = extend({}, this.headers);
61
-
62
- if (idToken === null || idToken === undefined) {
63
- throw new ArgumentError('An ID token is required');
64
- }
65
-
66
- if (typeof idToken !== 'string' || idToken.trim().length === 0) {
67
- throw new ArgumentError('The ID token is not valid');
68
- }
69
-
70
- // Perform the request.
71
- var promise = axios({
72
- method: 'POST',
73
- url: this.baseUrl + '/tokeninfo',
74
- data: { id_token: idToken },
75
- headers: headers
76
- }).then(({ data }) => data);
77
-
78
- // Use callback if given.
79
- if (cb instanceof Function) {
80
- promise.then(cb.bind(null, null)).catch(cb);
81
- return;
82
- }
83
-
84
- return promise;
85
- };
86
-
87
- /**
88
- * Exchange the token of the logged in user with a token that is valid to call
89
- * the API (signed with the API secret).
90
- *
91
- * @method getDelegationToken
92
- * @memberOf module:auth.TokensManager.prototype
93
- *
94
- * @example <caption>
95
- * Given an existing token, this endpoint will generate a new token signed
96
- * with the target client secret. This is used to flow the identity of the
97
- * user from the application to an API or across different APIs that are
98
- * protected with different secrets. Find more information in the
99
- * <a href="https://auth0.com/docs/auth-api#!#post--delegation">API Docs</a>.
100
- * </caption>
101
- *
102
- * var data = {
103
- * id_token: '{ID_TOKEN}',
104
- * api_type: 'app',
105
- * target: '{TARGET}',
106
- * grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer'
107
- * };
108
- *
109
- * auth0.tokens.getDelegationToken(data, function (err, token) {
110
- * if (err) {
111
- * // Handle error.
112
- * }
113
- *
114
- * console.log(token);
115
- * });
116
- *
117
- * @param {Object} data Token data object.
118
- * @param {String} data.id_token User ID token.
119
- * @param {String} data.refresh_token User refresh token.
120
- * @param {String} data.target Target client ID.
121
- * @param {String} data.api_type The API to be used (aws, auth0, etc).
122
- * @param {String} data.grant_type Grant type (password, jwt, etc).
123
- * @param {Function} [cb] Callback function.
124
- *
125
- * @return {Promise|undefined}
126
- */
127
- TokensManager.prototype.getDelegationToken = function(data, cb) {
128
- var body = extend({ client_id: this.clientId }, data);
129
- var headers = this.headers;
130
-
131
- if (!data) {
132
- throw new ArgumentError('Missing token data object');
133
- }
134
-
135
- var hasIdToken = typeof data.id_token === 'string' && data.id_token.trim().length !== 0;
136
-
137
- var hasRefreshToken =
138
- typeof data.refresh_token === 'string' && data.refresh_token.trim().length !== 0;
139
-
140
- if (!hasIdToken && !hasRefreshToken) {
141
- throw new ArgumentError('one of id_token or refresh_token is required');
142
- }
143
-
144
- if (hasIdToken && hasRefreshToken) {
145
- throw new ArgumentError('id_token and refresh_token fields cannot be specified simulatenously');
146
- }
147
-
148
- if (typeof data.target !== 'string' || data.target.trim().length === 0) {
149
- throw new ArgumentError('target field is required');
150
- }
151
-
152
- if (typeof data.api_type !== 'string' || data.api_type.trim().length === 0) {
153
- throw new ArgumentError('api_type field is required');
154
- }
155
-
156
- if (typeof data.grant_type !== 'string' || data.grant_type.trim().length === 0) {
157
- throw new ArgumentError('grant_type field is required');
158
- }
159
-
160
- // Perform the request.
161
- var promise = axios({
162
- method: 'POST',
163
- url: this.baseUrl + '/delegation',
164
- data: body,
165
- headers: headers
166
- }).then(({ data }) => data);
167
-
168
- // Use callback if given.
169
- if (cb instanceof Function) {
170
- promise.then(cb.bind(null, null)).catch(cb);
171
- return;
172
- }
173
-
174
- return promise;
175
- };
176
-
177
- /**
178
- * Proactively revoke an issued refresh token.
179
- *
180
- * @method revokeRefreshToken
181
- * @memberOf module:auth.TokensManager.prototype
182
- *
183
- * @example <caption>
184
- * Given an existing refresh token, this endpoint will revoke it in order
185
- * to prevent unauthorized silently user authentication tokens refresh.
186
- * Find more information in the <a href="https://auth0.com/docs/api/authentication#revoke-refresh-token">API Docs</a>.
187
- * </caption>
188
- *
189
- * * var data = {
190
- * token: '{REFRESH_TOKEN}'
191
- * };
192
- *
193
- * auth0.tokens.revokeRefreshToken(data, function (err, _) {
194
- * if (err) {
195
- * // Handle error.
196
- * }
197
- *
198
- * // Do stuff.
199
- * });
200
- *
201
- * @param {Object} data Token data object.
202
- * @param {String} data.token User refresh token.
203
- * @param {String} [data.client_id] Target client ID.
204
- * @param {String} [data.client_secret] Target client secret.
205
- * @param {Function} [cb] Callback function.
206
- *
207
- * @return {Promise|undefined}
208
- */
209
- TokensManager.prototype.revokeRefreshToken = function(data, cb) {
210
- if (!data) {
211
- throw new ArgumentError('Missing token data object');
212
- }
213
-
214
- var hasToken = typeof data.token === 'string' && data.token.trim().length !== 0;
7
+ class TokensManager {
8
+ /**
9
+ * @param {object} options Manager options.
10
+ * @param {string} options.baseUrl The auth0 account URL.
11
+ * @param {string} [options.headers] Default request headers.
12
+ * @param {string} [options.clientId] Default client ID.
13
+ * @param {string} [options.clientSecret] Default client Secret.
14
+ */
15
+ constructor(options) {
16
+ if (typeof options !== 'object') {
17
+ throw new ArgumentError('Missing tokens manager options');
18
+ }
19
+
20
+ if (typeof options.baseUrl !== 'string') {
21
+ throw new ArgumentError('baseUrl field is required');
22
+ }
23
+
24
+ this.baseUrl = options.baseUrl;
25
+ this.headers = options.headers || {};
26
+ this.clientId = options.clientId || '';
27
+ this.clientSecret = options.clientSecret || '';
28
+ }
29
+
30
+ /**
31
+ * Given an ID token get the user profile linked to it.
32
+ *
33
+ * @example <caption>
34
+ * Validates a JSON Web Token (signature and expiration) and returns the user
35
+ * information associated with the user id (sub property) of the token. Find
36
+ * more information in the
37
+ * <a href="https://auth0.com/docs/auth-api#!#post--tokeninfo">API Docs</a>.
38
+ * </caption>
39
+ *
40
+ * auth0.tokens.getInfo(token, function (err, tokenInfo) {
41
+ * if (err) {
42
+ * // Handle error.
43
+ * }
44
+ *
45
+ * console.log(tokenInfo);
46
+ * });
47
+ * @param {string} idToken User ID token.
48
+ * @param {Function} [cb] Method callback.
49
+ * @returns {Promise|undefined}
50
+ */
51
+ getInfo(idToken, cb) {
52
+ const headers = { ...this.headers };
53
+
54
+ if (idToken === null || idToken === undefined) {
55
+ throw new ArgumentError('An ID token is required');
56
+ }
57
+
58
+ if (typeof idToken !== 'string' || idToken.trim().length === 0) {
59
+ throw new ArgumentError('The ID token is not valid');
60
+ }
61
+
62
+ // Perform the request.
63
+ const promise = axios({
64
+ method: 'POST',
65
+ url: `${this.baseUrl}/tokeninfo`,
66
+ data: { id_token: idToken },
67
+ headers,
68
+ }).then(({ data }) => data);
69
+
70
+ // Use callback if given.
71
+ if (cb instanceof Function) {
72
+ promise.then(cb.bind(null, null)).catch(cb);
73
+ return;
74
+ }
75
+
76
+ return promise;
77
+ }
78
+
79
+ /**
80
+ * Exchange the token of the logged in user with a token that is valid to call
81
+ * the API (signed with the API secret).
82
+ *
83
+ * @example <caption>
84
+ * Given an existing token, this endpoint will generate a new token signed
85
+ * with the target client secret. This is used to flow the identity of the
86
+ * user from the application to an API or across different APIs that are
87
+ * protected with different secrets. Find more information in the
88
+ * <a href="https://auth0.com/docs/auth-api#!#post--delegation">API Docs</a>.
89
+ * </caption>
90
+ *
91
+ * var data = {
92
+ * id_token: '{ID_TOKEN}',
93
+ * api_type: 'app',
94
+ * target: '{TARGET}',
95
+ * grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer'
96
+ * };
97
+ *
98
+ * auth0.tokens.getDelegationToken(data, function (err, token) {
99
+ * if (err) {
100
+ * // Handle error.
101
+ * }
102
+ *
103
+ * console.log(token);
104
+ * });
105
+ * @param {object} data Token data object.
106
+ * @param {string} data.id_token User ID token.
107
+ * @param {string} data.refresh_token User refresh token.
108
+ * @param {string} data.target Target client ID.
109
+ * @param {string} data.api_type The API to be used (aws, auth0, etc).
110
+ * @param {string} data.grant_type Grant type (password, jwt, etc).
111
+ * @param {Function} [cb] Callback function.
112
+ * @returns {Promise|undefined}
113
+ */
114
+ getDelegationToken(data, cb) {
115
+ const body = { client_id: this.clientId, ...data };
116
+ const { headers } = this;
117
+
118
+ if (!data) {
119
+ throw new ArgumentError('Missing token data object');
120
+ }
121
+
122
+ const hasIdToken = typeof data.id_token === 'string' && data.id_token.trim().length !== 0;
123
+
124
+ const hasRefreshToken =
125
+ typeof data.refresh_token === 'string' && data.refresh_token.trim().length !== 0;
126
+
127
+ if (!hasIdToken && !hasRefreshToken) {
128
+ throw new ArgumentError('one of id_token or refresh_token is required');
129
+ }
130
+
131
+ if (hasIdToken && hasRefreshToken) {
132
+ throw new ArgumentError(
133
+ 'id_token and refresh_token fields cannot be specified simulatenously'
134
+ );
135
+ }
136
+
137
+ if (typeof data.target !== 'string' || data.target.trim().length === 0) {
138
+ throw new ArgumentError('target field is required');
139
+ }
140
+
141
+ if (typeof data.api_type !== 'string' || data.api_type.trim().length === 0) {
142
+ throw new ArgumentError('api_type field is required');
143
+ }
144
+
145
+ if (typeof data.grant_type !== 'string' || data.grant_type.trim().length === 0) {
146
+ throw new ArgumentError('grant_type field is required');
147
+ }
148
+
149
+ // Perform the request.
150
+ const promise = axios({
151
+ method: 'POST',
152
+ url: `${this.baseUrl}/delegation`,
153
+ data: body,
154
+ headers,
155
+ }).then(({ data }) => data);
156
+
157
+ // Use callback if given.
158
+ if (cb instanceof Function) {
159
+ promise.then(cb.bind(null, null)).catch(cb);
160
+ return;
161
+ }
162
+
163
+ return promise;
164
+ }
165
+
166
+ /**
167
+ * Proactively revoke an issued refresh token.
168
+ *
169
+ * @example <caption>
170
+ * Given an existing refresh token, this endpoint will revoke it in order
171
+ * to prevent unauthorized silently user authentication tokens refresh.
172
+ * Find more information in the <a href="https://auth0.com/docs/api/authentication#revoke-refresh-token">API Docs</a>.
173
+ * </caption>
174
+ *
175
+ * var data = {
176
+ * token: '{REFRESH_TOKEN}'
177
+ * };
178
+ *
179
+ * auth0.tokens.revokeRefreshToken(data, function (err, _) {
180
+ * if (err) {
181
+ * // Handle error.
182
+ * }
183
+ *
184
+ * // Do stuff.
185
+ * });
186
+ * @param {object} data Token data object.
187
+ * @param {string} data.token User refresh token.
188
+ * @param {string} [data.client_id] Target client ID.
189
+ * @param {string} [data.client_secret] Target client secret.
190
+ * @param {Function} [cb] Callback function.
191
+ * @returns {Promise|undefined}
192
+ */
193
+ revokeRefreshToken(data, cb) {
194
+ if (!data) {
195
+ throw new ArgumentError('Missing token data object');
196
+ }
197
+
198
+ const hasToken = typeof data.token === 'string' && data.token.trim().length !== 0;
199
+
200
+ if (!hasToken) {
201
+ throw new ArgumentError('token property is required');
202
+ }
203
+
204
+ const hasClientId =
205
+ (data.client_id &&
206
+ typeof data.client_id === 'string' &&
207
+ data.client_id.trim().length !== 0) ||
208
+ this.clientId !== '';
209
+
210
+ if (!hasClientId) {
211
+ throw new ArgumentError(
212
+ 'Neither token data client_id property or constructor clientId property has been set'
213
+ );
214
+ }
215
+
216
+ const body = {
217
+ client_id: this.clientId,
218
+ client_secret: this.clientSecret,
219
+ ...data,
220
+ };
215
221
 
216
- if (!hasToken) {
217
- throw new ArgumentError('token property is required');
218
- }
222
+ const { headers } = this;
219
223
 
220
- var hasClientId =
221
- (data.client_id && typeof data.client_id === 'string' && data.client_id.trim().length !== 0) ||
222
- this.clientId !== '';
224
+ // Perform the request.
225
+ const promise = axios({
226
+ method: 'POST',
227
+ url: `${this.baseUrl}/oauth/revoke`,
228
+ data: body,
229
+ headers,
230
+ }).then(({ data }) => data);
223
231
 
224
- if (!hasClientId) {
225
- throw new ArgumentError(
226
- 'Neither token data client_id property or constructor clientId property has been set'
227
- );
228
- }
232
+ // Use callback if given.
233
+ if (cb instanceof Function) {
234
+ promise.then(cb.bind(null, null)).catch(cb);
235
+ return;
236
+ }
229
237
 
230
- var body = extend(
231
- {
232
- client_id: this.clientId,
233
- client_secret: this.clientSecret
234
- },
235
- data
236
- );
237
-
238
- var headers = this.headers;
239
-
240
- // Perform the request.
241
- var promise = axios({
242
- method: 'POST',
243
- url: this.baseUrl + '/oauth/revoke',
244
- data: body,
245
- headers: headers
246
- }).then(({ data }) => data);
247
-
248
- // Use callback if given.
249
- if (cb instanceof Function) {
250
- promise.then(cb.bind(null, null)).catch(cb);
251
- return;
238
+ return promise;
252
239
  }
253
-
254
- return promise;
255
- };
240
+ }
256
241
 
257
242
  module.exports = TokensManager;