diff options
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/avatar.ts | 11 | ||||
-rw-r--r-- | server/lib/oauth-model.ts | 40 |
2 files changed, 43 insertions, 8 deletions
diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts index 5cfb81fc7..14f0a05f5 100644 --- a/server/lib/avatar.ts +++ b/server/lib/avatar.ts | |||
@@ -3,23 +3,18 @@ import { sendUpdateActor } from './activitypub/send' | |||
3 | import { AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../initializers' | 3 | import { AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../initializers' |
4 | import { updateActorAvatarInstance } from './activitypub' | 4 | import { updateActorAvatarInstance } from './activitypub' |
5 | import { processImage } from '../helpers/image-utils' | 5 | import { processImage } from '../helpers/image-utils' |
6 | import { ActorModel } from '../models/activitypub/actor' | ||
7 | import { AccountModel } from '../models/account/account' | 6 | import { AccountModel } from '../models/account/account' |
8 | import { VideoChannelModel } from '../models/video/video-channel' | 7 | import { VideoChannelModel } from '../models/video/video-channel' |
9 | import { extname, join } from 'path' | 8 | import { extname, join } from 'path' |
10 | 9 | ||
11 | async function updateActorAvatarFile ( | 10 | async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) { |
12 | avatarPhysicalFile: Express.Multer.File, | ||
13 | actor: ActorModel, | ||
14 | accountOrChannel: AccountModel | VideoChannelModel | ||
15 | ) { | ||
16 | const extension = extname(avatarPhysicalFile.filename) | 11 | const extension = extname(avatarPhysicalFile.filename) |
17 | const avatarName = actor.uuid + extension | 12 | const avatarName = accountOrChannel.Actor.uuid + extension |
18 | const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) | 13 | const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) |
19 | await processImage(avatarPhysicalFile, destination, AVATARS_SIZE) | 14 | await processImage(avatarPhysicalFile, destination, AVATARS_SIZE) |
20 | 15 | ||
21 | return sequelizeTypescript.transaction(async t => { | 16 | return sequelizeTypescript.transaction(async t => { |
22 | const updatedActor = await updateActorAvatarInstance(actor, avatarName, t) | 17 | const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t) |
23 | await updatedActor.save({ transaction: t }) | 18 | await updatedActor.save({ transaction: t }) |
24 | 19 | ||
25 | await sendUpdateActor(accountOrChannel, t) | 20 | await sendUpdateActor(accountOrChannel, t) |
diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts index 2f8667e19..5cbe60b82 100644 --- a/server/lib/oauth-model.ts +++ b/server/lib/oauth-model.ts | |||
@@ -4,15 +4,50 @@ import { UserModel } from '../models/account/user' | |||
4 | import { OAuthClientModel } from '../models/oauth/oauth-client' | 4 | import { OAuthClientModel } from '../models/oauth/oauth-client' |
5 | import { OAuthTokenModel } from '../models/oauth/oauth-token' | 5 | import { OAuthTokenModel } from '../models/oauth/oauth-token' |
6 | import { CONFIG } from '../initializers/constants' | 6 | import { CONFIG } from '../initializers/constants' |
7 | import { Transaction } from 'sequelize' | ||
7 | 8 | ||
8 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } | 9 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } |
10 | const accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {} | ||
11 | const userHavingToken: { [ userId: number ]: string } = {} | ||
9 | 12 | ||
10 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
11 | 14 | ||
15 | function deleteUserToken (userId: number, t?: Transaction) { | ||
16 | clearCacheByUserId(userId) | ||
17 | |||
18 | return OAuthTokenModel.deleteUserToken(userId, t) | ||
19 | } | ||
20 | |||
21 | function clearCacheByUserId (userId: number) { | ||
22 | const token = userHavingToken[userId] | ||
23 | if (token !== undefined) { | ||
24 | accessTokenCache[ token ] = undefined | ||
25 | userHavingToken[ userId ] = undefined | ||
26 | } | ||
27 | } | ||
28 | |||
29 | function clearCacheByToken (token: string) { | ||
30 | const tokenModel = accessTokenCache[ token ] | ||
31 | if (tokenModel !== undefined) { | ||
32 | userHavingToken[tokenModel.userId] = undefined | ||
33 | accessTokenCache[ token ] = undefined | ||
34 | } | ||
35 | } | ||
36 | |||
12 | function getAccessToken (bearerToken: string) { | 37 | function getAccessToken (bearerToken: string) { |
13 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') | 38 | logger.debug('Getting access token (bearerToken: ' + bearerToken + ').') |
14 | 39 | ||
40 | if (accessTokenCache[bearerToken] !== undefined) return accessTokenCache[bearerToken] | ||
41 | |||
15 | return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) | 42 | return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) |
43 | .then(tokenModel => { | ||
44 | if (tokenModel) { | ||
45 | accessTokenCache[ bearerToken ] = tokenModel | ||
46 | userHavingToken[ tokenModel.userId ] = tokenModel.accessToken | ||
47 | } | ||
48 | |||
49 | return tokenModel | ||
50 | }) | ||
16 | } | 51 | } |
17 | 52 | ||
18 | function getClient (clientId: string, clientSecret: string) { | 53 | function getClient (clientId: string, clientSecret: string) { |
@@ -48,6 +83,8 @@ async function getUser (usernameOrEmail: string, password: string) { | |||
48 | async function revokeToken (tokenInfo: TokenInfo) { | 83 | async function revokeToken (tokenInfo: TokenInfo) { |
49 | const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) | 84 | const token = await OAuthTokenModel.getByRefreshTokenAndPopulateUser(tokenInfo.refreshToken) |
50 | if (token) { | 85 | if (token) { |
86 | clearCacheByToken(token.accessToken) | ||
87 | |||
51 | token.destroy() | 88 | token.destroy() |
52 | .catch(err => logger.error('Cannot destroy token when revoking token.', { err })) | 89 | .catch(err => logger.error('Cannot destroy token when revoking token.', { err })) |
53 | } | 90 | } |
@@ -85,6 +122,9 @@ async function saveToken (token: TokenInfo, client: OAuthClientModel, user: User | |||
85 | 122 | ||
86 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications | 123 | // See https://github.com/oauthjs/node-oauth2-server/wiki/Model-specification for the model specifications |
87 | export { | 124 | export { |
125 | deleteUserToken, | ||
126 | clearCacheByUserId, | ||
127 | clearCacheByToken, | ||
88 | getAccessToken, | 128 | getAccessToken, |
89 | getClient, | 129 | getClient, |
90 | getRefreshToken, | 130 | getRefreshToken, |