This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
40 lines (30 loc) · 1.09 KB
/
auth.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
var crypto = require('crypto');
function formatDate(d) {
// Eg: Thu, 01 Aug 2017 07:05:08 GMT
var dateString = d.toLocaleString('en-US', {
'weekday': 'short', 'day': '2-digit', 'month': 'short', 'year': 'numeric',
'hour': 'numeric', 'minute': 'numeric', 'second': 'numeric',
'hour12': false, 'timeZone': 'GMT', 'timeZoneName': 'short'
});
var parts = dateString.split(' ');
// swap month and day
parts[2] = [parts[1], parts[1] = parts[2]][0];
// strip comma from day and year
parts[1] = parts[1].slice(0, 2);
parts[3] = parts[3].slice(0, 4);
return parts.join(' ');
}
function expiry(duration) {
var expires = new Date();
expires.setTime(expires.getTime() + duration);
return expires;
}
function base64HMAC(message, key) {
return crypto.createHmac('SHA256', key).update(message).digest('base64')
}
function secureCookie(value, validDuration, cookieKey) {
var expires = formatDate(expiry(validDuration));
var hmac = base64HMAC(value + expires, cookieKey);
return [value, expires, hmac].map(encodeURIComponent).join('|');
}
module.exports.cookie = secureCookie;