forked from auth0/node-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordlessAuthenticator.js
286 lines (258 loc) · 9.86 KB
/
PasswordlessAuthenticator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const { ArgumentError } = require('rest-facade');
const RestClient = require('rest-facade').Client;
const { sanitizeArguments } = require('../utils');
function getParamsFromOptions(options) {
const params = {};
if (!options || typeof options !== 'object') {
return params;
}
if (options.forwardedFor) {
params._requestCustomizer = function (req) {
req.set('auth0-forwarded-for', options.forwardedFor);
};
}
return params;
}
/**
* Handles authenticator with passwordless flows, e.g. SMS, Touch ID, etc.
*/
class PasswordlessAuthenticator {
/**
* @param {object} options Authenticator options.
* @param {string} options.baseUrl The auth0 account URL.
* @param {string} [options.clientId] Default client ID.
* @param {string} [options.clientSecret] Default client secret.
* @param {OAuthAuthenticator} oauth OAuthAuthenticator instance.
*/
constructor(options, oauth) {
if (!options) {
throw new ArgumentError('Missing authenticator options');
}
if (typeof options !== 'object') {
throw new ArgumentError('The authenticator options must be an object');
}
/**
* Options object for the Rest Client instance.
*
* @type {object}
*/
const clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
};
this.oauth = oauth;
this.passwordless = new RestClient(`${options.baseUrl}/passwordless/start`, clientOptions);
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
}
/**
* Sign in with the given user credentials.
*
* @example <caption>
* Once you have a verification code, use this endpoint to login
* the user with their phone number/email and verification code.
*
* https://auth0.com/docs/api/authentication#authenticate-user
* </caption>
*
* var data = {
* username: '{PHONE_NUMBER OR EMAIL}',
* otp: '{VERIFICATION_CODE}',
* realm: '{sms or email}' // OPTIONAL DEFAULTS TO SMS
* };
*
* auth0.passwordless.signIn(data, function (err) {
* if (err) {
* // Handle error.
* }
* });
* @example <caption>
* The user data object has the following structure.
* </caption>
*
* {
* id_token: String,
* access_token: String,
* token_type: String
* }
* @example <caption>
* LEGACY signIn using the `/oauth/ro` endpoint. When otp is not specified
* password is required. Given the user credentials (`phone_number` and `code`),
* it will do the authentication on the provider and return a JSON with
* the `access_token` and `id_token`.
*
* https://auth0.com/docs/api/authentication#resource-owner
* </caption>
*
* var data = {
* username: '{PHONE_NUMBER}',
* password: '{VERIFICATION_CODE}'
* };
*
* auth0.passwordless.signIn(data, function (err) {
* if (err) {
* // Handle error.
* }
* });
* @param {object} userData User credentials object.
* @param {string} userData.username The user's phone number if realm=sms, or the user's email if realm=email
* @param {string} userData.otp The user's verification code. Required
* @param {string} [userData.audience] API Identifier of the API for which you want to get an Access Token.
* @param {string} [userData.realm=sms] Realm string: "sms" or "email".
* @param {string} [userData.password] [DEPRECATED] Password required if using legacy /oauth/ro endpoint
* @param {string} [userData.connection=sms] [DEPRECATED] Connection string: "sms" or "email".
* @param {object} [options] Additional options.
* @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
* @param {Function} [cb] Method callback.
* @returns {Promise|undefined}
*/
signIn(userData, options, cb) {
const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
if (!userData || typeof userData !== 'object') {
throw new ArgumentError('Missing user data object');
}
const data = {
client_id: this.clientId,
client_secret: this.clientSecret,
...userData,
};
if (typeof data.username !== 'string' || data.username.trim().length === 0) {
throw new ArgumentError('username field (phone number) is required');
}
// If otp is provided, attempt to sign in using otp grant
if (typeof data.otp === 'string' && data.otp.trim().length > 0) {
if (!data.realm || (data.realm !== 'email' && data.realm !== 'sms')) {
data.realm = 'sms';
}
data.grant_type = 'http://auth0.com/oauth/grant-type/passwordless/otp';
return this.oauth.signIn(data, { type: 'token', ...sanitizedOptions }, sanitizedCb);
}
// Don't let the user override the connection nor the grant type.
if (!data.connection || (data.connection !== 'email' && data.connection !== 'sms')) {
data.connection = 'sms';
}
data.grant_type = 'password';
if (typeof data.password !== 'string' || data.password.trim().length === 0) {
throw new ArgumentError('password field (verification code) is required');
}
console.warn(
'The oauth/ro endpoint has been deprecated. Please use the realm and otp parameters in this function.'
);
return this.oauth.signIn(data, sanitizedOptions, sanitizedCb);
}
/**
* Start passwordless flow sending an email.
*
* @example <caption>
* Given the user `email` address, it will send an email with:
*
* <ul>
* <li>A link (default, `send:"link"`). You can then authenticate with this
* user opening the link and he will be automatically logged in to the
* application. Optionally, you can append/override parameters to the link
* (like `scope`, `redirect_uri`, `protocol`, `response_type`, etc.) using
* `authParams` object.
* </li>
* <li>
* A verification code (`send:"code"`). You can then authenticate with
* this user using the `/oauth/ro` endpoint specifying `email` as
* `username` and `code` as `password`.
* </li>
* </ul>
*
* Find more information in the
* <a href="https://auth0.com/docs/auth-api#!#post--with_email">API Docs</a>
* </caption>
*
* var data = {
* email: '{EMAIL}',
* send: 'link',
* authParams: {} // Optional auth params.
* };
*
* auth0.passwordless.sendEmail(data, function (err) {
* if (err) {
* // Handle error.
* }
* });
* @param {object} userData User account data.
* @param {string} userData.email User email address.
* @param {string} userData.send The type of email to be sent.
* @param {object} [options] Additional options.
* @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
* @param {Function} [cb] Method callback.
* @returns {Promise|undefined}
*/
sendEmail(userData, options, cb) {
const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
const data = {
client_id: this.clientId,
client_secret: this.clientSecret,
...userData,
};
const params = getParamsFromOptions(sanitizedOptions);
// Don't let the user override the connection nor the grant type.
data.connection = 'email';
if (!userData || typeof userData !== 'object') {
throw new ArgumentError('Missing user data object');
}
if (typeof data.email !== 'string' || data.email.trim().length === 0) {
throw new ArgumentError('email field is required');
}
if (typeof data.send !== 'string' || data.send.trim().length === 0) {
throw new ArgumentError('send field is required');
}
if (sanitizedCb && sanitizedCb instanceof Function) {
return this.passwordless.create(params, data, sanitizedCb);
}
return this.passwordless.create(params, data);
}
/**
* Start passwordless flow sending an SMS.
*
* @example <caption>
* Given the user `phone_number`, it will send a SMS message with a
* verification code. You can then authenticate with this user using the
* `/oauth/ro` endpoint specifying `phone_number` as `username` and `code` as
* `password`:
* </caption>
*
* var data = {
* phone_number: '{PHONE}'
* };
*
* auth0.passwordless.sendSMS(data, function (err) {
* if (err) {
* // Handle error.
* }
* });
* @param {object} userData User account data.
* @param {string} userData.phone_number User phone number.
* @param {object} [options] Additional options.
* @param {string} [options.forwardedFor] Value to be used for auth0-forwarded-for header
* @param {Function} [cb] Method callback.
* @returns {Promise|undefined}
*/
sendSMS(userData, options, cb) {
const { options: sanitizedOptions, cb: sanitizedCb } = sanitizeArguments(options, cb);
const data = {
client_id: this.clientId,
client_secret: this.clientSecret,
...userData,
};
const params = getParamsFromOptions(sanitizedOptions);
// Don't let the user override the connection nor the grant type.
data.connection = 'sms';
if (!userData || typeof userData !== 'object') {
throw new ArgumentError('Missing user data object');
}
if (typeof data.phone_number !== 'string' || data.phone_number.trim().length === 0) {
throw new ArgumentError('phone_number field is required');
}
if (sanitizedCb && sanitizedCb instanceof Function) {
return this.passwordless.create(params, data, sanitizedCb);
}
return this.passwordless.create(params, data);
}
}
module.exports = PasswordlessAuthenticator;