1 import { HttpStatusCode, OAuth2ErrorCode, UserRefreshToken } from '../../../../../shared/models'
2 import { OAuthUserTokens, objectToUrlEncoded } from '../../../root-helpers'
3 import { peertubeLocalStorage } from '../../../root-helpers/peertube-web-storage'
5 export class AuthHTTP {
6 private readonly LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
7 CLIENT_ID: 'client_id',
8 CLIENT_SECRET: 'client_secret'
11 private userOAuthTokens: OAuthUserTokens
13 private headers = new Headers()
16 this.userOAuthTokens = OAuthUserTokens.getUserTokens(peertubeLocalStorage)
18 if (this.userOAuthTokens) this.setHeadersFromTokens()
21 fetch (url: string, { optionalAuth, method }: { optionalAuth: boolean, method?: string }) {
22 const refreshFetchOptions = optionalAuth
23 ? { headers: this.headers }
26 return this.refreshFetch(url.toString(), { ...refreshFetchOptions, method })
29 getHeaderTokenValue () {
30 if (!this.userOAuthTokens) return null
32 return `${this.userOAuthTokens.tokenType} ${this.userOAuthTokens.accessToken}`
36 return !!this.userOAuthTokens
39 private refreshFetch (url: string, options?: RequestInit) {
40 return fetch(url, options)
41 .then((res: Response) => {
42 if (res.status !== HttpStatusCode.UNAUTHORIZED_401) return res
44 const refreshingTokenPromise = new Promise<void>((resolve, reject) => {
45 const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
46 const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
48 const headers = new Headers()
49 headers.set('Content-Type', 'application/x-www-form-urlencoded')
52 refresh_token: this.userOAuthTokens.refreshToken,
54 client_secret: clientSecret,
55 response_type: 'code',
56 grant_type: 'refresh_token'
59 fetch('/api/v1/users/token', {
62 body: objectToUrlEncoded(data)
64 if (res.status === HttpStatusCode.UNAUTHORIZED_401) return undefined
67 }).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => {
68 if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) {
69 OAuthUserTokens.flushLocalStorage(peertubeLocalStorage)
70 this.removeTokensFromHeaders()
75 this.userOAuthTokens.accessToken = obj.access_token
76 this.userOAuthTokens.refreshToken = obj.refresh_token
77 OAuthUserTokens.saveToLocalStorage(peertubeLocalStorage, this.userOAuthTokens)
79 this.setHeadersFromTokens()
82 }).catch((refreshTokenError: any) => {
83 reject(refreshTokenError)
87 return refreshingTokenPromise
89 OAuthUserTokens.flushLocalStorage(peertubeLocalStorage)
91 this.removeTokensFromHeaders()
92 }).then(() => fetch(url, {
100 private setHeadersFromTokens () {
101 this.headers.set('Authorization', this.getHeaderTokenValue())
104 private removeTokensFromHeaders () {
105 this.headers.delete('Authorization')