-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
242 lines (197 loc) · 6.77 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
var Class = require( 'findhit-class' ),
Util = require( 'findhit-util' ),
crypto = require( 'crypto' ),
debug = require( 'debug' )( 'token-generator' );
var TokenGenerator = module.exports = Class.extend({
options: {
/**
* @option salt
* @required
*
* Salt for generate different tokens
*/
salt: undefined,
/**
* @option timestampMap
* @required
*
* Timestamp replacement in leters
* should always have 10 chars!
*
* {String}.length 10
*/
timestampMap: undefined,
/**
* @option timestampObfuscater
* @required
*
* @param {Number} timestamp
* @param {String} map
* @return {String}
*/
timestampObfuscater: function ( timestamp, map ) {
var tokenGenerator = this;
return Array.prototype.map.call( timestamp + '', function ( number ) {
number = parseInt( number );
return tokenGenerator.options.timestampMap[ number ];
})
.join( '' );
},
/**
* @option timestampDeobfuscater
* @required
*
* @param {Number} timestamp
* @param {String} map
* @return {String}
*/
timestampDeobfuscater: function ( token ) {
var tokenGenerator = this;
return parseInt(
Array.prototype.map.call( token + '', function ( letter ) {
return tokenGenerator.options.timestampMap.indexOf( letter );
})
.join( '' )
);
},
/**
* @option expiresOn
*
* Token Expiration in seconds
*
* {Integer}
*/
expiresOn: 60 * 60, // 1 hour
/**
* @option hashGeneratorMethod
* @required
*
* Hash generator method
* This is an option since we use another method for generating hash.
*
* {Function}
*/
hashGeneratorMethod: function ( timestamp ) {
return crypto.createHash( 'md5' )
.update(
timestamp +
this.options.salt +
this.options.timestampMap
)
.digest( 'hex' );
},
/**
* @option hashLength
* @required
*
* Length of hash gathering to mix up with token
* used as first argument of `.substr` method
*
* {Integer}
*/
hashLength: 4,
},
initialize: function ( options ) {
options = this.setOptions( options );
// Check for salt
if( Util.isnt.String( options.salt ) || ! options.salt ) {
throw new TypeError( "options.salt should always be a non-empty string" );
}
// Check for timestampMap
//
if ( Util.isnt.String( options.timestampMap ) || options.timestampMap.length !== 10 ) {
throw new TypeError( "options.timestampMap should always be a 10 lenght string" );
}
if ( Util.isnt.Number( options.hashLength ) ) {
throw new TypeError( "options.hashLength should be an integer" );
}
if ( Util.isnt.Number( options.expiresOn ) ) {
throw new TypeError( "options.expiresOn is invalid" );
}
},
generate: function ( expireTimestamp ) {
var expireTimestamp = (
expireTimestamp ||
currentTimestamp() + this.options.expiresOn
);
debug( 'starting token generation with timestamp "%s"', expireTimestamp );
var hashPart = (
this.options.hashGeneratorMethod.call( this, expireTimestamp )
.substr( 0, this.options.hashLength )
),
timestampPart = (
this.options.timestampObfuscater.call( this,
expireTimestamp,
this.options.timestampMap
)
);
debug( 'expireTimestamp "%s" has hashPart "%s"', expireTimestamp, hashPart );
debug( 'expireTimestamp "%s" has timestampPart "%s"', expireTimestamp, timestampPart );
return hashPart + timestampPart;
},
validate: function ( token ) {
debug( 'trying to validate token "%s"', token );
var hashPart = token.substr( 0, this.options.hashLength ),
timestampPart = token.substr( this.options.hashLength ),
timestamp = parseInt(
this.options.timestampDeobfuscater.call( this, timestampPart )
);
debug( 'token "%s" has hashPart "%s"', token, hashPart );
debug( 'token "%s" has timestampPart "%s"', token, timestampPart );
debug( 'token "%s" has timestamp "%s"', token, timestamp );
try{
if ( Util.isnt.Number( timestamp ) ) {
throw new Error( "token has invalid timestamp on it" );
}
// Check if timestamp is expired
if ( timestamp > currentTimestamp() + this.options.expiresOn ) {
throw new Error( "token is expired" );
}
if ( Util.isnt.String( hashPart ) ) {
throw new Error( "token has invalid hashPart on it" );
}
if ( hashPart.length !== this.options.hashLength ) {
throw new Error( "hashPart has invalid length" );
}
// Since we have timestamp, lets create hash based on that timestamp
// and check if hashpart is equal
var hash = this.options.hashGeneratorMethod.call( this, timestamp );
if ( hashPart !== hash.substr( 0, this.options.hashLength ) ) {
throw new Error( "hashPart doesn't match with expected hashPart" );
}
} catch ( error ) {
debug( 'token "%s" validation has failed: %s', token, error + '' );
// In case any error occurs, lets wrap it into an Invalid Token one.
// You can access it via err.parent
var err = new Error( "Invalid or expired token" );
err.parent = error;
throw err;
}
}
});
TokenGenerator.prototype.isValid =
TokenGenerator.prototype.isntInvalid =
function ( token ) {
try {
this.validate( token );
} catch ( err ) {
return false;
}
return true;
};
TokenGenerator.prototype.isntValid =
TokenGenerator.prototype.isInvalid =
function ( token ) {
try {
this.validate( token );
} catch ( err ) {
return true;
}
return false;
};
//
// Private methods
//
function currentTimestamp() {
return parseInt( new Date().valueOf() / 1000 ) ;
}