diff options
Diffstat (limited to 'server/models/oauth-token.ts')
-rw-r--r-- | server/models/oauth-token.ts | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/server/models/oauth-token.ts b/server/models/oauth-token.ts new file mode 100644 index 000000000..74c9180eb --- /dev/null +++ b/server/models/oauth-token.ts | |||
@@ -0,0 +1,142 @@ | |||
1 | import { logger } from '../helpers' | ||
2 | |||
3 | // --------------------------------------------------------------------------- | ||
4 | |||
5 | module.exports = function (sequelize, DataTypes) { | ||
6 | const OAuthToken = sequelize.define('OAuthToken', | ||
7 | { | ||
8 | accessToken: { | ||
9 | type: DataTypes.STRING, | ||
10 | allowNull: false | ||
11 | }, | ||
12 | accessTokenExpiresAt: { | ||
13 | type: DataTypes.DATE, | ||
14 | allowNull: false | ||
15 | }, | ||
16 | refreshToken: { | ||
17 | type: DataTypes.STRING, | ||
18 | allowNull: false | ||
19 | }, | ||
20 | refreshTokenExpiresAt: { | ||
21 | type: DataTypes.DATE, | ||
22 | allowNull: false | ||
23 | } | ||
24 | }, | ||
25 | { | ||
26 | indexes: [ | ||
27 | { | ||
28 | fields: [ 'refreshToken' ], | ||
29 | unique: true | ||
30 | }, | ||
31 | { | ||
32 | fields: [ 'accessToken' ], | ||
33 | unique: true | ||
34 | }, | ||
35 | { | ||
36 | fields: [ 'userId' ] | ||
37 | }, | ||
38 | { | ||
39 | fields: [ 'oAuthClientId' ] | ||
40 | } | ||
41 | ], | ||
42 | classMethods: { | ||
43 | associate, | ||
44 | |||
45 | getByRefreshTokenAndPopulateClient, | ||
46 | getByTokenAndPopulateUser, | ||
47 | getByRefreshTokenAndPopulateUser, | ||
48 | removeByUserId | ||
49 | } | ||
50 | } | ||
51 | ) | ||
52 | |||
53 | return OAuthToken | ||
54 | } | ||
55 | |||
56 | // --------------------------------------------------------------------------- | ||
57 | |||
58 | function associate (models) { | ||
59 | this.belongsTo(models.User, { | ||
60 | foreignKey: { | ||
61 | name: 'userId', | ||
62 | allowNull: false | ||
63 | }, | ||
64 | onDelete: 'cascade' | ||
65 | }) | ||
66 | |||
67 | this.belongsTo(models.OAuthClient, { | ||
68 | foreignKey: { | ||
69 | name: 'oAuthClientId', | ||
70 | allowNull: false | ||
71 | }, | ||
72 | onDelete: 'cascade' | ||
73 | }) | ||
74 | } | ||
75 | |||
76 | function getByRefreshTokenAndPopulateClient (refreshToken) { | ||
77 | const query = { | ||
78 | where: { | ||
79 | refreshToken: refreshToken | ||
80 | }, | ||
81 | include: [ this.associations.OAuthClient ] | ||
82 | } | ||
83 | |||
84 | return this.findOne(query).then(function (token) { | ||
85 | if (!token) return token | ||
86 | |||
87 | const tokenInfos = { | ||
88 | refreshToken: token.refreshToken, | ||
89 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | ||
90 | client: { | ||
91 | id: token.client.id | ||
92 | }, | ||
93 | user: { | ||
94 | id: token.user | ||
95 | } | ||
96 | } | ||
97 | |||
98 | return tokenInfos | ||
99 | }).catch(function (err) { | ||
100 | logger.info('getRefreshToken error.', { error: err }) | ||
101 | }) | ||
102 | } | ||
103 | |||
104 | function getByTokenAndPopulateUser (bearerToken) { | ||
105 | const query = { | ||
106 | where: { | ||
107 | accessToken: bearerToken | ||
108 | }, | ||
109 | include: [ this.sequelize.models.User ] | ||
110 | } | ||
111 | |||
112 | return this.findOne(query).then(function (token) { | ||
113 | if (token) token.user = token.User | ||
114 | |||
115 | return token | ||
116 | }) | ||
117 | } | ||
118 | |||
119 | function getByRefreshTokenAndPopulateUser (refreshToken) { | ||
120 | const query = { | ||
121 | where: { | ||
122 | refreshToken: refreshToken | ||
123 | }, | ||
124 | include: [ this.sequelize.models.User ] | ||
125 | } | ||
126 | |||
127 | return this.findOne(query).then(function (token) { | ||
128 | token.user = token.User | ||
129 | |||
130 | return token | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | function removeByUserId (userId, callback) { | ||
135 | const query = { | ||
136 | where: { | ||
137 | userId: userId | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return this.destroy(query).asCallback(callback) | ||
142 | } | ||