From 3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Oct 2022 16:09:02 +0200 Subject: Put private videos under a specific subdirectory --- client/src/standalone/videos/embed.ts | 22 ++++++++++------- client/src/standalone/videos/shared/auth-http.ts | 28 +++++++++++----------- .../videos/shared/player-manager-options.ts | 12 +++++++++- .../src/standalone/videos/shared/video-fetcher.ts | 17 +++++++++---- 4 files changed, 52 insertions(+), 27 deletions(-) (limited to 'client/src/standalone') diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index 451e54840..c6160151a 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -6,7 +6,7 @@ import { peertubeTranslate } from '../../../../shared/core-utils/i18n' import { HTMLServerConfig, LiveVideo, ResultList, VideoDetails, VideoPlaylist, VideoPlaylistElement } from '../../../../shared/models' import { PeertubePlayerManager } from '../../assets/player' import { TranslationsManager } from '../../assets/player/translations-manager' -import { getParamString, logger } from '../../root-helpers' +import { getParamString, logger, videoRequiresAuth } from '../../root-helpers' import { PeerTubeEmbedApi } from './embed-api' import { AuthHTTP, LiveManager, PeerTubePlugin, PlayerManagerOptions, PlaylistFetcher, PlaylistTracker, VideoFetcher } from './shared' import { PlayerHTML } from './shared/player-html' @@ -167,22 +167,25 @@ export class PeerTubeEmbed { private async buildVideoPlayer (videoResponse: Response, captionsPromise: Promise) { const alreadyHadPlayer = this.resetPlayerElement() - const videoInfoPromise: Promise<{ video: VideoDetails, live?: LiveVideo }> = videoResponse.json() - .then((videoInfo: VideoDetails) => { + const videoInfoPromise = videoResponse.json() + .then(async (videoInfo: VideoDetails) => { this.playerManagerOptions.loadParams(this.config, videoInfo) if (!alreadyHadPlayer && !this.playerManagerOptions.hasAutoplay()) { this.playerHTML.buildPlaceholder(videoInfo) } + const live = videoInfo.isLive + ? await this.videoFetcher.loadLive(videoInfo) + : undefined - if (!videoInfo.isLive) { - return { video: videoInfo } - } + const videoFileToken = videoRequiresAuth(videoInfo) + ? await this.videoFetcher.loadVideoToken(videoInfo) + : undefined - return this.videoFetcher.loadVideoWithLive(videoInfo) + return { live, video: videoInfo, videoFileToken } }) - const [ { video, live }, translations, captionsResponse, PeertubePlayerManagerModule ] = await Promise.all([ + const [ { video, live, videoFileToken }, translations, captionsResponse, PeertubePlayerManagerModule ] = await Promise.all([ videoInfoPromise, this.translationsPromise, captionsPromise, @@ -200,6 +203,9 @@ export class PeerTubeEmbed { translations, serverConfig: this.config, + authorizationHeader: () => this.http.getHeaderTokenValue(), + videoFileToken: () => videoFileToken, + onVideoUpdate: (uuid: string) => this.loadVideoAndBuildPlayer(uuid), playlistTracker: this.playlistTracker, diff --git a/client/src/standalone/videos/shared/auth-http.ts b/client/src/standalone/videos/shared/auth-http.ts index 0356ab8a6..43af5dff4 100644 --- a/client/src/standalone/videos/shared/auth-http.ts +++ b/client/src/standalone/videos/shared/auth-http.ts @@ -1,5 +1,5 @@ import { HttpStatusCode, OAuth2ErrorCode, UserRefreshToken } from '../../../../../shared/models' -import { objectToUrlEncoded, UserTokens } from '../../../root-helpers' +import { OAuthUserTokens, objectToUrlEncoded } from '../../../root-helpers' import { peertubeLocalStorage } from '../../../root-helpers/peertube-web-storage' export class AuthHTTP { @@ -8,30 +8,30 @@ export class AuthHTTP { CLIENT_SECRET: 'client_secret' } - private userTokens: UserTokens + private userOAuthTokens: OAuthUserTokens private headers = new Headers() constructor () { - this.userTokens = UserTokens.getUserTokens(peertubeLocalStorage) + this.userOAuthTokens = OAuthUserTokens.getUserTokens(peertubeLocalStorage) - if (this.userTokens) this.setHeadersFromTokens() + if (this.userOAuthTokens) this.setHeadersFromTokens() } - fetch (url: string, { optionalAuth }: { optionalAuth: boolean }) { + fetch (url: string, { optionalAuth, method }: { optionalAuth: boolean, method?: string }) { const refreshFetchOptions = optionalAuth ? { headers: this.headers } : {} - return this.refreshFetch(url.toString(), refreshFetchOptions) + return this.refreshFetch(url.toString(), { ...refreshFetchOptions, method }) } getHeaderTokenValue () { - return `${this.userTokens.tokenType} ${this.userTokens.accessToken}` + return `${this.userOAuthTokens.tokenType} ${this.userOAuthTokens.accessToken}` } isLoggedIn () { - return !!this.userTokens + return !!this.userOAuthTokens } private refreshFetch (url: string, options?: RequestInit) { @@ -47,7 +47,7 @@ export class AuthHTTP { headers.set('Content-Type', 'application/x-www-form-urlencoded') const data = { - refresh_token: this.userTokens.refreshToken, + refresh_token: this.userOAuthTokens.refreshToken, client_id: clientId, client_secret: clientSecret, response_type: 'code', @@ -64,15 +64,15 @@ export class AuthHTTP { return res.json() }).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => { if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) { - UserTokens.flushLocalStorage(peertubeLocalStorage) + OAuthUserTokens.flushLocalStorage(peertubeLocalStorage) this.removeTokensFromHeaders() return resolve() } - this.userTokens.accessToken = obj.access_token - this.userTokens.refreshToken = obj.refresh_token - UserTokens.saveToLocalStorage(peertubeLocalStorage, this.userTokens) + this.userOAuthTokens.accessToken = obj.access_token + this.userOAuthTokens.refreshToken = obj.refresh_token + OAuthUserTokens.saveToLocalStorage(peertubeLocalStorage, this.userOAuthTokens) this.setHeadersFromTokens() @@ -84,7 +84,7 @@ export class AuthHTTP { return refreshingTokenPromise .catch(() => { - UserTokens.flushLocalStorage(peertubeLocalStorage) + OAuthUserTokens.flushLocalStorage(peertubeLocalStorage) this.removeTokensFromHeaders() }).then(() => fetch(url, { diff --git a/client/src/standalone/videos/shared/player-manager-options.ts b/client/src/standalone/videos/shared/player-manager-options.ts index eed821994..87a84975b 100644 --- a/client/src/standalone/videos/shared/player-manager-options.ts +++ b/client/src/standalone/videos/shared/player-manager-options.ts @@ -17,7 +17,8 @@ import { isP2PEnabled, logger, peertubeLocalStorage, - UserLocalStorageKeys + UserLocalStorageKeys, + videoRequiresAuth } from '../../../root-helpers' import { PeerTubePlugin } from './peertube-plugin' import { PlayerHTML } from './player-html' @@ -154,6 +155,9 @@ export class PlayerManagerOptions { captionsResponse: Response live?: LiveVideo + authorizationHeader: () => string + videoFileToken: () => string + serverConfig: HTMLServerConfig alreadyHadPlayer: boolean @@ -169,9 +173,11 @@ export class PlayerManagerOptions { video, captionsResponse, alreadyHadPlayer, + videoFileToken, translations, playlistTracker, live, + authorizationHeader, serverConfig } = options @@ -227,6 +233,10 @@ export class PlayerManagerOptions { embedUrl: window.location.origin + video.embedPath, embedTitle: video.name, + requiresAuth: videoRequiresAuth(video), + authorizationHeader, + videoFileToken, + errorNotifier: () => { // Empty, we don't have a notifier in the embed }, diff --git a/client/src/standalone/videos/shared/video-fetcher.ts b/client/src/standalone/videos/shared/video-fetcher.ts index b42d622f9..cf6d12831 100644 --- a/client/src/standalone/videos/shared/video-fetcher.ts +++ b/client/src/standalone/videos/shared/video-fetcher.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode, LiveVideo, VideoDetails } from '../../../../../shared/models' +import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models' import { logger } from '../../../root-helpers' import { AuthHTTP } from './auth-http' @@ -36,10 +36,15 @@ export class VideoFetcher { return { captionsPromise, videoResponse } } - loadVideoWithLive (video: VideoDetails) { + loadLive (video: VideoDetails) { return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true }) - .then(res => res.json()) - .then((live: LiveVideo) => ({ video, live })) + .then(res => res.json() as Promise) + } + + loadVideoToken (video: VideoDetails) { + return this.http.fetch(this.getVideoTokenUrl(video.uuid), { optionalAuth: true, method: 'POST' }) + .then(res => res.json() as Promise) + .then(token => token.files.token) } getVideoViewsUrl (videoUUID: string) { @@ -61,4 +66,8 @@ export class VideoFetcher { private getLiveUrl (videoId: string) { return window.location.origin + '/api/v1/videos/live/' + videoId } + + private getVideoTokenUrl (id: string) { + return this.getVideoUrl(id) + '/token' + } } -- cgit v1.2.3