From 868314e8bf6bcc325b0fea35887071ef0614a46d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Dec 2022 09:15:49 +0100 Subject: Add ability to get user from file token --- server/lib/plugins/plugin-helpers-builder.ts | 2 +- server/lib/video-tokens-manager.ts | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'server/lib') diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts index 7b1def6e3..e75c0b994 100644 --- a/server/lib/plugins/plugin-helpers-builder.ts +++ b/server/lib/plugins/plugin-helpers-builder.ts @@ -245,7 +245,7 @@ function buildUserHelpers () { }, getAuthUser: (res: express.Response) => { - const user = res.locals.oauth?.token?.User + const user = res.locals.oauth?.token?.User || res.locals.videoFileToken?.user if (!user) return undefined return UserModel.loadByIdFull(user.id) diff --git a/server/lib/video-tokens-manager.ts b/server/lib/video-tokens-manager.ts index c43085d16..17aa29cdd 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 { 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 () { -- cgit v1.2.3