-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.js
145 lines (115 loc) · 2.94 KB
/
tokens.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
const rndm = require('rndm');
const uid = require('uid-safe');
const compare = require('tsscmp');
const crypto = require('crypto');
/**
* Module variables.
* @private
*/
const EQUAL_GLOBAL_REGEXP = /=/g;
const PLUS_GLOBAL_REGEXP = /\+/g;
const SLASH_GLOBAL_REGEXP = /\//g;
/**
* Hash a string with SHA1, returning url-safe base64
* @param {string} str
* @private
*/
function hash(str) {
return crypto
.createHash('sha1')
.update(str, 'ascii')
.digest('base64')
.replace(PLUS_GLOBAL_REGEXP, '-')
.replace(SLASH_GLOBAL_REGEXP, '_')
.replace(EQUAL_GLOBAL_REGEXP, '');
}
/**
* Token generation/verification class.
*
* @param {object} [options]
* @param {number} [options.saltLength=8] The string length of the salt
* @param {number} [options.secretLength=18] The byte length of the secret key
* @public
*/
function Tokens(options) {
if (!(this instanceof Tokens)) {
return new Tokens(options);
}
const opts = options || {};
const saltLength = opts.saltLength !== undefined
? opts.saltLength
: 8;
if (typeof saltLength !== 'number' || !Number.isFinite(saltLength) || saltLength < 1) {
throw new TypeError('option saltLength must be finite number > 1');
}
const secretLength = opts.secretLength !== undefined
? opts.secretLength
: 18;
if (typeof secretLength !== 'number' || !Number.isFinite(secretLength) || secretLength < 1) {
throw new TypeError('option secretLength must be finite number > 1');
}
this.saltLength = saltLength;
this.secretLength = secretLength;
}
/**
* Create a new CSRF token.
*
* @param {string} secret The secret for the token.
* @public
*/
Tokens.prototype.create = function create(secret) {
if (!secret || typeof secret !== 'string') {
throw new TypeError('argument secret is required');
}
return this.privateTokenize(secret, rndm(this.saltLength));
};
/**
* Create a new secret key.
*
* @param {function} [callback]
* @public
*/
Tokens.prototype.secret = function secret(callback) {
return uid(this.secretLength, callback);
};
/**
* Create a new secret key synchronously.
* @public
*/
Tokens.prototype.secretSync = function secretSync() {
return uid.sync(this.secretLength);
};
/**
* Tokenize a secret and salt.
* @private
*/
Tokens.prototype.privateTokenize = function tokenize(secret, salt) {
return `${salt}-${hash(`${salt}-${secret}`)}`;
};
/**
* Verify if a given token is valid for a given secret.
*
* @param {string} secret
* @param {string} token
* @public
*/
Tokens.prototype.verify = function verify(secret, token) {
if (!secret || typeof secret !== 'string') {
return false;
}
if (!token || typeof token !== 'string') {
return false;
}
const index = token.indexOf('-');
if (index === -1) {
return false;
}
const salt = token.slice(0, index);
const expected = this.privateTokenize(secret, salt);
return compare(token, expected);
};
/**
* Module exports.
* @public
*/
module.exports = Tokens;