1
0
Fork 0
peertube/server/lib/video-tokens-manager.ts
Wicklow 40346ead2b
Feature/password protected videos (#5836)
* Add server endpoints

* Refactoring test suites

* Update server and add openapi documentation

* fix compliation and tests

* upload/import password protected video on client

* add server error code

* Add video password to update resolver

* add custom message when sharing pw protected video

* improve confirm component

* Add new alert in component

* Add ability to watch protected video on client

* Cannot have password protected replay privacy

* Add migration

* Add tests

* update after review

* Update check params tests

* Add live videos test

* Add more filter test

* Update static file privacy test

* Update object storage tests

* Add test on feeds

* Add missing word

* Fix tests

* Fix tests on live videos

* add embed support on password protected videos

* fix style

* Correcting data leaks

* Unable to add password protected privacy on replay

* Updated code based on review comments

* fix validator and command

* Updated code based on review comments
2023-06-29 09:48:55 +02:00

78 lines
2 KiB
TypeScript

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'
// ---------------------------------------------------------------------------
// Create temporary tokens that can be used as URL query parameters to access video static files
// ---------------------------------------------------------------------------
class VideoTokensManager {
private static instance: VideoTokensManager
private readonly lruCache = new LRUCache<string, { videoUUID: string, user?: MUserAccountUrl }>({
max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE,
ttl: LRU_CACHE.VIDEO_TOKENS.TTL
})
private constructor () {}
createForAuthUser (options: {
user: MUserAccountUrl
videoUUID: string
}) {
const { token, expires } = this.generateVideoToken()
this.lruCache.set(token, pick(options, [ 'user', 'videoUUID' ]))
return { token, expires }
}
createForPasswordProtectedVideo (options: {
videoUUID: string
}) {
const { token, expires } = this.generateVideoToken()
this.lruCache.set(token, pick(options, [ 'videoUUID' ]))
return { token, expires }
}
hasToken (options: {
token: string
videoUUID: string
}) {
const value = this.lruCache.get(options.token)
if (!value) return false
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 () {
return this.instance || (this.instance = new this())
}
private generateVideoToken () {
const token = buildUUID()
const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL)
return { token, expires }
}
}
// ---------------------------------------------------------------------------
export {
VideoTokensManager
}