auth0 2.36.1 → 2.38.1

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 +557 -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 +238 -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 -182
  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,11 +1,8 @@
1
- var extend = require('util')._extend;
2
- var sanitizeArguments = require('../utils').sanitizeArguments;
3
-
4
- var ArgumentError = require('rest-facade').ArgumentError;
5
- var RestClient = require('rest-facade').Client;
6
-
7
- var SanitizedError = require('../errors').SanitizedError;
8
- var OAUthWithIDTokenValidation = require('./OAUthWithIDTokenValidation');
1
+ const { sanitizeArguments } = require('../utils');
2
+ const { ArgumentError } = require('rest-facade');
3
+ const RestClient = require('rest-facade').Client;
4
+ const { SanitizedError } = require('../errors');
5
+ const OAUthWithIDTokenValidation = require('./OAUthWithIDTokenValidation');
9
6
 
10
7
  function getParamsFromOptions(options) {
11
8
  const params = {};
@@ -13,7 +10,7 @@ function getParamsFromOptions(options) {
13
10
  return params;
14
11
  }
15
12
  if (options.forwardedFor) {
16
- params._requestCustomizer = function(req) {
13
+ params._requestCustomizer = function (req) {
17
14
  req.set('auth0-forwarded-for', options.forwardedFor);
18
15
  };
19
16
  }
@@ -24,387 +21,371 @@ function getParamsFromOptions(options) {
24
21
  }
25
22
 
26
23
  /**
27
- * @class
28
24
  * Abstracts the sign-in, sign-up and change-password processes for Database &
29
25
  * Active Directory authentication services.
30
- * @constructor
31
- * @memberOf module:auth
32
- *
33
- * @param {Object} options Authenticator options.
34
- * @param {String} options.baseUrl The Auth0 account URL.
35
- * @param {String} options.domain AuthenticationClient server domain
36
- * @param {String} [options.clientId] Default client ID.
37
- * @param {String} [options.clientSecret] Default client Secret.
38
- * @param {Boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
39
26
  */
40
- var OAuthAuthenticator = function(options) {
41
- if (!options) {
42
- throw new ArgumentError('Missing authenticator options');
43
- }
27
+ class OAuthAuthenticator {
28
+ /**
29
+ * @param {object} options Authenticator options.
30
+ * @param {string} options.baseUrl The Auth0 account URL.
31
+ * @param {string} options.domain AuthenticationClient server domain
32
+ * @param {string} [options.clientId] Default client ID.
33
+ * @param {string} [options.clientSecret] Default client Secret.
34
+ * @param {boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
35
+ */
36
+ constructor(options) {
37
+ if (!options) {
38
+ throw new ArgumentError('Missing authenticator options');
39
+ }
40
+
41
+ if (typeof options !== 'object') {
42
+ throw new ArgumentError('The authenticator options must be an object');
43
+ }
44
+
45
+ /**
46
+ * Options object for the Rest Client instance.
47
+ *
48
+ * @type {object}
49
+ */
50
+ const clientOptions = {
51
+ errorCustomizer: SanitizedError,
52
+ errorFormatter: { message: 'message', name: 'error' },
53
+ headers: options.headers,
54
+ };
44
55
 
45
- if (typeof options !== 'object') {
46
- throw new ArgumentError('The authenticator options must be an object');
56
+ this.oauth = new RestClient(`${options.baseUrl}/oauth/:type`, clientOptions);
57
+ this.oauthWithIDTokenValidation = new OAUthWithIDTokenValidation(this.oauth, options);
58
+ this.clientId = options.clientId;
59
+ this.clientSecret = options.clientSecret;
47
60
  }
48
61
 
49
62
  /**
50
- * Options object for the Rest Client instance.
63
+ * Sign in using a username and password.
64
+ *
65
+ * @example <caption>
66
+ * Given the user's credentials and the connection specified, it
67
+ * will return a JSON with the access_token and id_token.
68
+ * More information in the
69
+ *
70
+ * <a href="https://auth0.com/docs/api/authentication#database-ad-ldap-active-">
71
+ * API Docs
72
+ * </a>.
73
+ * </caption>
74
+ *
75
+ * var data = {
76
+ * client_id: '{CLIENT_ID}', // Optional field.
77
+ * username: '{USERNAME}',
78
+ * password: '{PASSWORD}',
79
+ * connection: '{CONNECTION_NAME}',
80
+ * scope: 'openid' // Optional field.
81
+ * };
82
+ *
83
+ * auth0.oauth.signIn(data, function (err, userData) {
84
+ * if (err) {
85
+ * // Handle error.
86
+ * }
51
87
  *
52
- * @type {Object}
88
+ * console.log(userData);
89
+ * });
90
+ * @param {object} userData User credentials object.
91
+ * @param {string} userData.username Username.
92
+ * @param {string} userData.password User password.
93
+ * @param {string} userData.connection The identity provider in use.
94
+ * @param {object} [options] Additional options.
95
+ * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
96
+ * @param {Function} cb Callback
97
+ * @returns {Promise|undefined}
53
98
  */
54
- var clientOptions = {
55
- errorCustomizer: SanitizedError,
56
- errorFormatter: { message: 'message', name: 'error' },
57
- headers: options.headers
58
- };
59
-
60
- this.oauth = new RestClient(options.baseUrl + '/oauth/:type', clientOptions);
61
- this.oauthWithIDTokenValidation = new OAUthWithIDTokenValidation(this.oauth, options);
62
- this.clientId = options.clientId;
63
- this.clientSecret = options.clientSecret;
64
- };
65
-
66
- /**
67
- * Sign in using a username and password.
68
- *
69
- * @method signIn
70
- * @memberOf module:auth.OAuthAuthenticator.prototype
71
- *
72
- * @example <caption>
73
- * Given the user's credentials and the connection specified, it
74
- * will return a JSON with the access_token and id_token.
75
- * More information in the
76
- * <a href="https://auth0.com/docs/api/authentication#database-ad-ldap-active-">
77
- * API Docs
78
- * </a>.
79
- * </caption>
80
- *
81
- * var data = {
82
- * client_id: '{CLIENT_ID}', // Optional field.
83
- * username: '{USERNAME}',
84
- * password: '{PASSWORD}',
85
- * connection: '{CONNECTION_NAME}',
86
- * scope: 'openid' // Optional field.
87
- * };
88
- *
89
- * auth0.oauth.signIn(data, function (err, userData) {
90
- * if (err) {
91
- * // Handle error.
92
- * }
93
- *
94
- * console.log(userData);
95
- * });
96
- *
97
- * @param {Object} userData User credentials object.
98
- * @param {String} userData.username Username.
99
- * @param {String} userData.password User password.
100
- * @param {String} userData.connection The identity provider in use.
101
- * @param {Object} [options] Additional options.
102
- * @param {String} [options.forwardedFor] Value to be used for auth0-forwarded-for header
103
- *
104
- * @return {Promise|undefined}
105
- */
106
- OAuthAuthenticator.prototype.signIn = function(userData, options, cb) {
107
- var { options, cb } = sanitizeArguments(options, cb);
108
- var defaultParams = {
109
- type: 'ro'
110
- };
111
- var params = extend(defaultParams, getParamsFromOptions(options));
112
- var defaultFields = {
113
- client_id: this.clientId,
114
- grant_type: 'password',
115
- scope: 'openid'
116
- };
117
- var data = extend(defaultFields, userData);
118
-
119
- if (!userData || typeof userData !== 'object') {
120
- throw new ArgumentError('Missing user data object');
121
- }
122
-
123
- if (
124
- params.type === 'ro' &&
125
- (typeof data.connection !== 'string' || data.connection.split().length === 0)
126
- ) {
127
- throw new ArgumentError('connection field is required');
128
- }
99
+ signIn(userData, options, cb) {
100
+ if (!userData || typeof userData !== 'object') {
101
+ throw new ArgumentError('Missing user data object');
102
+ }
103
+
104
+ const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
105
+ const params = {
106
+ type: 'ro',
107
+ ...getParamsFromOptions(sanitizedOptions),
108
+ };
129
109
 
130
- if (cb && cb instanceof Function) {
131
- return this.oauthWithIDTokenValidation.create(params, data, cb);
132
- }
110
+ const data = {
111
+ client_id: this.clientId,
112
+ grant_type: 'password',
113
+ scope: 'openid',
114
+ ...userData,
115
+ };
133
116
 
134
- return this.oauthWithIDTokenValidation.create(params, data);
135
- };
117
+ if (
118
+ params.type === 'ro' &&
119
+ (typeof data.connection !== 'string' || data.connection.split().length === 0)
120
+ ) {
121
+ throw new ArgumentError('connection field is required');
122
+ }
136
123
 
137
- /**
138
- * Sign in using a username and password
139
- *
140
- * @method passwordGrant
141
- * @memberOf module:auth.OAuthAuthenticator.prototype
142
- *
143
- * @example <caption>
144
- * Given the user's credentials perform the OAuth password grant
145
- * or Password Realm grant if a realm is provided,
146
- * it will return a JSON with the access_token and id_token.
147
- * More information in the
148
- * <a href="https://auth0.com/docs/api/authentication#resource-owner-password">
149
- * API Docs
150
- * </a>.
151
- * </caption>
152
- *
153
- * var data = {
154
- * client_id: '{CLIENT_ID}', // Optional field.
155
- * username: '{USERNAME}',
156
- * password: '{PASSWORD}',
157
- * realm: '{CONNECTION_NAME}', // Optional field.
158
- * scope: 'openid' // Optional field.
159
- * };
160
- *
161
- * auth0.oauth.passwordGrant(data, function (err, userData) {
162
- * if (err) {
163
- * // Handle error.
164
- * }
165
- *
166
- * console.log(userData);
167
- * });
168
- *
169
- * @param {Object} userData User credentials object.
170
- * @param {String} userData.username Username.
171
- * @param {String} userData.password User password.
172
- * @param {String} [userData.realm] Name of the realm to use to authenticate or the connection name
173
- * @param {Object} [options] Additional options.
174
- * @param {String} [options.forwardedFor] Value to be used for auth0-forwarded-for header
175
- *
176
- * @return {Promise|undefined}
177
- */
178
- OAuthAuthenticator.prototype.passwordGrant = function(userData, options, cb) {
179
- var { options, cb } = sanitizeArguments(options, cb);
180
- var defaultParams = {
181
- type: 'token'
182
- };
183
- var params = extend(defaultParams, getParamsFromOptions(options));
184
- var defaultFields = {
185
- client_id: this.clientId,
186
- client_secret: this.clientSecret,
187
- grant_type: 'password'
188
- };
189
- var data = extend(defaultFields, userData);
190
-
191
- if (!userData || typeof userData !== 'object') {
192
- throw new ArgumentError('Missing user data object');
193
- }
124
+ if (sanitizedCb && sanitizedCb instanceof Function) {
125
+ return this.oauthWithIDTokenValidation.create(params, data, sanitizedCb);
126
+ }
194
127
 
195
- if (typeof data.username !== 'string' || data.username.split().length === 0) {
196
- throw new ArgumentError('username field is required');
128
+ return this.oauthWithIDTokenValidation.create(params, data);
197
129
  }
198
130
 
199
- if (typeof data.password !== 'string' || data.password.split().length === 0) {
200
- throw new ArgumentError('password field is required');
201
- }
131
+ /**
132
+ * Sign in using a username and password
133
+ *
134
+ * @example <caption>
135
+ * Given the user's credentials perform the OAuth password grant
136
+ * or Password Realm grant if a realm is provided,
137
+ * it will return a JSON with the access_token and id_token.
138
+ *
139
+ * More information in the
140
+ * <a href="https://auth0.com/docs/api/authentication#resource-owner-password">
141
+ * API Docs
142
+ * </a>.
143
+ * </caption>
144
+ *
145
+ * var data = {
146
+ * client_id: '{CLIENT_ID}', // Optional field.
147
+ * username: '{USERNAME}',
148
+ * password: '{PASSWORD}',
149
+ * realm: '{CONNECTION_NAME}', // Optional field.
150
+ * scope: 'openid' // Optional field.
151
+ * };
152
+ *
153
+ * auth0.oauth.passwordGrant(data, function (err, userData) {
154
+ * if (err) {
155
+ * // Handle error.
156
+ * }
157
+ *
158
+ * console.log(userData);
159
+ * });
160
+ * @param {object} userData User credentials object.
161
+ * @param {string} userData.username Username.
162
+ * @param {string} userData.password User password.
163
+ * @param {string} [userData.realm] Name of the realm to use to authenticate or the connection name
164
+ * @param {object} [options] Additional options.
165
+ * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
166
+ * @param {Function} cb Callback
167
+ * @returns {Promise|undefined}
168
+ */
169
+ passwordGrant(userData, options, cb) {
170
+ const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
171
+
172
+ if (!userData || typeof userData !== 'object') {
173
+ throw new ArgumentError('Missing user data object');
174
+ }
175
+
176
+ const data = {
177
+ client_id: this.clientId,
178
+ client_secret: this.clientSecret,
179
+ grant_type: 'password',
180
+ ...userData,
181
+ };
202
182
 
203
- if (typeof data.realm === 'string' && data.realm.split().length !== 0) {
204
- data.grant_type = 'http://auth0.com/oauth/grant-type/password-realm';
205
- }
183
+ if (typeof data.username !== 'string' || data.username.split().length === 0) {
184
+ throw new ArgumentError('username field is required');
185
+ }
206
186
 
207
- if (cb && cb instanceof Function) {
208
- return this.oauthWithIDTokenValidation.create(params, data, cb);
209
- }
187
+ if (typeof data.password !== 'string' || data.password.split().length === 0) {
188
+ throw new ArgumentError('password field is required');
189
+ }
210
190
 
211
- return this.oauthWithIDTokenValidation.create(params, data);
212
- };
191
+ if (typeof data.realm === 'string' && data.realm.split().length !== 0) {
192
+ data.grant_type = 'http://auth0.com/oauth/grant-type/password-realm';
193
+ }
213
194
 
214
- /**
215
- * Exchange a refresh token
216
- *
217
- * @method refreshToken
218
- * @memberOf module:auth.OAuthAuthenticator.prototype
219
- *
220
- * @example <caption>
221
- * Given a refresh token from a previous authentication request
222
- * it will return a JSON with the access_token and id_token if
223
- * the openid scope was originally included.
224
- * More information in the
225
- * <a href="https://auth0.com/docs/api/authentication#refresh-token">
226
- * API Docs
227
- * </a>.
228
- * </caption>
229
- *
230
- * var data = {
231
- * refresh_token: '{REFRESH_TOKEN}',
232
- * };
233
- *
234
- * auth0.oauth.refreshToken(data, function (err, data) {
235
- * if (err) {
236
- * // Handle error.
237
- * }
238
- *
239
- * console.log(data);
240
- * });
241
- *
242
- * @param {Object} data Data object.
243
- * @param {String} data.refresh_token Refresh token.
244
- *
245
- * @return {Promise|undefined}
246
- */
247
- OAuthAuthenticator.prototype.refreshToken = function(data, cb) {
248
- if (!data || typeof data !== 'object') {
249
- throw new ArgumentError('Missing data object');
250
- }
195
+ const params = {
196
+ type: 'token',
197
+ ...getParamsFromOptions(sanitizedOptions),
198
+ };
251
199
 
252
- var defaultFields = {
253
- client_id: this.clientId,
254
- client_secret: this.clientSecret,
255
- grant_type: 'refresh_token'
256
- };
200
+ if (sanitizedCb && sanitizedCb instanceof Function) {
201
+ return this.oauthWithIDTokenValidation.create(params, data, sanitizedCb);
202
+ }
257
203
 
258
- var data = extend(defaultFields, data);
259
- if (typeof data.refresh_token !== 'string' || data.refresh_token.split().length === 0) {
260
- throw new ArgumentError('refresh_token is required');
204
+ return this.oauthWithIDTokenValidation.create(params, data);
261
205
  }
262
206
 
263
- var params = {
264
- type: 'token'
265
- };
207
+ /**
208
+ * Exchange a refresh token
209
+ *
210
+ * @example <caption>
211
+ * Given a refresh token from a previous authentication request
212
+ * it will return a JSON with the access_token and id_token if
213
+ * the openid scope was originally included.
214
+ * More information in the
215
+ * <a href="https://auth0.com/docs/api/authentication#refresh-token">
216
+ * API Docs
217
+ * </a>.
218
+ * </caption>
219
+ *
220
+ * var data = {
221
+ * refresh_token: '{REFRESH_TOKEN}',
222
+ * };
223
+ *
224
+ * auth0.oauth.refreshToken(data, function (err, data) {
225
+ * if (err) {
226
+ * // Handle error.
227
+ * }
228
+ *
229
+ * console.log(data);
230
+ * });
231
+ * @param {object} data Data object.
232
+ * @param {string} data.refresh_token Refresh token.
233
+ * @param {Function} cb Callback
234
+ * @returns {Promise|undefined}
235
+ */
236
+ refreshToken(data, cb) {
237
+ if (!data || typeof data !== 'object') {
238
+ throw new ArgumentError('Missing data object');
239
+ }
240
+
241
+ data = {
242
+ client_id: this.clientId,
243
+ client_secret: this.clientSecret,
244
+ grant_type: 'refresh_token',
245
+ ...data,
246
+ };
266
247
 
267
- if (cb && cb instanceof Function) {
268
- return this.oauth.create(params, data, cb);
269
- }
270
- return this.oauth.create(params, data);
271
- };
248
+ if (typeof data.refresh_token !== 'string' || data.refresh_token.split().length === 0) {
249
+ throw new ArgumentError('refresh_token is required');
250
+ }
272
251
 
273
- /**
274
- * Sign in using a social provider access token.
275
- *
276
- * @method socialSignIn
277
- * @memberOf module:auth.OAuthAuthenticator.prototype
278
- *
279
- * @param {Object} data User credentials object.
280
- * @param {String} data.access_token User access token.
281
- * @param {String} data.connection Identity provider.
282
- *
283
- * @return {Promise|undefined}
284
- */
285
- OAuthAuthenticator.prototype.socialSignIn = function(data, cb) {
286
- var params = {
287
- type: 'access_token'
288
- };
252
+ const params = {
253
+ type: 'token',
254
+ };
289
255
 
290
- if (typeof data !== 'object') {
291
- throw new ArgumentError('Missing user credential objects');
256
+ if (cb && cb instanceof Function) {
257
+ return this.oauth.create(params, data, cb);
258
+ }
259
+ return this.oauth.create(params, data);
292
260
  }
293
261
 
294
- if (typeof data.access_token !== 'string' || data.access_token.trim().length === 0) {
295
- throw new ArgumentError('access_token field is required');
296
- }
262
+ /**
263
+ * Sign in using a social provider access token.
264
+ *
265
+ * @param {object} data User credentials object.
266
+ * @param {string} data.access_token User access token.
267
+ * @param {string} data.connection Identity provider.
268
+ * @param {Function} cb Callback
269
+ * @returns {Promise|undefined}
270
+ */
271
+ socialSignIn(data, cb) {
272
+ const params = {
273
+ type: 'access_token',
274
+ };
297
275
 
298
- if (typeof data.connection !== 'string' || data.connection.trim().length === 0) {
299
- throw new ArgumentError('connection field is required');
300
- }
276
+ if (typeof data !== 'object') {
277
+ throw new ArgumentError('Missing user credential objects');
278
+ }
301
279
 
302
- if (cb && cb instanceof Function) {
303
- return this.oauth.create(params, data, cb);
304
- }
280
+ if (typeof data.access_token !== 'string' || data.access_token.trim().length === 0) {
281
+ throw new ArgumentError('access_token field is required');
282
+ }
305
283
 
306
- return this.oauth.create(params, data);
307
- };
284
+ if (typeof data.connection !== 'string' || data.connection.trim().length === 0) {
285
+ throw new ArgumentError('connection field is required');
286
+ }
308
287
 
309
- OAuthAuthenticator.prototype.clientCredentialsGrant = function(options, cb) {
310
- var params = {
311
- type: 'token'
312
- };
288
+ if (cb && cb instanceof Function) {
289
+ return this.oauth.create(params, data, cb);
290
+ }
313
291
 
314
- var defaultFields = {
315
- grant_type: 'client_credentials',
316
- client_id: this.clientId,
317
- client_secret: this.clientSecret
318
- };
292
+ return this.oauth.create(params, data);
293
+ }
319
294
 
320
- var data = extend(defaultFields, options);
295
+ clientCredentialsGrant(options, cb) {
296
+ if (!options || typeof options !== 'object') {
297
+ throw new ArgumentError('Missing options object');
298
+ }
321
299
 
322
- if (!options || typeof options !== 'object') {
323
- throw new ArgumentError('Missing options object');
324
- }
300
+ const data = {
301
+ grant_type: 'client_credentials',
302
+ client_id: this.clientId,
303
+ client_secret: this.clientSecret,
304
+ ...options,
305
+ };
325
306
 
326
- if (!data.client_id || data.client_id.trim().length === 0) {
327
- throw new ArgumentError('client_id field is required');
328
- }
307
+ if (!data.client_id || data.client_id.trim().length === 0) {
308
+ throw new ArgumentError('client_id field is required');
309
+ }
329
310
 
330
- if (!data.client_secret || data.client_secret.trim().length === 0) {
331
- throw new ArgumentError('client_secret field is required');
332
- }
311
+ if (!data.client_secret || data.client_secret.trim().length === 0) {
312
+ throw new ArgumentError('client_secret field is required');
313
+ }
333
314
 
334
- if (cb && cb instanceof Function) {
335
- return this.oauth.create(params, data, cb);
336
- }
315
+ const params = {
316
+ type: 'token',
317
+ };
337
318
 
338
- return this.oauth.create(params, data);
339
- };
319
+ if (cb && cb instanceof Function) {
320
+ return this.oauth.create(params, data, cb);
321
+ }
340
322
 
341
- /**
342
- * Sign in using an authorization code
343
- *
344
- * @method authorizationCodeGrant
345
- * @memberOf module:auth.OAuthAuthenticator.prototype
346
- *
347
- * @example <caption>
348
- * Given the code returned in the URL params after the redirect
349
- * from successful authentication, exchange the code for auth0
350
- * credentials. It will return JSON with the access_token and id_token.
351
- * More information in the
352
- * <a href="https://auth0.com/docs/api/authentication#authorization-code-grant">
353
- * API Docs
354
- * </a>.
355
- * </caption>
356
- *
357
- * var options = {
358
- * code: '{CODE}',
359
- * redirect_uri: '{REDIRECT_URI}',
360
- * organization: '{ORGANIZATION_ID}' // Optiional field.
361
- * };
362
- *
363
- * auth0.oauth.authorizationCodeGrant(options, function (err, userData) {
364
- * if (err) {
365
- * // Handle error.
366
- * }
367
- *
368
- * console.log(userData);
369
- * });
370
- *
371
- * @param {Object} options Authorization code payload
372
- * @param {String} options.organization Organization ID
373
- * @param {String} options.code Code in URL returned after authentication
374
- * @param {String} options.redirect_uri The URL to which Auth0 will redirect the browser after authorization has been granted by the user.
375
- *
376
- * @return {Promise|undefined}
377
- */
378
- OAuthAuthenticator.prototype.authorizationCodeGrant = function(options, cb) {
379
- var params = {
380
- type: 'token'
381
- };
323
+ return this.oauth.create(params, data);
324
+ }
382
325
 
383
- var defaultFields = {
384
- grant_type: 'authorization_code',
385
- client_id: this.clientId,
386
- client_secret: this.clientSecret
387
- };
326
+ /**
327
+ * Sign in using an authorization code
328
+ *
329
+ * @example <caption>
330
+ * Given the code returned in the URL params after the redirect
331
+ * from successful authentication, exchange the code for auth0
332
+ * credentials. It will return JSON with the access_token and id_token.
333
+ * More information in the
334
+ * <a href="https://auth0.com/docs/api/authentication#authorization-code-grant">
335
+ * API Docs
336
+ * </a>.
337
+ * </caption>
338
+ *
339
+ * var options = {
340
+ * code: '{CODE}',
341
+ * redirect_uri: '{REDIRECT_URI}',
342
+ * organization: '{ORGANIZATION_ID}' // Optiional field.
343
+ * };
344
+ *
345
+ * auth0.oauth.authorizationCodeGrant(options, function (err, userData) {
346
+ * if (err) {
347
+ * // Handle error.
348
+ * }
349
+ *
350
+ * console.log(userData);
351
+ * });
352
+ * @param {object} options Authorization code payload
353
+ * @param {string} options.organization Organization ID
354
+ * @param {string} options.code Code in URL returned after authentication
355
+ * @param {string} options.redirect_uri The URL to which Auth0 will redirect the browser after authorization has been granted by the user.
356
+ * @param {Function} cb Callback
357
+ * @returns {Promise|undefined}
358
+ */
359
+ authorizationCodeGrant(options, cb) {
360
+ if (!options || typeof options !== 'object') {
361
+ throw new ArgumentError('Missing options object');
362
+ }
363
+
364
+ const data = {
365
+ grant_type: 'authorization_code',
366
+ client_id: this.clientId,
367
+ client_secret: this.clientSecret,
368
+ ...options,
369
+ };
388
370
 
389
- var data = extend(defaultFields, options);
371
+ if (!data.code || data.code.trim().length === 0) {
372
+ throw new ArgumentError('code field is required');
373
+ }
390
374
 
391
- if (!options || typeof options !== 'object') {
392
- throw new ArgumentError('Missing options object');
393
- }
375
+ if (!data.redirect_uri || data.redirect_uri.trim().length === 0) {
376
+ throw new ArgumentError('redirect_uri field is required');
377
+ }
394
378
 
395
- if (!data.code || data.code.trim().length === 0) {
396
- throw new ArgumentError('code field is required');
397
- }
379
+ const params = {
380
+ type: 'token',
381
+ };
398
382
 
399
- if (!data.redirect_uri || data.redirect_uri.trim().length === 0) {
400
- throw new ArgumentError('redirect_uri field is required');
401
- }
383
+ if (cb && cb instanceof Function) {
384
+ return this.oauthWithIDTokenValidation.create(params, data, cb);
385
+ }
402
386
 
403
- if (cb && cb instanceof Function) {
404
- return this.oauthWithIDTokenValidation.create(params, data, cb);
387
+ return this.oauthWithIDTokenValidation.create(params, data);
405
388
  }
406
-
407
- return this.oauthWithIDTokenValidation.create(params, data);
408
- };
389
+ }
409
390
 
410
391
  module.exports = OAuthAuthenticator;