-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtoken-model.js
More file actions
80 lines (66 loc) · 1.94 KB
/
token-model.js
File metadata and controls
80 lines (66 loc) · 1.94 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
'use strict';
/**
* Module dependencies.
*/
const InvalidArgumentError = require('../errors/invalid-argument-error');
const { getLifetimeFromExpiresAt } = require('../utils/date-util');
/**
* The core model attributes allowed when allowExtendedTokenAttributes is false.
*/
const modelAttributes = new Set([
'accessToken',
'accessTokenExpiresAt',
'refreshToken',
'refreshTokenExpiresAt',
'scope',
'client',
'user'
]);
class TokenModel {
constructor(data = {}, options = {}) {
const {
accessToken,
accessTokenExpiresAt,
refreshToken,
refreshTokenExpiresAt,
scope,
client,
user,
} = data;
if (!accessToken) {
throw new InvalidArgumentError('Missing parameter: `accessToken`');
}
if (!client) {
throw new InvalidArgumentError('Missing parameter: `client`');
}
if (!user) {
throw new InvalidArgumentError('Missing parameter: `user`');
}
if (accessTokenExpiresAt && !(accessTokenExpiresAt instanceof Date)) {
throw new InvalidArgumentError('Invalid parameter: `accessTokenExpiresAt`');
}
if (refreshTokenExpiresAt && !(refreshTokenExpiresAt instanceof Date)) {
throw new InvalidArgumentError('Invalid parameter: `refreshTokenExpiresAt`');
}
this.accessToken = accessToken;
this.accessTokenExpiresAt = accessTokenExpiresAt;
this.client = client;
this.refreshToken = refreshToken;
this.refreshTokenExpiresAt = refreshTokenExpiresAt;
this.scope = scope;
this.user = user;
if (accessTokenExpiresAt) {
this.accessTokenLifetime = getLifetimeFromExpiresAt(accessTokenExpiresAt);
}
const { allowExtendedTokenAttributes } = options;
if (allowExtendedTokenAttributes) {
this.customAttributes = {};
Object.keys(data).forEach(key => {
if (!modelAttributes.has(key)) {
this.customAttributes[key] = data[key];
}
});
}
}
}
module.exports = TokenModel;