aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/oauth/oauth-token.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/oauth/oauth-token.ts')
-rw-r--r--server/models/oauth/oauth-token.ts47
1 files changed, 38 insertions, 9 deletions
diff --git a/server/models/oauth/oauth-token.ts b/server/models/oauth/oauth-token.ts
index 4c53848dc..ef9592c04 100644
--- a/server/models/oauth/oauth-token.ts
+++ b/server/models/oauth/oauth-token.ts
@@ -1,9 +1,23 @@
1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' 1import {
2 AfterDelete,
3 AfterUpdate,
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 ForeignKey,
9 Model,
10 Scopes,
11 Table,
12 UpdatedAt
13} from 'sequelize-typescript'
2import { logger } from '../../helpers/logger' 14import { logger } from '../../helpers/logger'
3import { AccountModel } from '../account/account'
4import { UserModel } from '../account/user' 15import { UserModel } from '../account/user'
5import { OAuthClientModel } from './oauth-client' 16import { OAuthClientModel } from './oauth-client'
6import { Transaction } from 'sequelize' 17import { Transaction } from 'sequelize'
18import { AccountModel } from '../account/account'
19import { ActorModel } from '../activitypub/actor'
20import { clearCacheByToken } from '../../lib/oauth-model'
7 21
8export type OAuthTokenInfo = { 22export type OAuthTokenInfo = {
9 refreshToken: string 23 refreshToken: string
@@ -17,18 +31,27 @@ export type OAuthTokenInfo = {
17} 31}
18 32
19enum ScopeNames { 33enum ScopeNames {
20 WITH_ACCOUNT = 'WITH_ACCOUNT' 34 WITH_USER = 'WITH_USER'
21} 35}
22 36
23@Scopes({ 37@Scopes({
24 [ScopeNames.WITH_ACCOUNT]: { 38 [ScopeNames.WITH_USER]: {
25 include: [ 39 include: [
26 { 40 {
27 model: () => UserModel, 41 model: () => UserModel.unscoped(),
42 required: true,
28 include: [ 43 include: [
29 { 44 {
30 model: () => AccountModel, 45 attributes: [ 'id' ],
31 required: true 46 model: () => AccountModel.unscoped(),
47 required: true,
48 include: [
49 {
50 attributes: [ 'id' ],
51 model: () => ActorModel.unscoped(),
52 required: true
53 }
54 ]
32 } 55 }
33 ] 56 ]
34 } 57 }
@@ -102,6 +125,12 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
102 }) 125 })
103 OAuthClients: OAuthClientModel[] 126 OAuthClients: OAuthClientModel[]
104 127
128 @AfterUpdate
129 @AfterDelete
130 static removeTokenCache (token: OAuthTokenModel) {
131 return clearCacheByToken(token.accessToken)
132 }
133
105 static getByRefreshTokenAndPopulateClient (refreshToken: string) { 134 static getByRefreshTokenAndPopulateClient (refreshToken: string) {
106 const query = { 135 const query = {
107 where: { 136 where: {
@@ -138,7 +167,7 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
138 } 167 }
139 } 168 }
140 169
141 return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query).then(token => { 170 return OAuthTokenModel.scope(ScopeNames.WITH_USER).findOne(query).then(token => {
142 if (token) token['user'] = token.User 171 if (token) token['user'] = token.User
143 172
144 return token 173 return token
@@ -152,7 +181,7 @@ export class OAuthTokenModel extends Model<OAuthTokenModel> {
152 } 181 }
153 } 182 }
154 183
155 return OAuthTokenModel.scope(ScopeNames.WITH_ACCOUNT) 184 return OAuthTokenModel.scope(ScopeNames.WITH_USER)
156 .findOne(query) 185 .findOne(query)
157 .then(token => { 186 .then(token => {
158 if (token) { 187 if (token) {