This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
383 lines (344 loc) · 12.1 KB
/
index.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
var assert = require('assert')
var TownshipAuth = require('township-auth')
var basic = require('township-auth/basic')
var createAccess = require('township-access')
var createToken = require('township-token')
var xtend = require('xtend')
var locks = require('semlocks')
/**
* Create an accounts object
* @name townshipAccounts
* @param {Object} db – instance of a level db. See https://npmjs.com/level
* @param {Object} config – configuration object
* @example
*
* var townshipAccounts = require('township-accounts')
* var level = require('level')
* var db = level('db')
*
* var accounts = townshipAccounts(db, {
* secret: 'not a secret'
* })
**/
module.exports = function townshipAccounts (db, config) {
assert.strictEqual(typeof db, 'object', 'leveldb instance required as first argument')
assert.strictEqual(typeof config, 'object', 'config object required')
var accounts = {}
var auth = new TownshipAuth(db, { providers: { basic: basic } })
var access = createAccess(db, config)
var jwt = createToken(db, config)
/*
* Hooks
*/
var hooks = config.hooks || {}
hooks.beforeRegister = hooks.beforeRegister || noop
hooks.afterRegister = hooks.afterRegister || noop
hooks.beforeDestroy = hooks.beforeDestroy || noop
function lock (key, callback) {
locks.acquire(key, callback)
}
/**
* Create an account
* @name register
* @param {Object} options – email address that corresponds to an account
* @param {String} options.email – email address that corresponds to an account
* @param {String} options.password – password that corresponds to an account
* @param {Array} options.scopes – access scopes for an account
* @param {Function} callback – callback function called with `error` and `accountData` arguments
* @example
*
* var creds = {
* email: '[email protected]',
* password: 'notverysecret',
* scopes: ['example:read']
* }
*
* accounts.register(creds, function (err, account) {
* if (err) return console.log(err)
* console.log(account.key, account.token)
* })
**/
accounts.register = function register (options, callback) {
console.log('register', options)
if (!options.email) return callback(new Error('options.email required'))
if (!options.password) return callback(new Error('options.password required'))
options.scopes = options.scopes || []
lock(options.email, (err, release) => {
if (err) return callback(err)
hooks.beforeRegister(options, function (err, hookOptions) {
if (err) {
release()
return callback(err)
}
options = xtend(options, hookOptions)
accounts.findByEmail(options.email, function (err, account) {
if (!err && account) {
release()
return callback(new Error('Cannot create account with that email address'))
}
auth.create({ basic: options }, function (err, authData) {
if (err) {
release()
return callback(err)
}
access.create(authData.key, options.scopes, function (err, accessData) {
if (err) {
release()
return callback(err)
}
var token = jwt.sign({
auth: authData,
access: accessData
})
var account = { key: authData.key, token: token }
hooks.afterRegister(account, function (err, data) {
release()
if (err) return callback(err)
return callback(null, data)
})
})
})
})
})
})
}
/**
* Log in to an account
* @name login
* @param {Object} options – email address that corresponds to an account
* @param {String} options.email – email address that corresponds to an account
* @param {String} options.password – password that corresponds to an account
* @param {Function} callback – callback function called with `error` and `account` arguments
* @example
*
* var creds = {
* email: '[email protected]',
* password: 'notverysecret'
* }
*
* accounts.login(creds, function (err, account) {
* if (err) return console.log(err)
* console.log(account.key, account.token)
* })
**/
accounts.login = function login (options, callback) {
if (!options || typeof options !== 'object') {
return callback(new Error('email and password properties required'), 400)
}
var email = options.email
var password = options.password
if (!email) {
return callback(new Error('email property required'), 400)
} else if (!password) {
return callback(new Error('password property required'), 400)
}
auth.verify('basic', options, function (err, authData) {
if (err) return callback(err)
access.get(authData.key, function (err, accessData) {
if (err) return callback(err)
var token = jwt.sign({ auth: authData, access: accessData })
callback(null, { key: authData.key, token: token })
})
})
}
/**
* Log out of an account
* @name logout
* @param {String} token – the active token for an account
* @param {Function} callback – callback function called with an `error` argument
* @example
*
* accounts.logout(token, function (err) {
* if (err) return console.log(err)
* })
**/
accounts.logout = function logout (token, callback) {
jwt.verify(token, function (err, account) {
if (err && err.message === 'jwt expired') return callback()
else if (err) return callback(err)
jwt.invalidate(token, callback)
})
}
/**
* Destroy an account
* @name logout
* @param {String} key – the key for an account
* @param {Function} callback – callback function called with an `error` argument
* @example
*
* accounts.destroy(token, function (err) {
* if (err) return console.log(err)
* })
**/
accounts.destroy = function destroy (key, callback) {
if (!key || typeof key !== 'string') return callback(new Error('account key is required'))
hooks.beforeDestroy(key, function (err) {
if (err) return callback(err)
auth.destroy(key, function (err) {
if (err) return callback(err)
access.destroy(key, function (err) {
if (err) return callback(err)
callback(null, key)
})
})
})
}
/**
* Find an account by email
* @name findbyEmail
* @param {String} email – email address that corresponds to an account
* @param {Function} callback – callback function called with `error` and `accountData` arguments
* @example
*
* accounts.findByEmail('[email protected]', function (err, accountData) {
* if (err) return console.log(err)
* console.log(accountData)
* })
**/
accounts.findByEmail = function findByEmail (email, callback) {
auth.findOne('basic', email, function (err, authData) {
if (err || !authData) return callback(new Error('Account not found'))
access.get(authData.key, function (err, accessData) {
if (err) return callback(err)
callback(null, { key: authData.key, access: accessData, auth: authData })
})
})
}
/**
* Update password of an account
* @name updatePassword
* @param {Object} options – email address that corresponds to an account
* @param {String} options.email – email address that corresponds to an account
* @param {String} options.password – password that corresponds to an account
* @param {String} options.newPassword – new password for an account
* @param {Function} callback – callback function called with `error` and `account` arguments
* @example
*
* var creds = {
* email: '[email protected]',
* password: 'notverysecret',
* newPassword: 'still not very secret',
* token: token
* }
*
* accounts.updatePassword(creds, function (err, account) {
* if (err) return console.log(err)
* console.log(account.key, account.token)
* })
**/
accounts.updatePassword = function updatePassword (options, callback) {
if (typeof options !== 'object') return callback(new Error('options object is required'))
if (typeof options.email !== 'string') return callback(new Error('options.email string is required'))
if (typeof options.password !== 'string') return callback(new Error('options.password string is required'))
if (typeof options.newPassword !== 'string') return callback(new Error('options.newPassword string is required'))
var creds = {
email: options.email,
password: options.password
}
lock(options.email, (err, release) => {
if (err) return callback(err)
accounts.findByEmail(options.email, function (err, account) {
if (err) {
release()
return callback(err)
}
auth.verify('basic', creds, function (err, authData) {
if (err) {
release()
return callback(err)
}
var updatedCreds = {
key: authData.key,
basic: {
email: options.email,
password: options.newPassword
}
}
auth.update(updatedCreds, function (err, authData) {
if (err) {
release()
return callback(err)
}
var newToken = jwt.sign({
auth: authData,
access: account.access
})
release()
callback(null, { key: authData.key, token: newToken })
})
})
})
})
}
/**
* Authorize account using access scopes
* @name authorize
* @param {String} accountKey – the key for an account
* @param {Array} scopes – the scopes your are checking for in the account's access permissions
* @param {Function} callback – callback function called with an `error` argument
* @example
*
* accounts.authorize(key, ['example:read'], function (err) {
* if (err) return console.log(err)
* })
**/
accounts.authorize = function authorize (accountKey, scopes, callback) {
access.verify(accountKey, scopes, callback)
}
/**
* Authorize account using access scopes via their email
* @name authorizeByEmail
* @param {String} email – the email address associated with an account
* @param {Array} scopes – the scopes your are checking for in the account's access permissions
* @param {Function} callback – callback function called with an `error` argument
* @example
*
* accounts.authorizeByEmail('[email protected]', ['example:read'], function (err) {
* if (err) return console.log(err)
* })
**/
accounts.authorizeByEmail = function authorizeByEmail (email, scopes, callback) {
accounts.findByEmail(email, function (err, accountData) {
if (err) return callback(err)
access.verify(accountData.key, scopes, callback)
})
}
/**
* Update the access scopes of an account
* @name updateScopes
* @param {String} key – the key associated with an account
* @param {Array} scopes – the new scopes of the account. note that this completely replaces the old scopes array
* @param {Function} callback – callback function called with an `error` argument
* @example
*
* accounts.updateScopes(key, ['example:read'], function (err) {
* if (err) return console.log(err)
* })
**/
accounts.updateScopes = function updateScopes (key, scopes, callback) {
access.update(key, scopes, callback)
}
/**
* verify that a token is valid
* @name verifyToken
* @param {String} token – the JWT created when registering or logging in
* @param {Function} callback – callback function called with `error` and `accountData` arguments
* @example
*
* accounts.verifyToken(token, function (err, account) {
* if (err) return console.log(err)
* })
**/
accounts.verifyToken = function verifyToken (token, callback) {
if (typeof token !== 'string') return callback(new Error('encrypted token is required'))
return jwt.verify(token, function (err, accountData) {
if (err) return callback(err)
callback(null, accountData, token)
})
}
accounts.auth = auth
accounts.access = access
accounts.token = jwt
return accounts
}
function noop (data, callback) { return callback(null, data) }