-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate-token.js
38 lines (32 loc) · 1.35 KB
/
generate-token.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
const crypto = require('crypto');
const moment = require('moment-timezone');
/**
* Generate Token
*
* @function sails.helpers.generateToken
* @param {String} [extra='Evil will always triumph, because good is dumb. -Lord Helmet'] - A bit of random, extra bits to change up the hash.
*
* @returns {String} A SHA512 hash of a cryptographically-secure, randomly generated string of characters.
*/
module.exports = {
friendlyName: 'Generate Token',
description: 'Generate generic token for generic use, generically. (64 characters)',
sync: true, // not async
inputs: {
extra: {
type: 'string',
description: 'A bit of random, extra bits to change up the hash.',
defaultsTo: 'Evil will always triumph, because good is dumb. -Lord Helmet'
}
},
fn: (inputs, exits) => {
return exits.success(
crypto.createHmac('sha512', sails.config.session.secret).update(
crypto.randomBytes(21) // cryptographically-secure random characters
+ moment(new Date()).format() // throw in the current time stamp
+ inputs.extra // an optional way to add a bit more randomness to the mix
+ crypto.randomBytes(21) // cryptographically-secure random characters
).digest('hex')
);
}
};