/
GoogleAuthenticator.js
301 lines (264 loc) · 9.21 KB
/
GoogleAuthenticator.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* See LICENSE file for terms of use */
'use strict';
// import libraries
var _ = require('underscore'),
base32 = require('thirty-two'),
joi = require('joi'),
schema = require('./schemas/googleAuthenticator'),
qr = require('qr-image'),
uid = require('uid-safe'),
TOTP = require('./TOTP');
/**
* {GoogleAuthenticator} constructor
*
* @constructor
*/
var GoogleAuthenticator = function GoogleAuthenticator()
{
// save instance
var self = this;
Object.defineProperties(self, {
/**
* Function to encode in base 32
*
* @param {@code secret} secret will be encoded
*
* @return base 32 string
*/
encode:
{
value: function (secret)
{
return base32.encode(secret).toString().replace(/=/g,'');
},
enumerable: true
},
/**
* Function to decode a base 32 secret
*
* @param {@code secret} base 32 secret will be decoded
*
* @return secret string
*/
decode:
{
value: function (base32Secret)
{
return base32.decode(base32Secret).toString();
},
enumerable: true
},
/**
* Function to generate base32 secret
*
* @return random 16-digit base32 encoded secret
*/
secret:
{
value: function()
{
return self.encode(uid.sync(7).toUpperCase());
},
enumerable: true
},
/**
* Function to generate the key uri for authenticator
* otpauth://totp/issuer:user@host?secret=xxx&issuer=yyy
*
* @param {@code u} the user for this account
*
* @param {@code i} the provider or service managing that account
*
* @param {@code s} the secret in base32. This should be unique and secret for
* every user as this is the seed that is used to calculate the HMAC.
*
* @return string representation of key uri
*/
keyUri:
{
value: function(u, i, s)
{
// set defaults values
var _user = u || '',
_issuer = i || '',
_secret = s || '',
_data = '%issuer:%user?secret=%secret&issuer=%issuer',
_protocol = 'otpauth://totp/';
// validate inputs
var userValidate = joi.validate(_user, schema.user, {language: {label: 'user'}});
if (userValidate.error)
{
throw userValidate.error;
}
_user = userValidate.value;
var issuerValidate = joi.validate(_issuer, schema.issuer, {language: {label: 'issuer'}});
if (issuerValidate.error)
{
throw issuerValidate.error;
}
_issuer = issuerValidate.value;
// ensure uniformity
_issuer = _issuer.charAt(0).toUpperCase() + _issuer.substr(1);
var secretValidate = joi.validate(_secret, schema.secret, {language: {label: 'secret'}});
if (secretValidate.error)
{
throw secretValidate.error;
}
_secret = secretValidate.value;
// ensure uniformity
_secret = _secret.toUpperCase();
// Repalce string
_data = _data.replace('%user', _user);
_data = _data.replace('%secret', _secret);
_data = _data.replace(/%issuer/g, _issuer);
return encodeURIComponent(_protocol + _data);
},
enumerable: true
},
/**
* Function to generate QR code
*
* @param {@code u} the user for this account
*
* @param {@code i} the provider or service managing that account
*
* @param {@code s} the secret in base32. This should be unique and secret for
* every user as this is the seed that is used to calculate the HMAC.
*
* @return string with image data - SVG format
*/
qrCode:
{
value: function(u, i, s)
{
return qr.imageSync(decodeURIComponent(self.keyUri(u, i, s)), {type: 'svg', size: 5});
},
enumerable: true
},
/**
* Function to generate Google Authenticator TOTP
*
* @param {@code s} the secret in `base32` to generate OTP. This should be unique and secret for
* every user as this is the seed that is used to calculate the HMAC.
*
* @return time based one time password.
*/
gen:
{
value: function(s)
{
// set defaults values
var _secret = s || '';
// validate inputs
var secretValidate = joi.validate(_secret, schema.secret, {language: {label: 'secret'}});
if (secretValidate.error)
{
throw secretValidate.error;
}
_secret = secretValidate.value;
// ensure uniformity
_secret = _secret.toUpperCase();
return TOTP.gen({string:self.decode(_secret)});
},
enumerable: true
},
/**
* Function to verify Google Authenticator TOTP
*
* @param {@code t} Passcode to validate
*
* @param {@code s} the secret in `base32` to verify OTP. This should be unique and secret for
* every user as this is the seed that is used to calculate the HMAC.
*
* @param {@code w} - The allowable margin, time steps in seconds since T0, for the counter. The function will check
* 'W' codes in the past and the future against the provided passcode. Note,
* it is the calling applications responsibility to keep track of
* 'C' and increment it for each password check, and also to adjust
* it accordingly in the case where the client and server become
* out of sync (second argument returns non zero).
* E.g. if W = 5, and C = 100, this function will check the passcode
* against all One Time Passcodes between 95 and 105. - default 6.
*
* @return an object {@code {delta: #}} if the token is valid else {@code null}
*/
verify:
{
value: function(t, s, w)
{
// set defaults values
var _token = t || '',
_secret = s || '',
_window = w;
// validate inputs
var tokenValidate = joi.validate(_token, schema.token, {language: {label: 'token'}});
if (tokenValidate.error)
{
throw tokenValidate.error;
}
_token = tokenValidate.value;
var secretValidate = joi.validate(_secret, schema.secret, {language: {label: 'secret'}});
if (secretValidate.error)
{
throw secretValidate.error;
}
_secret = secretValidate.value;
// ensure uniformity
_secret = _secret.toUpperCase();
if (!_.isUndefined(_window))
{
var windowValidate = joi.validate(_window, schema.window, {language: {label: 'window'}});
if (windowValidate.error)
{
throw windowValidate.error;
}
_window = windowValidate.value;
return TOTP.verify(_token, {string:self.decode(_secret)}, {window:_window});
}
else
{
return TOTP.verify(_token, {string:self.decode(_secret)});
}
},
enumerable: true
},
/**
* Function to return and test singleton instance
*/
singleton:
{
value: function()
{
return GoogleAuthenticator.getInstance();
},
enumerable: false
}
});
};
/* ******************************
SINGLETON CLASS DEFINITION
***************************** */
var instance = null;
// define object static private properties
Object.defineProperties(GoogleAuthenticator, {
/**
* Method to return singleton instance of {GoogleAuthenticator}
*/
getInstance:
{
value: function ()
{
if (_.isNull(instance))
{
instance = new GoogleAuthenticator();
}
return instance;
},
enumerable: false
}
});
/**
* Expose {GoogleAuthenticator} singleton
*
* @type {GoogleAuthenticator}
*/
module.exports = GoogleAuthenticator.getInstance();