-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthToken.js
127 lines (91 loc) · 4.29 KB
/
AuthToken.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
const jsonwebtoken = require(`../jwt`);
const _ = require(`lodash`);
const moment = require(`moment`);
const logger = require(`@geek/logger`).createLogger(`@geek/jwt`, { meta: { filename: __filename } });
class AuthToken {
constructor(data = {}, params = {}) {
logger.track(`🔒 you are here → AuthToken.constructor`);
this.token_type = data.token_type && data.token_type.toLowerCase();
this.access_token = data.access_token;
this.access_token_jwt = data.access_token_jwt || jsonwebtoken.decode(this.access_token, params.key, false, params.alg);
this.refresh_token = data.refresh_token;
this.refresh_token_jwt = data.refresh_token_jwt || (this.refresh_token ? jsonwebtoken.decode(this.refresh_token, null, true) : undefined);
this.expires_at = data.expires_at || parseExpiresIn(Number(data.expires_in));
this.raw = data.raw || _.omit(data, [ `token_type`, `access_token`, `refresh_token` ]);
if (this.access_token_jwt) {
this.authenticated = true;
this.user = {
username: this.access_token_jwt.username || this.access_token_jwt.preferred_username,
first_name: this.access_token_jwt.given_name,
last_name: this.access_token_jwt.family_name,
formatted_name: this.access_token_jwt.name,
email: this.access_token_jwt.email,
subject_id: this.access_token_jwt.sub,
scopes: _.split(_.trim(this.access_token_jwt.scope || ``), /\s+/g).filter(o => o),
};
this.issuer = this.access_token_jwt.iss;
this.audience = this.access_token_jwt.aud;
this.subject = this.access_token_jwt.sub;
// this.access_token_issued_at = moment.unix(this.access_token_jwt.iat);
// this.access_token_expires_at = moment.unix(this.access_token_jwt.exp);
// DEBUG: access_token_expires_at
logger.debug(`🔑 \x1b[43m access_token_expires_at:\x1b[0m ${JSON.stringify(this.access_token_expires_at, null, 2)}`);
// DEBUG: access_token_expires_in
logger.debug(`🔑 \x1b[43m access_token_expires_in:\x1b[0m ${JSON.stringify(this.access_token_expires_in, null, 2)}`);
// DEBUG: this.access_token_expires_at.fromNow()
logger.debug(`🔑 \x1b[43m this.access_token_expires_at.fromNow():\x1b[0m ${JSON.stringify(this.access_token_expires_at.fromNow(), null, 2)}`);
// DEBUG: refresh_token_expires_at
logger.debug(`🔑 \x1b[43m refresh_token_expires_at:\x1b[0m ${JSON.stringify(this.refresh_token_expires_at, null, 2)}`);
// DEBUG: refresh_token_expires_in
logger.debug(`🔑 \x1b[43m refresh_token_expires_in:\x1b[0m ${JSON.stringify(this.refresh_token_expires_in, null, 2)}`);
// DEBUG: this.refresh_token_expires_at.fromNow()
logger.debug(`🔑 \x1b[43m this.refresh_token_expires_at.fromNow():\x1b[0m ${JSON.stringify(this.refresh_token_expires_at.fromNow(), null, 2)}`);
}
this.expiresIn = () => this.expires_at.fromNow();
}
isExpired() {
return Date.now() > this.expires.getTime();
}
get access_token_issued_at() {
const issued_at = _.get(this, `access_token_jwt.iat`, 0);
return moment.unix(issued_at);
}
get access_token_expires_at() {
const expires_at = _.get(this, `access_token_jwt.exp`, moment().subtract(1, `days`).unix());
return moment.unix(expires_at);
}
get access_token_expires_in() {
return this.access_token_expires_at.fromNow();
}
get refresh_token_issued_at() {
const issued_at = _.get(this, `refresh_token_jwt.iat`, 0);
return moment.unix(issued_at);
}
get refresh_token_expires_at() {
const expires_at = _.get(this, `refresh_token_jwt.exp`, moment().subtract(1, `days`).unix());
return moment.unix(expires_at);
}
get refresh_token_expires_in() {
return this.refresh_token_expires_at.fromNow();
}
isAccessTokenExpired() {
return moment().isSameOrAfter(this.access_token_expires_at.subtract(1, `minutes`));
}
isRefreshTokenExpired() {
return moment().isSameOrAfter(this.refresh_token_expires_at.subtract(1, `minutes`));
}
}
const parseExpiresIn = duration => {
logger.trace(`🦖 you are here → token.parseExpiresIn`);
let expires_at;
if (typeof duration === `number`) {
expires_at = new Date();
expires_at.setSeconds(expires_at.getSeconds() + duration);
} else if (duration instanceof Date) {
expires_at = new Date(duration.getTime());
} else {
throw new TypeError(`Unknown duration: ${duration}`);
}
return expires_at;
};
module.exports = AuthToken;