diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-08-03 18:06:49 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-06 15:08:58 +0200 |
commit | 4504f09f6e85f09b0489debb547a17209d7176ea (patch) | |
tree | 1e2fdcdd3a0982f6e873205f69bdf90de7f4a883 /client/src/standalone/videos | |
parent | 71ab65d02f359000f9ca6a00f163d66d56a33955 (diff) | |
download | PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.gz PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.zst PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.zip |
deal with refresh token in embed
Diffstat (limited to 'client/src/standalone/videos')
-rw-r--r-- | client/src/standalone/videos/embed.ts | 67 |
1 files changed, 61 insertions, 6 deletions
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index dca23ca7e..5bf38a04c 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts | |||
@@ -4,7 +4,8 @@ import { | |||
4 | peertubeTranslate, | 4 | peertubeTranslate, |
5 | ResultList, | 5 | ResultList, |
6 | ServerConfig, | 6 | ServerConfig, |
7 | VideoDetails | 7 | VideoDetails, |
8 | UserRefreshToken | ||
8 | } from '../../../../shared' | 9 | } from '../../../../shared' |
9 | import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model' | 10 | import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model' |
10 | import { | 11 | import { |
@@ -17,7 +18,7 @@ import { PeerTubeEmbedApi } from './embed-api' | |||
17 | import { TranslationsManager } from '../../assets/player/translations-manager' | 18 | import { TranslationsManager } from '../../assets/player/translations-manager' |
18 | import videojs from 'video.js' | 19 | import videojs from 'video.js' |
19 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' | 20 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' |
20 | import { AuthUser } from '@app/core/auth/auth-user.model' | 21 | import { PureAuthUser, objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' |
21 | 22 | ||
22 | type Translations = { [ id: string ]: string } | 23 | type Translations = { [ id: string ]: string } |
23 | 24 | ||
@@ -43,8 +44,12 @@ export class PeerTubeEmbed { | |||
43 | mode: PlayerMode | 44 | mode: PlayerMode |
44 | scope = 'peertube' | 45 | scope = 'peertube' |
45 | 46 | ||
46 | user: AuthUser | 47 | user: PureAuthUser |
47 | headers = new Headers() | 48 | headers = new Headers() |
49 | LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { | ||
50 | CLIENT_ID: 'client_id', | ||
51 | CLIENT_SECRET: 'client_secret' | ||
52 | } | ||
48 | 53 | ||
49 | static async main () { | 54 | static async main () { |
50 | const videoContainerId = 'video-container' | 55 | const videoContainerId = 'video-container' |
@@ -60,12 +65,62 @@ export class PeerTubeEmbed { | |||
60 | return window.location.origin + '/api/v1/videos/' + id | 65 | return window.location.origin + '/api/v1/videos/' + id |
61 | } | 66 | } |
62 | 67 | ||
68 | refreshFetch (url: string, options?: Object) { | ||
69 | return fetch(url, options) | ||
70 | .then((res: Response) => { | ||
71 | if (res.status !== 401) return res | ||
72 | |||
73 | // 401 unauthorized is not catch-ed, but then-ed | ||
74 | const error = res | ||
75 | |||
76 | const refreshingTokenPromise = new Promise((resolve, reject) => { | ||
77 | const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) | ||
78 | const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) | ||
79 | const headers = new Headers() | ||
80 | headers.set('Content-Type', 'application/x-www-form-urlencoded') | ||
81 | const data = { | ||
82 | refresh_token: this.user.getRefreshToken(), | ||
83 | client_id: clientId, | ||
84 | client_secret: clientSecret, | ||
85 | response_type: 'code', | ||
86 | grant_type: 'refresh_token' | ||
87 | } | ||
88 | |||
89 | fetch('/api/v1/users/token', { | ||
90 | headers, | ||
91 | method: 'POST', | ||
92 | body: objectToUrlEncoded(data) | ||
93 | }) | ||
94 | .then(res => res.json()) | ||
95 | .then((obj: UserRefreshToken) => { | ||
96 | this.user.refreshTokens(obj.access_token, obj.refresh_token) | ||
97 | this.user.save() | ||
98 | this.headers.set('Authorization', `${this.user.getTokenType()} ${this.user.getAccessToken()}`) | ||
99 | resolve() | ||
100 | }) | ||
101 | .catch((refreshTokenError: any) => { | ||
102 | reject(refreshTokenError) | ||
103 | }) | ||
104 | }) | ||
105 | |||
106 | return refreshingTokenPromise | ||
107 | .catch(() => { | ||
108 | // If refreshing fails, continue with original error | ||
109 | throw error | ||
110 | }) | ||
111 | .then(() => fetch(url, { | ||
112 | ...options, | ||
113 | headers: this.headers | ||
114 | })) | ||
115 | }) | ||
116 | } | ||
117 | |||
63 | loadVideoInfo (videoId: string): Promise<Response> { | 118 | loadVideoInfo (videoId: string): Promise<Response> { |
64 | return fetch(this.getVideoUrl(videoId), { headers: this.headers }) | 119 | return this.refreshFetch(this.getVideoUrl(videoId), { headers: this.headers }) |
65 | } | 120 | } |
66 | 121 | ||
67 | loadVideoCaptions (videoId: string): Promise<Response> { | 122 | loadVideoCaptions (videoId: string): Promise<Response> { |
68 | return fetch(this.getVideoUrl(videoId) + '/captions', { headers: this.headers }) | 123 | return fetch(this.getVideoUrl(videoId) + '/captions') |
69 | } | 124 | } |
70 | 125 | ||
71 | loadConfig (): Promise<Response> { | 126 | loadConfig (): Promise<Response> { |
@@ -115,7 +170,7 @@ export class PeerTubeEmbed { | |||
115 | 170 | ||
116 | async init () { | 171 | async init () { |
117 | try { | 172 | try { |
118 | this.user = AuthUser.load() | 173 | this.user = PureAuthUser.load() |
119 | await this.initCore() | 174 | await this.initCore() |
120 | } catch (e) { | 175 | } catch (e) { |
121 | console.error(e) | 176 | console.error(e) |