aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/oauth-model.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-08-09 11:32:40 +0200
committerChocobozzz <me@florianbigard.com>2019-08-09 11:32:40 +0200
commit557b13ae24019d9ab214bbea7eaa0f892c8f4b05 (patch)
treeaa32396531acf93e3dfdb29880177813039ed77f /server/lib/oauth-model.ts
parentc5407d7046168abb4098df1408e7aa84519cb61a (diff)
downloadPeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.gz
PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.zst
PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.zip
Lazy load avatars
Diffstat (limited to 'server/lib/oauth-model.ts')
-rw-r--r--server/lib/oauth-model.ts34
1 files changed, 16 insertions, 18 deletions
diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts
index 45ac3e7c4..a1153e88a 100644
--- a/server/lib/oauth-model.ts
+++ b/server/lib/oauth-model.ts
@@ -4,13 +4,15 @@ import { logger } from '../helpers/logger'
4import { UserModel } from '../models/account/user' 4import { UserModel } from '../models/account/user'
5import { OAuthClientModel } from '../models/oauth/oauth-client' 5import { OAuthClientModel } from '../models/oauth/oauth-client'
6import { OAuthTokenModel } from '../models/oauth/oauth-token' 6import { OAuthTokenModel } from '../models/oauth/oauth-token'
7import { CACHE } from '../initializers/constants' 7import { LRU_CACHE } from '../initializers/constants'
8import { Transaction } from 'sequelize' 8import { Transaction } from 'sequelize'
9import { CONFIG } from '../initializers/config' 9import { CONFIG } from '../initializers/config'
10import * as LRUCache from 'lru-cache'
10 11
11type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } 12type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
12let accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {} 13
13let userHavingToken: { [ userId: number ]: string } = {} 14const accessTokenCache = new LRUCache<string, OAuthTokenModel>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE })
15const userHavingToken = new LRUCache<number, string>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE })
14 16
15// --------------------------------------------------------------------------- 17// ---------------------------------------------------------------------------
16 18
@@ -21,18 +23,20 @@ function deleteUserToken (userId: number, t?: Transaction) {
21} 23}
22 24
23function clearCacheByUserId (userId: number) { 25function clearCacheByUserId (userId: number) {
24 const token = userHavingToken[userId] 26 const token = userHavingToken.get(userId)
27
25 if (token !== undefined) { 28 if (token !== undefined) {
26 accessTokenCache[ token ] = undefined 29 accessTokenCache.del(token)
27 userHavingToken[ userId ] = undefined 30 userHavingToken.del(userId)
28 } 31 }
29} 32}
30 33
31function clearCacheByToken (token: string) { 34function clearCacheByToken (token: string) {
32 const tokenModel = accessTokenCache[ token ] 35 const tokenModel = accessTokenCache.get(token)
36
33 if (tokenModel !== undefined) { 37 if (tokenModel !== undefined) {
34 userHavingToken[tokenModel.userId] = undefined 38 userHavingToken.del(tokenModel.userId)
35 accessTokenCache[ token ] = undefined 39 accessTokenCache.del(token)
36 } 40 }
37} 41}
38 42
@@ -41,19 +45,13 @@ function getAccessToken (bearerToken: string) {
41 45
42 if (!bearerToken) return Bluebird.resolve(undefined) 46 if (!bearerToken) return Bluebird.resolve(undefined)
43 47
44 if (accessTokenCache[bearerToken] !== undefined) return Bluebird.resolve(accessTokenCache[bearerToken]) 48 if (accessTokenCache.has(bearerToken)) return Bluebird.resolve(accessTokenCache.get(bearerToken))
45 49
46 return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) 50 return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken)
47 .then(tokenModel => { 51 .then(tokenModel => {
48 if (tokenModel) { 52 if (tokenModel) {
49 // Reinit our cache 53 accessTokenCache.set(bearerToken, tokenModel)
50 if (Object.keys(accessTokenCache).length > CACHE.USER_TOKENS.MAX_SIZE) { 54 userHavingToken.set(tokenModel.userId, tokenModel.accessToken)
51 accessTokenCache = {}
52 userHavingToken = {}
53 }
54
55 accessTokenCache[ bearerToken ] = tokenModel
56 userHavingToken[ tokenModel.userId ] = tokenModel.accessToken
57 } 55 }
58 56
59 return tokenModel 57 return tokenModel