Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect private/internal video files #5370

Merged
merged 6 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
PGHOST: localhost
NODE_PENDING_JOB_WAIT: 250
ENABLE_OBJECT_STORAGE_TESTS: true
OBJECT_STORAGE_SCALEWAY_KEY_ID: ${{ secrets.OBJECT_STORAGE_SCALEWAY_KEY_ID }}
OBJECT_STORAGE_SCALEWAY_ACCESS_KEY: ${{ secrets.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY }}

steps:
- uses: actions/checkout@v3
Expand Down
53 changes: 37 additions & 16 deletions client/src/app/+videos/+video-watch/video-watch.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {
} from '@app/core'
import { HooksService } from '@app/core/plugins/hooks.service'
import { isXPercentInViewport, scrollToTop } from '@app/helpers'
import { Video, VideoCaptionService, VideoDetails, VideoService } from '@app/shared/shared-main'
import { Video, VideoCaptionService, VideoDetails, VideoFileTokenService, VideoService } from '@app/shared/shared-main'
import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription'
import { LiveVideoService } from '@app/shared/shared-video-live'
import { VideoPlaylist, VideoPlaylistService } from '@app/shared/shared-video-playlist'
import { logger } from '@root-helpers/logger'
import { isP2PEnabled } from '@root-helpers/video'
import { isP2PEnabled, videoRequiresAuth } from '@root-helpers/video'
import { timeToInt } from '@shared/core-utils'
import {
HTMLServerConfig,
Expand Down Expand Up @@ -78,6 +78,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
private nextVideoUUID = ''
private nextVideoTitle = ''

private videoFileToken: string

private currentTime: number

private paramsSub: Subscription
Expand Down Expand Up @@ -110,6 +112,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
private pluginService: PluginService,
private peertubeSocket: PeerTubeSocket,
private screenService: ScreenService,
private videoFileTokenService: VideoFileTokenService,
private location: PlatformLocation,
@Inject(LOCALE_ID) private localeId: string
) { }
Expand Down Expand Up @@ -252,12 +255,19 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
'filter:api.video-watch.video.get.result'
)

const videoAndLiveObs: Observable<{ video: VideoDetails, live?: LiveVideo }> = videoObs.pipe(
const videoAndLiveObs: Observable<{ video: VideoDetails, live?: LiveVideo, videoFileToken?: string }> = videoObs.pipe(
switchMap(video => {
if (!video.isLive) return of({ video })
if (!video.isLive) return of({ video, live: undefined })

return this.liveVideoService.getVideoLive(video.uuid)
.pipe(map(live => ({ live, video })))
}),

switchMap(({ video, live }) => {
if (!videoRequiresAuth(video)) return of({ video, live, videoFileToken: undefined })

return this.videoFileTokenService.getVideoFileToken(video.uuid)
.pipe(map(({ token }) => ({ video, live, videoFileToken: token })))
})
)

Expand All @@ -266,7 +276,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
this.videoCaptionService.listCaptions(videoId),
this.userService.getAnonymousOrLoggedUser()
]).subscribe({
next: ([ { video, live }, captionsResult, loggedInOrAnonymousUser ]) => {
next: ([ { video, live, videoFileToken }, captionsResult, loggedInOrAnonymousUser ]) => {
const queryParams = this.route.snapshot.queryParams

const urlOptions = {
Expand All @@ -283,7 +293,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
peertubeLink: false
}

this.onVideoFetched({ video, live, videoCaptions: captionsResult.data, loggedInOrAnonymousUser, urlOptions })
this.onVideoFetched({ video, live, videoCaptions: captionsResult.data, videoFileToken, loggedInOrAnonymousUser, urlOptions })
.catch(err => this.handleGlobalError(err))
},

Expand Down Expand Up @@ -356,16 +366,19 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
video: VideoDetails
live: LiveVideo
videoCaptions: VideoCaption[]
videoFileToken: string

urlOptions: URLOptions
loggedInOrAnonymousUser: User
}) {
const { video, live, videoCaptions, urlOptions, loggedInOrAnonymousUser } = options
const { video, live, videoCaptions, urlOptions, videoFileToken, loggedInOrAnonymousUser } = options

this.subscribeToLiveEventsIfNeeded(this.video, video)

this.video = video
this.videoCaptions = videoCaptions
this.liveVideo = live
this.videoFileToken = videoFileToken

// Re init attributes
this.playerPlaceholderImgSrc = undefined
Expand Down Expand Up @@ -414,6 +427,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
video: this.video,
videoCaptions: this.videoCaptions,
liveVideo: this.liveVideo,
videoFileToken: this.videoFileToken,
urlOptions,
loggedInOrAnonymousUser,
user: this.user
Expand Down Expand Up @@ -561,11 +575,15 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
video: VideoDetails
liveVideo: LiveVideo
videoCaptions: VideoCaption[]

videoFileToken: string

urlOptions: CustomizationOptions & { playerMode: PlayerMode }

loggedInOrAnonymousUser: User
user?: AuthUser // Keep for plugins
}) {
const { video, liveVideo, videoCaptions, urlOptions, loggedInOrAnonymousUser } = params
const { video, liveVideo, videoCaptions, videoFileToken, urlOptions, loggedInOrAnonymousUser } = params

const getStartTime = () => {
const byUrl = urlOptions.startTime !== undefined
Expand Down Expand Up @@ -623,13 +641,6 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
theaterButton: true,
captions: videoCaptions.length !== 0,

videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
? this.videoService.getVideoViewUrl(video.uuid)
: null,
authorizationHeader: this.authService.getRequestHeaderValue(),

metricsUrl: environment.apiUrl + '/api/v1/metrics/playback',

embedUrl: video.embedUrl,
embedTitle: video.name,
instanceName: this.serverConfig.instance.name,
Expand All @@ -639,7 +650,17 @@ export class VideoWatchComponent implements OnInit, OnDestroy {

language: this.localeId,

serverUrl: environment.apiUrl,
metricsUrl: environment.apiUrl + '/api/v1/metrics/playback',

videoViewUrl: video.privacy.id !== VideoPrivacy.PRIVATE
? this.videoService.getVideoViewUrl(video.uuid)
: null,
authorizationHeader: () => this.authService.getRequestHeaderValue(),

serverUrl: environment.originServerUrl,

videoFileToken: () => videoFileToken,
requiresAuth: videoRequiresAuth(video),

videoCaptions: playerCaptions,

Expand Down
18 changes: 9 additions & 9 deletions client/src/app/core/auth/auth-user.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
import { User } from '@app/core/users/user.model'
import { UserTokens } from '@root-helpers/users'
import { OAuthUserTokens } from '@root-helpers/users'
import { hasUserRight } from '@shared/core-utils/users'
import {
MyUser as ServerMyUserModel,
Expand All @@ -13,33 +13,33 @@ import {
} from '@shared/models'

export class AuthUser extends User implements ServerMyUserModel {
tokens: UserTokens
oauthTokens: OAuthUserTokens
specialPlaylists: MyUserSpecialPlaylist[]

canSeeVideosLink = true

constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<OAuthUserTokens>) {
super(userHash)

this.tokens = new UserTokens(hashTokens)
this.oauthTokens = new OAuthUserTokens(hashTokens)
this.specialPlaylists = userHash.specialPlaylists
}

getAccessToken () {
return this.tokens.accessToken
return this.oauthTokens.accessToken
}

getRefreshToken () {
return this.tokens.refreshToken
return this.oauthTokens.refreshToken
}

getTokenType () {
return this.tokens.tokenType
return this.oauthTokens.tokenType
}

refreshTokens (accessToken: string, refreshToken: string) {
this.tokens.accessToken = accessToken
this.tokens.refreshToken = refreshToken
this.oauthTokens.accessToken = accessToken
this.oauthTokens.refreshToken = refreshToken
}

hasRight (right: UserRight) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular
import { Injectable } from '@angular/core'
import { Router } from '@angular/router'
import { Notifier } from '@app/core/notification/notifier.service'
import { logger, objectToUrlEncoded, peertubeLocalStorage, UserTokens } from '@root-helpers/index'
import { logger, OAuthUserTokens, objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index'
import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models'
import { environment } from '../../../environments/environment'
import { RestExtractor } from '../rest/rest-extractor.service'
Expand Down Expand Up @@ -74,7 +74,7 @@ export class AuthService {
]
}

buildAuthUser (userInfo: Partial<User>, tokens: UserTokens) {
buildAuthUser (userInfo: Partial<User>, tokens: OAuthUserTokens) {
this.user = new AuthUser(userInfo, tokens)
}

Expand Down
14 changes: 7 additions & 7 deletions client/src/app/core/users/user-local-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Injectable } from '@angular/core'
import { AuthService, AuthStatus } from '@app/core/auth'
import { getBoolOrDefault } from '@root-helpers/local-storage-utils'
import { logger } from '@root-helpers/logger'
import { UserLocalStorageKeys, UserTokens } from '@root-helpers/users'
import { UserLocalStorageKeys, OAuthUserTokens } from '@root-helpers/users'
import { UserRole, UserUpdateMe } from '@shared/models'
import { NSFWPolicyType } from '@shared/models/videos'
import { ServerService } from '../server'
Expand All @@ -24,7 +24,7 @@ export class UserLocalStorageService {

this.setLoggedInUser(user)
this.setUserInfo(user)
this.setTokens(user.tokens)
this.setTokens(user.oauthTokens)
}
})

Expand All @@ -43,7 +43,7 @@ export class UserLocalStorageService {
next: () => {
const user = this.authService.getUser()

this.setTokens(user.tokens)
this.setTokens(user.oauthTokens)
}
})
}
Expand Down Expand Up @@ -174,14 +174,14 @@ export class UserLocalStorageService {
// ---------------------------------------------------------------------------

getTokens () {
return UserTokens.getUserTokens(this.localStorageService)
return OAuthUserTokens.getUserTokens(this.localStorageService)
}

setTokens (tokens: UserTokens) {
UserTokens.saveToLocalStorage(this.localStorageService, tokens)
setTokens (tokens: OAuthUserTokens) {
OAuthUserTokens.saveToLocalStorage(this.localStorageService, tokens)
}

flushTokens () {
UserTokens.flushLocalStorage(this.localStorageService)
OAuthUserTokens.flushLocalStorage(this.localStorageService)
}
}
5 changes: 3 additions & 2 deletions client/src/app/helpers/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ function objectToFormData (obj: any, form?: FormData, namespace?: string) {
}

export {
objectToFormData,
getAbsoluteAPIUrl,
getAPIHost,
getAbsoluteEmbedUrl
getAbsoluteEmbedUrl,

objectToFormData
}
11 changes: 10 additions & 1 deletion client/src/app/shared/shared-main/shared-main.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ import {
import { PluginPlaceholderComponent, PluginSelectorDirective } from './plugins'
import { ActorRedirectGuard } from './router'
import { UserHistoryService, UserNotificationsComponent, UserNotificationService, UserQuotaComponent } from './users'
import { EmbedComponent, RedundancyService, VideoImportService, VideoOwnershipService, VideoResolver, VideoService } from './video'
import {
EmbedComponent,
RedundancyService,
VideoFileTokenService,
VideoImportService,
VideoOwnershipService,
VideoResolver,
VideoService
} from './video'
import { VideoCaptionService } from './video-caption'
import { VideoChannelService } from './video-channel'

Expand Down Expand Up @@ -185,6 +193,7 @@ import { VideoChannelService } from './video-channel'
VideoImportService,
VideoOwnershipService,
VideoService,
VideoFileTokenService,
VideoResolver,

VideoCaptionService,
Expand Down
1 change: 1 addition & 0 deletions client/src/app/shared/shared-main/video/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './embed.component'
export * from './redundancy.service'
export * from './video-details.model'
export * from './video-edit.model'
export * from './video-file-token.service'
export * from './video-import.service'
export * from './video-ownership.service'
export * from './video.model'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { catchError, map, of, tap } from 'rxjs'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor } from '@app/core'
import { VideoToken } from '@shared/models'
import { VideoService } from './video.service'

@Injectable()
export class VideoFileTokenService {

private readonly store = new Map<string, { token: string, expires: Date }>()

constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor
) {}

getVideoFileToken (videoUUID: string) {
const existing = this.store.get(videoUUID)
if (existing) return of(existing)

return this.createVideoFileToken(videoUUID)
.pipe(tap(result => this.store.set(videoUUID, { token: result.token, expires: new Date(result.expires) })))
}

private createVideoFileToken (videoUUID: string) {
return this.authHttp.post<VideoToken>(`${VideoService.BASE_VIDEO_URL}/${videoUUID}/token`, {})
.pipe(
map(({ files }) => files),
catchError(err => this.restExtractor.handleError(err))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ <h4 class="modal-title">

<ng-template ngbNavContent>
<div class="nav-content">
<my-input-text
*ngIf="!isConfidentialVideo()"
[show]="true" [readonly]="true" [withCopy]="true" [withToggle]="false" [value]="getLink()"
></my-input-text>
<my-input-text [show]="true" [readonly]="true" [withCopy]="true" [withToggle]="false" [value]="getLink()"></my-input-text>
</div>
</ng-template>
</ng-container>
Expand Down
Loading