Fix refreshing oauth token

pull/603/head
Chocobozzz 2018-05-29 17:16:20 +02:00
parent f6a7c82ca5
commit a20776fcbb
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 15 additions and 8 deletions

View File

@ -1,5 +1,5 @@
import { Observable, ReplaySubject, Subject, throwError as observableThrowError } from 'rxjs' import { Observable, ReplaySubject, Subject, throwError as observableThrowError } from 'rxjs'
import { catchError, map, mergeMap, tap } from 'rxjs/operators' import { catchError, map, mergeMap, tap, share } from 'rxjs/operators'
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http' import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { Router } from '@angular/router' import { Router } from '@angular/router'
@ -12,6 +12,7 @@ import { RestExtractor } from '../../shared/rest'
import { AuthStatus } from './auth-status.model' import { AuthStatus } from './auth-status.model'
import { AuthUser } from './auth-user.model' import { AuthUser } from './auth-user.model'
import { objectToUrlEncoded } from '@app/shared/misc/utils' import { objectToUrlEncoded } from '@app/shared/misc/utils'
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
interface UserLoginWithUsername extends UserLogin { interface UserLoginWithUsername extends UserLogin {
access_token: string access_token: string
@ -27,12 +28,16 @@ export class AuthService {
private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local' private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local'
private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token' private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token'
private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me' private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me'
private static LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
CLIENT_ID: 'client_id',
CLIENT_SECRET: 'client_secret'
}
loginChangedSource: Observable<AuthStatus> loginChangedSource: Observable<AuthStatus>
userInformationLoaded = new ReplaySubject<boolean>(1) userInformationLoaded = new ReplaySubject<boolean>(1)
private clientId: string private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
private clientSecret: string private clientSecret: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
private loginChanged: Subject<AuthStatus> private loginChanged: Subject<AuthStatus>
private user: AuthUser = null private user: AuthUser = null
private refreshingTokenObservable: Observable<any> private refreshingTokenObservable: Observable<any>
@ -52,13 +57,16 @@ export class AuthService {
loadClientCredentials () { loadClientCredentials () {
// Fetch the client_id/client_secret // Fetch the client_id/client_secret
// FIXME: save in local storage?
this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL)
.pipe(catchError(res => this.restExtractor.handleError(res))) .pipe(catchError(res => this.restExtractor.handleError(res)))
.subscribe( .subscribe(
res => { res => {
this.clientId = res.client_id this.clientId = res.client_id
this.clientSecret = res.client_secret this.clientSecret = res.client_secret
peertubeLocalStorage.setItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID, this.clientId)
peertubeLocalStorage.setItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET, this.clientSecret)
console.log('Client credentials loaded.') console.log('Client credentials loaded.')
}, },
@ -174,7 +182,8 @@ export class AuthService {
return observableThrowError({ return observableThrowError({
error: 'You need to reconnect.' error: 'You need to reconnect.'
}) })
}) }),
share()
) )
return this.refreshingTokenObservable return this.refreshingTokenObservable

View File

@ -7,9 +7,7 @@
"resources": { "resources": {
"files": [ "files": [
"/index.html", "/index.html",
"/client/assets/images/favicon.png" "/client/assets/images/favicon.png",
],
"versionedFiles": [
"/client/*.bundle.css", "/client/*.bundle.css",
"/client/*.bundle.js", "/client/*.bundle.js",
"/client/*.chunk.js" "/client/*.chunk.js"