X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Flib%2Fvideo-tokens-manager.ts;h=660533528d7fc80db2c517de931580e3c7799aae;hb=83002a823465fe03a8d82833cb2e073a383405a8;hp=c43085d167631d5bf08956a31c20aceb7dda922f;hpb=3545e72c686ff1725bbdfd8d16d693e2f4aa75a3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/video-tokens-manager.ts b/server/lib/video-tokens-manager.ts index c43085d16..660533528 100644 --- a/server/lib/video-tokens-manager.ts +++ b/server/lib/video-tokens-manager.ts @@ -1,5 +1,7 @@ -import LRUCache from 'lru-cache' +import { LRUCache } from 'lru-cache' import { LRU_CACHE } from '@server/initializers/constants' +import { MUserAccountUrl } from '@server/types/models' +import { pick } from '@shared/core-utils' import { buildUUID } from '@shared/extra-utils' // --------------------------------------------------------------------------- @@ -10,19 +12,22 @@ class VideoTokensManager { private static instance: VideoTokensManager - private readonly lruCache = new LRUCache({ + private readonly lruCache = new LRUCache({ max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE, ttl: LRU_CACHE.VIDEO_TOKENS.TTL }) private constructor () {} - create (videoUUID: string) { + create (options: { + user: MUserAccountUrl + videoUUID: string + }) { const token = buildUUID() const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL) - this.lruCache.set(token, videoUUID) + this.lruCache.set(token, pick(options, [ 'user', 'videoUUID' ])) return { token, expires } } @@ -34,7 +39,16 @@ class VideoTokensManager { const value = this.lruCache.get(options.token) if (!value) return false - return value === options.videoUUID + return value.videoUUID === options.videoUUID + } + + getUserFromToken (options: { + token: string + }) { + const value = this.lruCache.get(options.token) + if (!value) return undefined + + return value.user } static get Instance () {