|
3 | 3 | /**
|
4 | 4 | * Module dependencies.
|
5 | 5 | */
|
6 |
| - |
7 | 6 | const InvalidArgumentError = require('../errors/invalid-argument-error');
|
| 7 | +const { getLifetimeFromExpiresAt } = require('../utils/date-util'); |
8 | 8 |
|
9 | 9 | /**
|
10 |
| - * Constructor. |
| 10 | + * The core model attributes allowed when allowExtendedTokenAttributes is false. |
11 | 11 | */
|
| 12 | +const modelAttributes = new Set([ |
| 13 | + 'accessToken', |
| 14 | + 'accessTokenExpiresAt', |
| 15 | + 'refreshToken', |
| 16 | + 'refreshTokenExpiresAt', |
| 17 | + 'scope', |
| 18 | + 'client', |
| 19 | + 'user' |
| 20 | +]); |
| 21 | + |
| 22 | +class TokenModel { |
| 23 | + constructor(data = {}, options = {}) { |
| 24 | + const { |
| 25 | + accessToken, |
| 26 | + accessTokenExpiresAt, |
| 27 | + refreshToken, |
| 28 | + refreshTokenExpiresAt, |
| 29 | + scope, |
| 30 | + client, |
| 31 | + user, |
| 32 | + } = data; |
| 33 | + |
| 34 | + if (!accessToken) { |
| 35 | + throw new InvalidArgumentError('Missing parameter: `accessToken`'); |
| 36 | + } |
12 | 37 |
|
13 |
| -const modelAttributes = ['accessToken', 'accessTokenExpiresAt', 'refreshToken', 'refreshTokenExpiresAt', 'scope', 'client', 'user']; |
14 |
| - |
15 |
| -function TokenModel(data, options) { |
16 |
| - data = data || {}; |
| 38 | + if (!client) { |
| 39 | + throw new InvalidArgumentError('Missing parameter: `client`'); |
| 40 | + } |
17 | 41 |
|
18 |
| - if (!data.accessToken) { |
19 |
| - throw new InvalidArgumentError('Missing parameter: `accessToken`'); |
20 |
| - } |
| 42 | + if (!user) { |
| 43 | + throw new InvalidArgumentError('Missing parameter: `user`'); |
| 44 | + } |
21 | 45 |
|
22 |
| - if (!data.client) { |
23 |
| - throw new InvalidArgumentError('Missing parameter: `client`'); |
24 |
| - } |
| 46 | + if (accessTokenExpiresAt && !(accessTokenExpiresAt instanceof Date)) { |
| 47 | + throw new InvalidArgumentError('Invalid parameter: `accessTokenExpiresAt`'); |
| 48 | + } |
25 | 49 |
|
26 |
| - if (!data.user) { |
27 |
| - throw new InvalidArgumentError('Missing parameter: `user`'); |
28 |
| - } |
| 50 | + if (refreshTokenExpiresAt && !(refreshTokenExpiresAt instanceof Date)) { |
| 51 | + throw new InvalidArgumentError('Invalid parameter: `refreshTokenExpiresAt`'); |
| 52 | + } |
29 | 53 |
|
30 |
| - if (data.accessTokenExpiresAt && !(data.accessTokenExpiresAt instanceof Date)) { |
31 |
| - throw new InvalidArgumentError('Invalid parameter: `accessTokenExpiresAt`'); |
32 |
| - } |
| 54 | + this.accessToken = accessToken; |
| 55 | + this.accessTokenExpiresAt = accessTokenExpiresAt; |
| 56 | + this.client = client; |
| 57 | + this.refreshToken = refreshToken; |
| 58 | + this.refreshTokenExpiresAt = refreshTokenExpiresAt; |
| 59 | + this.scope = scope; |
| 60 | + this.user = user; |
33 | 61 |
|
34 |
| - if (data.refreshTokenExpiresAt && !(data.refreshTokenExpiresAt instanceof Date)) { |
35 |
| - throw new InvalidArgumentError('Invalid parameter: `refreshTokenExpiresAt`'); |
36 |
| - } |
| 62 | + if (accessTokenExpiresAt) { |
| 63 | + this.accessTokenLifetime = getLifetimeFromExpiresAt(accessTokenExpiresAt); |
| 64 | + } |
37 | 65 |
|
38 |
| - this.accessToken = data.accessToken; |
39 |
| - this.accessTokenExpiresAt = data.accessTokenExpiresAt; |
40 |
| - this.client = data.client; |
41 |
| - this.refreshToken = data.refreshToken; |
42 |
| - this.refreshTokenExpiresAt = data.refreshTokenExpiresAt; |
43 |
| - this.scope = data.scope; |
44 |
| - this.user = data.user; |
| 66 | + const { allowExtendedTokenAttributes } = options; |
45 | 67 |
|
46 |
| - if (options && options.allowExtendedTokenAttributes) { |
47 |
| - this.customAttributes = {}; |
| 68 | + if (allowExtendedTokenAttributes) { |
| 69 | + this.customAttributes = {}; |
48 | 70 |
|
49 |
| - for (const key in data) { |
50 |
| - if ( Object.prototype.hasOwnProperty.call(data, key) && (modelAttributes.indexOf(key) < 0)) { |
51 |
| - this.customAttributes[key] = data[key]; |
52 |
| - } |
| 71 | + Object.keys(data).forEach(key => { |
| 72 | + if (!modelAttributes.has(key)) { |
| 73 | + this.customAttributes[key] = data[key]; |
| 74 | + } |
| 75 | + }); |
53 | 76 | }
|
54 | 77 | }
|
55 |
| - |
56 |
| - if(this.accessTokenExpiresAt) { |
57 |
| - this.accessTokenLifetime = Math.floor((this.accessTokenExpiresAt - new Date()) / 1000); |
58 |
| - } |
59 | 78 | }
|
60 | 79 |
|
61 |
| -/** |
62 |
| - * Export constructor. |
63 |
| - */ |
64 |
| - |
65 | 80 | module.exports = TokenModel;
|
0 commit comments