auth0 3.3.0 → 4.0.0-beta.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 (51) hide show
  1. package/README.md +19 -15
  2. package/package.json +47 -33
  3. package/CHANGELOG.md +0 -673
  4. package/src/Auth0RestClient.js +0 -123
  5. package/src/RetryRestClient.js +0 -105
  6. package/src/auth/DatabaseAuthenticator.js +0 -282
  7. package/src/auth/OAUthWithIDTokenValidation.js +0 -124
  8. package/src/auth/OAuthAuthenticator.js +0 -405
  9. package/src/auth/PasswordlessAuthenticator.js +0 -303
  10. package/src/auth/TokensManager.js +0 -254
  11. package/src/auth/UsersManager.js +0 -161
  12. package/src/auth/clientAuthentication.js +0 -86
  13. package/src/auth/idToken.js +0 -167
  14. package/src/auth/index.js +0 -573
  15. package/src/errors.js +0 -60
  16. package/src/index.js +0 -11
  17. package/src/index.mjs +0 -11
  18. package/src/management/ActionsManager.js +0 -526
  19. package/src/management/AttackProtectionManager.js +0 -158
  20. package/src/management/BaseManager.js +0 -41
  21. package/src/management/BlacklistedTokensManager.js +0 -67
  22. package/src/management/BrandingManager.js +0 -250
  23. package/src/management/ClientCredentialsManager.js +0 -112
  24. package/src/management/ClientGrantsManager.js +0 -124
  25. package/src/management/ClientsManager.js +0 -184
  26. package/src/management/ConnectionsManager.js +0 -213
  27. package/src/management/CustomDomainsManager.js +0 -141
  28. package/src/management/DeviceCredentialsManager.js +0 -91
  29. package/src/management/EmailProviderManager.js +0 -101
  30. package/src/management/EmailTemplatesManager.js +0 -90
  31. package/src/management/GrantsManager.js +0 -84
  32. package/src/management/GuardianManager.js +0 -361
  33. package/src/management/HooksManager.js +0 -295
  34. package/src/management/JobsManager.js +0 -324
  35. package/src/management/LogStreamsManager.js +0 -136
  36. package/src/management/LogsManager.js +0 -82
  37. package/src/management/ManagementTokenProvider.js +0 -144
  38. package/src/management/MigrationsManager.js +0 -76
  39. package/src/management/OrganizationsManager.js +0 -721
  40. package/src/management/PromptsManager.js +0 -162
  41. package/src/management/ResourceServersManager.js +0 -143
  42. package/src/management/RolesManager.js +0 -329
  43. package/src/management/RulesConfigsManager.js +0 -86
  44. package/src/management/RulesManager.js +0 -148
  45. package/src/management/StatsManager.js +0 -84
  46. package/src/management/TenantManager.js +0 -76
  47. package/src/management/TicketsManager.js +0 -104
  48. package/src/management/UserBlocksManager.js +0 -121
  49. package/src/management/UsersManager.js +0 -882
  50. package/src/management/index.js +0 -3405
  51. package/src/utils.js +0 -60
@@ -1,405 +0,0 @@
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');
6
- const { addClientAuthentication } = require('./clientAuthentication');
7
-
8
- function getParamsFromOptions(options) {
9
- const params = {};
10
- if (!options || typeof options !== 'object') {
11
- return params;
12
- }
13
- if (options.forwardedFor) {
14
- params._requestCustomizer = function (req) {
15
- req.set('auth0-forwarded-for', options.forwardedFor);
16
- };
17
- }
18
- if (options.type) {
19
- params.type = options.type;
20
- }
21
- return params;
22
- }
23
-
24
- /**
25
- * Abstracts the sign-in, sign-up and change-password processes for Database &
26
- * Active Directory authentication services.
27
- */
28
- class OAuthAuthenticator {
29
- /**
30
- * @param {object} options Authenticator options.
31
- * @param {string} options.baseUrl The Auth0 account URL.
32
- * @param {string} options.domain AuthenticationClient server domain
33
- * @param {string} [options.clientId] Default client ID.
34
- * @param {string} [options.clientSecret] Default client Secret.
35
- * @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT.
36
- * @param {string} [options.clientAssertionSigningAlg] Default 'RS256'.
37
- * @param {boolean} [options.__bypassIdTokenValidation] Whether the id_token should be validated or not
38
- * @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
39
- */
40
- constructor(options) {
41
- if (!options) {
42
- throw new ArgumentError('Missing authenticator options');
43
- }
44
-
45
- if (typeof options !== 'object') {
46
- throw new ArgumentError('The authenticator options must be an object');
47
- }
48
-
49
- /**
50
- * Options object for the Rest Client instance.
51
- *
52
- * @type {object}
53
- */
54
- const clientOptions = {
55
- errorCustomizer: SanitizedError,
56
- errorFormatter: { message: 'message', name: 'error' },
57
- headers: options.headers,
58
- proxy: options.proxy,
59
- };
60
-
61
- this.oauth = new RestClient(`${options.baseUrl}/oauth/:type`, clientOptions);
62
- this.oauthWithIDTokenValidation = new OAUthWithIDTokenValidation(this.oauth, options);
63
- this.domain = options.domain;
64
- this.clientId = options.clientId;
65
- this.clientSecret = options.clientSecret;
66
- this.clientAssertionSigningKey = options.clientAssertionSigningKey;
67
- this.clientAssertionSigningAlg = options.clientAssertionSigningAlg;
68
- }
69
-
70
- _addClientAuthentication(payload, required) {
71
- return addClientAuthentication({
72
- payload,
73
- required,
74
- domain: this.domain,
75
- clientSecret: this.clientSecret,
76
- clientAssertionSigningKey: this.clientAssertionSigningKey,
77
- clientAssertionSigningAlg: this.clientAssertionSigningAlg,
78
- });
79
- }
80
-
81
- /**
82
- * Sign in using a username and password.
83
- *
84
- * @example <caption>
85
- * Given the user's credentials and the connection specified, it
86
- * will return a JSON with the access_token and id_token.
87
- * More information in the
88
- *
89
- * <a href="https://auth0.com/docs/api/authentication#database-ad-ldap-active-">
90
- * API Docs
91
- * </a>.
92
- * </caption>
93
- *
94
- * var data = {
95
- * client_id: '{CLIENT_ID}', // Optional field.
96
- * username: '{USERNAME}',
97
- * password: '{PASSWORD}',
98
- * connection: '{CONNECTION_NAME}',
99
- * scope: 'openid' // Optional field.
100
- * };
101
- *
102
- * auth0.oauth.signIn(data, function (err, userData) {
103
- * if (err) {
104
- * // Handle error.
105
- * }
106
- *
107
- * console.log(userData);
108
- * });
109
- * @param {object} userData User credentials object.
110
- * @param {string} userData.username Username.
111
- * @param {string} userData.password User password.
112
- * @param {string} userData.connection The identity provider in use.
113
- * @param {object} [options] Additional options.
114
- * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
115
- * @param {Function} cb Callback
116
- * @returns {Promise|undefined}
117
- */
118
- signIn(userData, options, cb) {
119
- if (!userData || typeof userData !== 'object') {
120
- throw new ArgumentError('Missing user data object');
121
- }
122
-
123
- const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
124
- const params = {
125
- type: 'ro',
126
- ...getParamsFromOptions(sanitizedOptions),
127
- };
128
-
129
- const data = {
130
- client_id: this.clientId,
131
- grant_type: 'password',
132
- scope: 'openid',
133
- ...userData,
134
- };
135
-
136
- if (
137
- params.type === 'ro' &&
138
- (typeof data.connection !== 'string' || data.connection.split().length === 0)
139
- ) {
140
- throw new ArgumentError('connection field is required');
141
- }
142
-
143
- if (sanitizedCb && sanitizedCb instanceof Function) {
144
- return this.oauthWithIDTokenValidation.create(params, data, sanitizedCb);
145
- }
146
-
147
- return this.oauthWithIDTokenValidation.create(params, data);
148
- }
149
-
150
- /**
151
- * Sign in using a username and password
152
- *
153
- * @example <caption>
154
- * Given the user's credentials perform the OAuth password grant
155
- * or Password Realm grant if a realm is provided,
156
- * it will return a JSON with the access_token and id_token.
157
- *
158
- * More information in the
159
- * <a href="https://auth0.com/docs/api/authentication#resource-owner-password">
160
- * API Docs
161
- * </a>.
162
- * </caption>
163
- *
164
- * var data = {
165
- * client_id: '{CLIENT_ID}', // Optional field.
166
- * username: '{USERNAME}',
167
- * password: '{PASSWORD}',
168
- * realm: '{CONNECTION_NAME}', // Optional field.
169
- * scope: 'openid' // Optional field.
170
- * };
171
- *
172
- * auth0.oauth.passwordGrant(data, function (err, userData) {
173
- * if (err) {
174
- * // Handle error.
175
- * }
176
- *
177
- * console.log(userData);
178
- * });
179
- * @param {object} userData User credentials object.
180
- * @param {string} userData.username Username.
181
- * @param {string} userData.password User password.
182
- * @param {string} [userData.realm] Name of the realm to use to authenticate or the connection name
183
- * @param {object} [options] Additional options.
184
- * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
185
- * @param {Function} cb Callback
186
- * @returns {Promise|undefined}
187
- */
188
- passwordGrant(userData, options, cb) {
189
- const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
190
-
191
- if (!userData || typeof userData !== 'object') {
192
- throw new ArgumentError('Missing user data object');
193
- }
194
-
195
- const data = this._addClientAuthentication({
196
- client_id: this.clientId,
197
- grant_type: 'password',
198
- ...userData,
199
- });
200
-
201
- if (typeof data.username !== 'string' || data.username.split().length === 0) {
202
- throw new ArgumentError('username field is required');
203
- }
204
-
205
- if (typeof data.password !== 'string' || data.password.split().length === 0) {
206
- throw new ArgumentError('password field is required');
207
- }
208
-
209
- if (typeof data.realm === 'string' && data.realm.split().length !== 0) {
210
- data.grant_type = 'http://auth0.com/oauth/grant-type/password-realm';
211
- }
212
-
213
- const params = {
214
- type: 'token',
215
- ...getParamsFromOptions(sanitizedOptions),
216
- };
217
-
218
- if (sanitizedCb && sanitizedCb instanceof Function) {
219
- return this.oauthWithIDTokenValidation.create(params, data, sanitizedCb);
220
- }
221
-
222
- return this.oauthWithIDTokenValidation.create(params, data);
223
- }
224
-
225
- /**
226
- * Exchange a refresh token
227
- *
228
- * @example <caption>
229
- * Given a refresh token from a previous authentication request
230
- * it will return a JSON with the access_token and id_token if
231
- * the openid scope was originally included.
232
- * More information in the
233
- * <a href="https://auth0.com/docs/api/authentication#refresh-token">
234
- * API Docs
235
- * </a>.
236
- * </caption>
237
- *
238
- * var data = {
239
- * refresh_token: '{REFRESH_TOKEN}',
240
- * };
241
- *
242
- * auth0.oauth.refreshToken(data, function (err, data) {
243
- * if (err) {
244
- * // Handle error.
245
- * }
246
- *
247
- * console.log(data);
248
- * });
249
- * @param {object} data Data object.
250
- * @param {string} data.refresh_token Refresh token.
251
- * @param {Function} cb Callback
252
- * @returns {Promise|undefined}
253
- */
254
- refreshToken(data, cb) {
255
- if (!data || typeof data !== 'object') {
256
- throw new ArgumentError('Missing data object');
257
- }
258
-
259
- data = this._addClientAuthentication({
260
- client_id: this.clientId,
261
- grant_type: 'refresh_token',
262
- ...data,
263
- });
264
-
265
- if (typeof data.refresh_token !== 'string' || data.refresh_token.split().length === 0) {
266
- throw new ArgumentError('refresh_token is required');
267
- }
268
-
269
- const params = {
270
- type: 'token',
271
- };
272
-
273
- if (cb && cb instanceof Function) {
274
- return this.oauth.create(params, data, cb);
275
- }
276
- return this.oauth.create(params, data);
277
- }
278
-
279
- /**
280
- * Sign in using a social provider access token.
281
- *
282
- * @param {object} data User credentials object.
283
- * @param {string} data.access_token User access token.
284
- * @param {string} data.connection Identity provider.
285
- * @param {Function} cb Callback
286
- * @returns {Promise|undefined}
287
- */
288
- socialSignIn(data, cb) {
289
- const params = {
290
- type: 'access_token',
291
- };
292
-
293
- if (typeof data !== 'object') {
294
- throw new ArgumentError('Missing user credential objects');
295
- }
296
-
297
- if (typeof data.access_token !== 'string' || data.access_token.trim().length === 0) {
298
- throw new ArgumentError('access_token field is required');
299
- }
300
-
301
- if (typeof data.connection !== 'string' || data.connection.trim().length === 0) {
302
- throw new ArgumentError('connection field is required');
303
- }
304
-
305
- if (cb && cb instanceof Function) {
306
- return this.oauth.create(params, data, cb);
307
- }
308
-
309
- return this.oauth.create(params, data);
310
- }
311
-
312
- clientCredentialsGrant(options, cb) {
313
- if (!options || typeof options !== 'object') {
314
- throw new ArgumentError('Missing options object');
315
- }
316
-
317
- const data = this._addClientAuthentication(
318
- {
319
- grant_type: 'client_credentials',
320
- client_id: this.clientId,
321
- ...options,
322
- },
323
- true
324
- );
325
-
326
- if (!data.client_id || data.client_id.trim().length === 0) {
327
- throw new ArgumentError('client_id field is required');
328
- }
329
-
330
- const params = {
331
- type: 'token',
332
- };
333
-
334
- if (cb && cb instanceof Function) {
335
- return this.oauth.create(params, data, cb);
336
- }
337
-
338
- return this.oauth.create(params, data);
339
- }
340
-
341
- /**
342
- * Sign in using an authorization code
343
- *
344
- * @example <caption>
345
- * Given the code returned in the URL params after the redirect
346
- * from successful authentication, exchange the code for auth0
347
- * credentials. It will return JSON with the access_token and id_token.
348
- * More information in the
349
- * <a href="https://auth0.com/docs/api/authentication#authorization-code-grant">
350
- * API Docs
351
- * </a>.
352
- * </caption>
353
- *
354
- * var options = {
355
- * code: '{CODE}',
356
- * redirect_uri: '{REDIRECT_URI}',
357
- * organization: '{ORGANIZATION_ID}' // Optiional field.
358
- * };
359
- *
360
- * auth0.oauth.authorizationCodeGrant(options, function (err, userData) {
361
- * if (err) {
362
- * // Handle error.
363
- * }
364
- *
365
- * console.log(userData);
366
- * });
367
- * @param {object} options Authorization code payload
368
- * @param {string} options.organization Organization ID
369
- * @param {string} options.code Code in URL returned after authentication
370
- * @param {string} options.redirect_uri The URL to which Auth0 will redirect the browser after authorization has been granted by the user.
371
- * @param {Function} cb Callback
372
- * @returns {Promise|undefined}
373
- */
374
- authorizationCodeGrant(options, cb) {
375
- if (!options || typeof options !== 'object') {
376
- throw new ArgumentError('Missing options object');
377
- }
378
-
379
- const data = this._addClientAuthentication({
380
- grant_type: 'authorization_code',
381
- client_id: this.clientId,
382
- ...options,
383
- });
384
-
385
- if (!data.code || data.code.trim().length === 0) {
386
- throw new ArgumentError('code field is required');
387
- }
388
-
389
- if (!data.redirect_uri || data.redirect_uri.trim().length === 0) {
390
- throw new ArgumentError('redirect_uri field is required');
391
- }
392
-
393
- const params = {
394
- type: 'token',
395
- };
396
-
397
- if (cb && cb instanceof Function) {
398
- return this.oauthWithIDTokenValidation.create(params, data, cb);
399
- }
400
-
401
- return this.oauthWithIDTokenValidation.create(params, data);
402
- }
403
- }
404
-
405
- module.exports = OAuthAuthenticator;
@@ -1,303 +0,0 @@
1
- const { ArgumentError } = require('rest-facade');
2
- const RestClient = require('rest-facade').Client;
3
- const { sanitizeArguments } = require('../utils');
4
- const { addClientAuthentication } = require('./clientAuthentication');
5
-
6
- function getParamsFromOptions(options) {
7
- const params = {};
8
- if (!options || typeof options !== 'object') {
9
- return params;
10
- }
11
- if (options.forwardedFor) {
12
- params._requestCustomizer = function (req) {
13
- req.set('auth0-forwarded-for', options.forwardedFor);
14
- };
15
- }
16
- return params;
17
- }
18
-
19
- /**
20
- * Handles authenticator with passwordless flows, e.g. SMS, Touch ID, etc.
21
- */
22
-
23
- class PasswordlessAuthenticator {
24
- /**
25
- * @param {object} options Authenticator options.
26
- * @param {string} options.baseUrl The auth0 account URL.
27
- * @param {string} [options.domain] Required if using clientAssertionSigningKey.
28
- * @param {string} [options.clientId] Default client ID.
29
- * @param {string} [options.clientSecret] Default client secret.
30
- * @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT.
31
- * @param {string} [options.clientAssertionSigningAlg] Default 'RS256'.
32
- * @param {string} [options.proxy] Add the `superagent-proxy` dependency and specify a proxy url eg 'https://myproxy.com:1234'
33
- * @param {OAuthAuthenticator} oauth OAuthAuthenticator instance.
34
- */
35
- constructor(options, oauth) {
36
- if (!options) {
37
- throw new ArgumentError('Missing authenticator options');
38
- }
39
-
40
- if (typeof options !== 'object') {
41
- throw new ArgumentError('The authenticator options must be an object');
42
- }
43
-
44
- /**
45
- * Options object for the Rest Client instance.
46
- *
47
- * @type {object}
48
- */
49
- const clientOptions = {
50
- errorFormatter: { message: 'message', name: 'error' },
51
- headers: options.headers,
52
- proxy: options.proxy,
53
- };
54
-
55
- this.oauth = oauth;
56
- this.passwordless = new RestClient(`${options.baseUrl}/passwordless/start`, clientOptions);
57
- this.domain = options.domain;
58
- this.clientId = options.clientId;
59
- this.clientSecret = options.clientSecret;
60
- this.clientAssertionSigningKey = options.clientAssertionSigningKey;
61
- this.clientAssertionSigningAlg = options.clientAssertionSigningAlg;
62
- }
63
-
64
- _addClientAuthentication(payload, required) {
65
- return addClientAuthentication({
66
- payload,
67
- required,
68
- domain: this.domain,
69
- clientSecret: this.clientSecret,
70
- clientAssertionSigningKey: this.clientAssertionSigningKey,
71
- clientAssertionSigningAlg: this.clientAssertionSigningAlg,
72
- });
73
- }
74
-
75
- /**
76
- * Sign in with the given user credentials.
77
- *
78
- * @example <caption>
79
- * Once you have a verification code, use this endpoint to login
80
- * the user with their phone number/email and verification code.
81
- *
82
- * https://auth0.com/docs/api/authentication#authenticate-user
83
- * </caption>
84
- *
85
- * var data = {
86
- * username: '{PHONE_NUMBER OR EMAIL}',
87
- * otp: '{VERIFICATION_CODE}',
88
- * realm: '{sms or email}' // OPTIONAL DEFAULTS TO SMS
89
- * };
90
- *
91
- * auth0.passwordless.signIn(data, function (err) {
92
- * if (err) {
93
- * // Handle error.
94
- * }
95
- * });
96
- * @example <caption>
97
- * The user data object has the following structure.
98
- * </caption>
99
- *
100
- * {
101
- * id_token: String,
102
- * access_token: String,
103
- * token_type: String
104
- * }
105
- * @example <caption>
106
- * LEGACY signIn using the `/oauth/ro` endpoint. When otp is not specified
107
- * password is required. Given the user credentials (`phone_number` and `code`),
108
- * it will do the authentication on the provider and return a JSON with
109
- * the `access_token` and `id_token`.
110
- *
111
- * https://auth0.com/docs/api/authentication#resource-owner
112
- * </caption>
113
- *
114
- * var data = {
115
- * username: '{PHONE_NUMBER}',
116
- * password: '{VERIFICATION_CODE}'
117
- * };
118
- *
119
- * auth0.passwordless.signIn(data, function (err) {
120
- * if (err) {
121
- * // Handle error.
122
- * }
123
- * });
124
- * @param {object} userData User credentials object.
125
- * @param {string} userData.username The user's phone number if realm=sms, or the user's email if realm=email
126
- * @param {string} userData.otp The user's verification code. Required
127
- * @param {string} [userData.audience] API Identifier of the API for which you want to get an Access Token.
128
- * @param {string} [userData.realm=sms] Realm string: "sms" or "email".
129
- * @param {string} [userData.password] [DEPRECATED] Password required if using legacy /oauth/ro endpoint
130
- * @param {string} [userData.connection=sms] [DEPRECATED] Connection string: "sms" or "email".
131
- * @param {object} [options] Additional options.
132
- * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
133
- * @param {Function} [cb] Method callback.
134
- * @returns {Promise|undefined}
135
- */
136
- signIn(userData, options, cb) {
137
- const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
138
-
139
- if (!userData || typeof userData !== 'object') {
140
- throw new ArgumentError('Missing user data object');
141
- }
142
-
143
- const data = this._addClientAuthentication({
144
- client_id: this.clientId,
145
- ...userData,
146
- });
147
-
148
- if (typeof data.username !== 'string' || data.username.trim().length === 0) {
149
- throw new ArgumentError('username field (phone number) is required');
150
- }
151
-
152
- // If otp is provided, attempt to sign in using otp grant
153
- if (typeof data.otp === 'string' && data.otp.trim().length > 0) {
154
- if (!data.realm || (data.realm !== 'email' && data.realm !== 'sms')) {
155
- data.realm = 'sms';
156
- }
157
- data.grant_type = 'http://auth0.com/oauth/grant-type/passwordless/otp';
158
- return this.oauth.signIn(data, { type: 'token', ...sanitizedOptions }, sanitizedCb);
159
- }
160
-
161
- // Don't let the user override the connection nor the grant type.
162
- if (!data.connection || (data.connection !== 'email' && data.connection !== 'sms')) {
163
- data.connection = 'sms';
164
- }
165
- data.grant_type = 'password';
166
-
167
- if (typeof data.password !== 'string' || data.password.trim().length === 0) {
168
- throw new ArgumentError('password field (verification code) is required');
169
- }
170
-
171
- console.warn(
172
- 'The oauth/ro endpoint has been deprecated. Please use the realm and otp parameters in this function.'
173
- );
174
- return this.oauth.signIn(data, sanitizedOptions, sanitizedCb);
175
- }
176
-
177
- /**
178
- * Start passwordless flow sending an email.
179
- *
180
- * @example <caption>
181
- * Given the user `email` address, it will send an email with:
182
- *
183
- * <ul>
184
- * <li>A link (default, `send:"link"`). You can then authenticate with this
185
- * user opening the link and he will be automatically logged in to the
186
- * application. Optionally, you can append/override parameters to the link
187
- * (like `scope`, `redirect_uri`, `protocol`, `response_type`, etc.) using
188
- * `authParams` object.
189
- * </li>
190
- * <li>
191
- * A verification code (`send:"code"`). You can then authenticate with
192
- * this user using the `/oauth/ro` endpoint specifying `email` as
193
- * `username` and `code` as `password`.
194
- * </li>
195
- * </ul>
196
- *
197
- * Find more information in the
198
- * <a href="https://auth0.com/docs/auth-api#!#post--with_email">API Docs</a>
199
- * </caption>
200
- *
201
- * var data = {
202
- * email: '{EMAIL}',
203
- * send: 'link',
204
- * authParams: {} // Optional auth params.
205
- * };
206
- *
207
- * auth0.passwordless.sendEmail(data, function (err) {
208
- * if (err) {
209
- * // Handle error.
210
- * }
211
- * });
212
- * @param {object} userData User account data.
213
- * @param {string} userData.email User email address.
214
- * @param {string} userData.send The type of email to be sent.
215
- * @param {object} [options] Additional options.
216
- * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
217
- * @param {Function} [cb] Method callback.
218
- * @returns {Promise|undefined}
219
- */
220
- sendEmail(userData, options, cb) {
221
- const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
222
- const data = this._addClientAuthentication({
223
- client_id: this.clientId,
224
- ...userData,
225
- });
226
- const params = getParamsFromOptions(sanitizedOptions);
227
-
228
- // Don't let the user override the connection nor the grant type.
229
- data.connection = 'email';
230
-
231
- if (!userData || typeof userData !== 'object') {
232
- throw new ArgumentError('Missing user data object');
233
- }
234
-
235
- if (typeof data.email !== 'string' || data.email.trim().length === 0) {
236
- throw new ArgumentError('email field is required');
237
- }
238
-
239
- if (typeof data.send !== 'string' || data.send.trim().length === 0) {
240
- throw new ArgumentError('send field is required');
241
- }
242
-
243
- if (sanitizedCb && sanitizedCb instanceof Function) {
244
- return this.passwordless.create(params, data, sanitizedCb);
245
- }
246
-
247
- return this.passwordless.create(params, data);
248
- }
249
-
250
- /**
251
- * Start passwordless flow sending an SMS.
252
- *
253
- * @example <caption>
254
- * Given the user `phone_number`, it will send a SMS message with a
255
- * verification code. You can then authenticate with this user using the
256
- * `/oauth/ro` endpoint specifying `phone_number` as `username` and `code` as
257
- * `password`:
258
- * </caption>
259
- *
260
- * var data = {
261
- * phone_number: '{PHONE}'
262
- * };
263
- *
264
- * auth0.passwordless.sendSMS(data, function (err) {
265
- * if (err) {
266
- * // Handle error.
267
- * }
268
- * });
269
- * @param {object} userData User account data.
270
- * @param {string} userData.phone_number User phone number.
271
- * @param {object} [options] Additional options.
272
- * @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
273
- * @param {Function} [cb] Method callback.
274
- * @returns {Promise|undefined}
275
- */
276
- sendSMS(userData, options, cb) {
277
- const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
278
- const data = this._addClientAuthentication({
279
- client_id: this.clientId,
280
- ...userData,
281
- });
282
- const params = getParamsFromOptions(sanitizedOptions);
283
-
284
- // Don't let the user override the connection nor the grant type.
285
- data.connection = 'sms';
286
-
287
- if (!userData || typeof userData !== 'object') {
288
- throw new ArgumentError('Missing user data object');
289
- }
290
-
291
- if (typeof data.phone_number !== 'string' || data.phone_number.trim().length === 0) {
292
- throw new ArgumentError('phone_number field is required');
293
- }
294
-
295
- if (sanitizedCb && sanitizedCb instanceof Function) {
296
- return this.passwordless.create(params, data, sanitizedCb);
297
- }
298
-
299
- return this.passwordless.create(params, data);
300
- }
301
- }
302
-
303
- module.exports = PasswordlessAuthenticator;