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