This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookieSession.js
More file actions
106 lines (77 loc) · 2.96 KB
/
cookieSession.js
File metadata and controls
106 lines (77 loc) · 2.96 KB
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
// Prepare Module
class authSystem {
// Constructor
constructor() {
// Default Values
this.default = {
// Check Auth Time
checkAuthTime: (decodedIdToken) => {
// Only process if the user just signed in in the last 5 minutes.
if (new Date().getTime() / 1000 - decodedIdToken.auth_time < 5 * 60) {
return true;
}
// Nope
else { return false; }
},
// Cookie Time Generator
cookieTimeGenerator: () => {
// Set session expiration to 5 days.
const expiresIn = 60 * 60 * 24 * 5 * 1000;
// Create the session cookie. This will also verify the ID token in the process.
// The session cookie will have the same claims as the ID token.
// To only allow session cookie setting on recent sign-in, auth_time in ID token
// can be checked to ensure user was recently signed in before creating a session cookie.
// Complete
return expiresIn;
}
};
// Set Value
this.checkAuthTime = this.default.checkAuthTime;
this.cookieTimeGenerator = this.default.cookieTimeGenerator;
// Complete
return this;
}
// Check Auth Time
setCookieTimeGenerator(callback) {
if (typeof callback === "function") { this.cookieTimeGenerator = callback; }
return;
}
// Check Auth Time
setCheckAuthTime(callback) {
if (typeof callback === "function") { this.checkAuthTime = callback; }
return;
}
// Cookie Session Generator
genCookieSession(auth, token) {
const tinyThis = this;
return new Promise(function (resolve, reject) {
auth.verifyIdToken(token).then(async (decodedIdToken) => {
// Try
try {
// Check Time
const checkedTime = await tinyThis.checkAuthTime(decodedIdToken);
if (checkedTime) {
// Create Session
const expiresIn = await tinyThis.cookieTimeGenerator(decodedIdToken);
auth.createSessionCookie(token, { expiresIn }).then((sessionCookie) => {
resolve(sessionCookie); return;
}).catch(err => { reject(err); return; });
}
// Nope
else {
const err = new Error('Invalid Account ID Token Time.');
err.code = 401;
reject(err);
}
}
// Fail
catch (err) { reject(err); }
// Complete
return;
}).catch(err => { reject(err); return; });
return;
});
}
};
// Module
module.exports = authSystem;