]>
Commit | Line | Data |
---|---|---|
f201a749 | 1 | import { |
d175a6f7 | 2 | AfterDestroy, |
f201a749 C |
3 | AfterUpdate, |
4 | AllowNull, | |
5 | BelongsTo, | |
6 | Column, | |
7 | CreatedAt, | |
8 | ForeignKey, | |
9 | Model, | |
10 | Scopes, | |
11 | Table, | |
12 | UpdatedAt | |
13 | } from 'sequelize-typescript' | |
da854ddd | 14 | import { logger } from '../../helpers/logger' |
3fd3ab2d C |
15 | import { UserModel } from '../account/user' |
16 | import { OAuthClientModel } from './oauth-client' | |
e6921918 | 17 | import { Transaction } from 'sequelize' |
91411dba C |
18 | import { AccountModel } from '../account/account' |
19 | import { ActorModel } from '../activitypub/actor' | |
f201a749 | 20 | import { clearCacheByToken } from '../../lib/oauth-model' |
453e83ea C |
21 | import * as Bluebird from 'bluebird' |
22 | import { MOAuthTokenUser } from '@server/typings/models/oauth/oauth-token' | |
3fd3ab2d C |
23 | |
24 | export type OAuthTokenInfo = { | |
25 | refreshToken: string | |
a1587156 | 26 | refreshTokenExpiresAt: Date |
3fd3ab2d C |
27 | client: { |
28 | id: number | |
a1587156 | 29 | } |
3fd3ab2d C |
30 | user: { |
31 | id: number | |
32 | } | |
33 | } | |
2f372a86 | 34 | |
d48ff09d | 35 | enum ScopeNames { |
91411dba | 36 | WITH_USER = 'WITH_USER' |
d48ff09d C |
37 | } |
38 | ||
3acc5084 | 39 | @Scopes(() => ({ |
91411dba | 40 | [ScopeNames.WITH_USER]: { |
d48ff09d C |
41 | include: [ |
42 | { | |
3acc5084 | 43 | model: UserModel.unscoped(), |
91411dba | 44 | required: true, |
d48ff09d C |
45 | include: [ |
46 | { | |
91411dba | 47 | attributes: [ 'id' ], |
3acc5084 | 48 | model: AccountModel.unscoped(), |
91411dba C |
49 | required: true, |
50 | include: [ | |
51 | { | |
5c6d985f | 52 | attributes: [ 'id', 'url' ], |
3acc5084 | 53 | model: ActorModel.unscoped(), |
91411dba C |
54 | required: true |
55 | } | |
56 | ] | |
d48ff09d C |
57 | } |
58 | ] | |
59 | } | |
3acc5084 | 60 | ] |
d48ff09d | 61 | } |
3acc5084 | 62 | })) |
3fd3ab2d C |
63 | @Table({ |
64 | tableName: 'oAuthToken', | |
65 | indexes: [ | |
feb4bdfd | 66 | { |
3fd3ab2d C |
67 | fields: [ 'refreshToken' ], |
68 | unique: true | |
feb4bdfd C |
69 | }, |
70 | { | |
3fd3ab2d C |
71 | fields: [ 'accessToken' ], |
72 | unique: true | |
73 | }, | |
74 | { | |
75 | fields: [ 'userId' ] | |
76 | }, | |
77 | { | |
78 | fields: [ 'oAuthClientId' ] | |
feb4bdfd | 79 | } |
3fd3ab2d C |
80 | ] |
81 | }) | |
82 | export class OAuthTokenModel extends Model<OAuthTokenModel> { | |
feb4bdfd | 83 | |
3fd3ab2d C |
84 | @AllowNull(false) |
85 | @Column | |
86 | accessToken: string | |
e02643f3 | 87 | |
3fd3ab2d C |
88 | @AllowNull(false) |
89 | @Column | |
90 | accessTokenExpiresAt: Date | |
e02643f3 | 91 | |
3fd3ab2d C |
92 | @AllowNull(false) |
93 | @Column | |
94 | refreshToken: string | |
69b0a27c | 95 | |
3fd3ab2d C |
96 | @AllowNull(false) |
97 | @Column | |
98 | refreshTokenExpiresAt: Date | |
69b0a27c | 99 | |
3fd3ab2d C |
100 | @CreatedAt |
101 | createdAt: Date | |
102 | ||
103 | @UpdatedAt | |
104 | updatedAt: Date | |
105 | ||
106 | @ForeignKey(() => UserModel) | |
107 | @Column | |
108 | userId: number | |
109 | ||
110 | @BelongsTo(() => UserModel, { | |
feb4bdfd | 111 | foreignKey: { |
feb4bdfd C |
112 | allowNull: false |
113 | }, | |
114 | onDelete: 'cascade' | |
115 | }) | |
3fd3ab2d | 116 | User: UserModel |
319d072e | 117 | |
3fd3ab2d C |
118 | @ForeignKey(() => OAuthClientModel) |
119 | @Column | |
120 | oAuthClientId: number | |
121 | ||
122 | @BelongsTo(() => OAuthClientModel, { | |
319d072e | 123 | foreignKey: { |
319d072e C |
124 | allowNull: false |
125 | }, | |
126 | onDelete: 'cascade' | |
127 | }) | |
3fd3ab2d | 128 | OAuthClients: OAuthClientModel[] |
feb4bdfd | 129 | |
f201a749 | 130 | @AfterUpdate |
d175a6f7 | 131 | @AfterDestroy |
f201a749 C |
132 | static removeTokenCache (token: OAuthTokenModel) { |
133 | return clearCacheByToken(token.accessToken) | |
134 | } | |
135 | ||
3fd3ab2d C |
136 | static getByRefreshTokenAndPopulateClient (refreshToken: string) { |
137 | const query = { | |
138 | where: { | |
139 | refreshToken: refreshToken | |
140 | }, | |
141 | include: [ OAuthClientModel ] | |
142 | } | |
143 | ||
144 | return OAuthTokenModel.findOne(query) | |
145 | .then(token => { | |
146 | if (!token) return null | |
147 | ||
148 | return { | |
149 | refreshToken: token.refreshToken, | |
150 | refreshTokenExpiresAt: token.refreshTokenExpiresAt, | |
151 | client: { | |
152 | id: token.oAuthClientId | |
153 | }, | |
154 | user: { | |
155 | id: token.userId | |
156 | } | |
157 | } as OAuthTokenInfo | |
158 | }) | |
159 | .catch(err => { | |
e6921918 | 160 | logger.error('getRefreshToken error.', { err }) |
3fd3ab2d C |
161 | throw err |
162 | }) | |
feb4bdfd C |
163 | } |
164 | ||
453e83ea | 165 | static getByTokenAndPopulateUser (bearerToken: string): Bluebird<MOAuthTokenUser> { |
3fd3ab2d C |
166 | const query = { |
167 | where: { | |
168 | accessToken: bearerToken | |
d48ff09d | 169 | } |
3fd3ab2d | 170 | } |
2f372a86 | 171 | |
3acc5084 C |
172 | return OAuthTokenModel.scope(ScopeNames.WITH_USER) |
173 | .findOne(query) | |
174 | .then(token => { | |
453e83ea | 175 | if (!token) return null |
69b0a27c | 176 | |
453e83ea | 177 | return Object.assign(token, { user: token.User }) |
3acc5084 | 178 | }) |
feb4bdfd C |
179 | } |
180 | ||
453e83ea | 181 | static getByRefreshTokenAndPopulateUser (refreshToken: string): Bluebird<MOAuthTokenUser> { |
3fd3ab2d C |
182 | const query = { |
183 | where: { | |
184 | refreshToken: refreshToken | |
d48ff09d | 185 | } |
3fd3ab2d | 186 | } |
feb4bdfd | 187 | |
91411dba | 188 | return OAuthTokenModel.scope(ScopeNames.WITH_USER) |
d48ff09d C |
189 | .findOne(query) |
190 | .then(token => { | |
453e83ea C |
191 | if (!token) return new OAuthTokenModel() |
192 | ||
193 | return Object.assign(token, { user: token.User }) | |
d48ff09d | 194 | }) |
feb4bdfd | 195 | } |
f8b8c36b | 196 | |
e6921918 | 197 | static deleteUserToken (userId: number, t?: Transaction) { |
f8b8c36b C |
198 | const query = { |
199 | where: { | |
200 | userId | |
e6921918 C |
201 | }, |
202 | transaction: t | |
f8b8c36b C |
203 | } |
204 | ||
205 | return OAuthTokenModel.destroy(query) | |
206 | } | |
2f372a86 | 207 | } |