diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-11-09 16:25:27 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-25 11:07:56 +0100 |
commit | 5beb89f223539f1e415a976ff104f772526b4d20 (patch) | |
tree | 2164677d16a2965d63499e249aa75ab0e06e3a6c /client/src/app/core | |
parent | afff310e50f2fa8419bb4242470cbde46ab54463 (diff) | |
download | PeerTube-5beb89f223539f1e415a976ff104f772526b4d20.tar.gz PeerTube-5beb89f223539f1e415a976ff104f772526b4d20.tar.zst PeerTube-5beb89f223539f1e415a976ff104f772526b4d20.zip |
refactor scoped token service
Diffstat (limited to 'client/src/app/core')
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 45 | ||||
-rw-r--r-- | client/src/app/core/core.module.ts | 2 | ||||
-rw-r--r-- | client/src/app/core/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/core/scoped-tokens/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/core/scoped-tokens/scoped-tokens.service.ts | 33 |
5 files changed, 37 insertions, 45 deletions
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 224f35f82..fd6062d3f 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -11,7 +11,6 @@ import { environment } from '../../../environments/environment' | |||
11 | import { RestExtractor } from '../rest/rest-extractor.service' | 11 | import { RestExtractor } from '../rest/rest-extractor.service' |
12 | import { AuthStatus } from './auth-status.model' | 12 | import { AuthStatus } from './auth-status.model' |
13 | import { AuthUser } from './auth-user.model' | 13 | import { AuthUser } from './auth-user.model' |
14 | import { ScopedTokenType, ScopedToken } from '@shared/models/users/user-scoped-token' | ||
15 | 14 | ||
16 | interface UserLoginWithUsername extends UserLogin { | 15 | interface UserLoginWithUsername extends UserLogin { |
17 | access_token: string | 16 | access_token: string |
@@ -27,7 +26,6 @@ export class AuthService { | |||
27 | private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local' | 26 | private static BASE_CLIENT_URL = environment.apiUrl + '/api/v1/oauth-clients/local' |
28 | private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token' | 27 | private static BASE_TOKEN_URL = environment.apiUrl + '/api/v1/users/token' |
29 | private static BASE_REVOKE_TOKEN_URL = environment.apiUrl + '/api/v1/users/revoke-token' | 28 | private static BASE_REVOKE_TOKEN_URL = environment.apiUrl + '/api/v1/users/revoke-token' |
30 | private static BASE_SCOPED_TOKENS_URL = environment.apiUrl + '/api/v1/users/scoped-tokens' | ||
31 | private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me' | 29 | private static BASE_USER_INFORMATION_URL = environment.apiUrl + '/api/v1/users/me' |
32 | private static LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { | 30 | private static LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { |
33 | CLIENT_ID: 'client_id', | 31 | CLIENT_ID: 'client_id', |
@@ -43,7 +41,6 @@ export class AuthService { | |||
43 | private loginChanged: Subject<AuthStatus> | 41 | private loginChanged: Subject<AuthStatus> |
44 | private user: AuthUser = null | 42 | private user: AuthUser = null |
45 | private refreshingTokenObservable: Observable<any> | 43 | private refreshingTokenObservable: Observable<any> |
46 | private scopedTokens: ScopedToken | ||
47 | 44 | ||
48 | constructor ( | 45 | constructor ( |
49 | private http: HttpClient, | 46 | private http: HttpClient, |
@@ -247,48 +244,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular | |||
247 | ) | 244 | ) |
248 | } | 245 | } |
249 | 246 | ||
250 | getScopedTokens (): Promise<ScopedToken> { | ||
251 | return new Promise((res, rej) => { | ||
252 | if (this.scopedTokens) return res(this.scopedTokens) | ||
253 | |||
254 | const authHeaderValue = this.getRequestHeaderValue() | ||
255 | const headers = new HttpHeaders().set('Authorization', authHeaderValue) | ||
256 | |||
257 | this.http.get<ScopedToken>(AuthService.BASE_SCOPED_TOKENS_URL, { headers }) | ||
258 | .subscribe( | ||
259 | scopedTokens => { | ||
260 | this.scopedTokens = scopedTokens | ||
261 | res(this.scopedTokens) | ||
262 | }, | ||
263 | |||
264 | err => { | ||
265 | console.error(err) | ||
266 | rej(err) | ||
267 | } | ||
268 | ) | ||
269 | }) | ||
270 | } | ||
271 | |||
272 | renewScopedTokens (): Promise<ScopedToken> { | ||
273 | return new Promise((res, rej) => { | ||
274 | const authHeaderValue = this.getRequestHeaderValue() | ||
275 | const headers = new HttpHeaders().set('Authorization', authHeaderValue) | ||
276 | |||
277 | this.http.post<ScopedToken>(AuthService.BASE_SCOPED_TOKENS_URL, {}, { headers }) | ||
278 | .subscribe( | ||
279 | scopedTokens => { | ||
280 | this.scopedTokens = scopedTokens | ||
281 | res(this.scopedTokens) | ||
282 | }, | ||
283 | |||
284 | err => { | ||
285 | console.error(err) | ||
286 | rej(err) | ||
287 | } | ||
288 | ) | ||
289 | }) | ||
290 | } | ||
291 | |||
292 | private mergeUserInformation (obj: UserLoginWithUsername): Observable<UserLoginWithUserInformation> { | 247 | private mergeUserInformation (obj: UserLoginWithUsername): Observable<UserLoginWithUserInformation> { |
293 | // User is not loaded yet, set manually auth header | 248 | // User is not loaded yet, set manually auth header |
294 | const headers = new HttpHeaders().set('Authorization', `${obj.token_type} ${obj.access_token}`) | 249 | const headers = new HttpHeaders().set('Authorization', `${obj.token_type} ${obj.access_token}`) |
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index 6c0a2245d..f51f1920d 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts | |||
@@ -12,6 +12,7 @@ import { LoadingBarModule } from '@ngx-loading-bar/core' | |||
12 | import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client' | 12 | import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client' |
13 | import { LoadingBarRouterModule } from '@ngx-loading-bar/router' | 13 | import { LoadingBarRouterModule } from '@ngx-loading-bar/router' |
14 | import { AuthService } from './auth' | 14 | import { AuthService } from './auth' |
15 | import { ScopedTokensService } from './scoped-tokens' | ||
15 | import { ConfirmService } from './confirm' | 16 | import { ConfirmService } from './confirm' |
16 | import { CheatSheetComponent } from './hotkeys' | 17 | import { CheatSheetComponent } from './hotkeys' |
17 | import { MenuService } from './menu' | 18 | import { MenuService } from './menu' |
@@ -57,6 +58,7 @@ import { LocalStorageService, ScreenService, SessionStorageService } from './wra | |||
57 | 58 | ||
58 | providers: [ | 59 | providers: [ |
59 | AuthService, | 60 | AuthService, |
61 | ScopedTokensService, | ||
60 | ConfirmService, | 62 | ConfirmService, |
61 | ServerService, | 63 | ServerService, |
62 | ThemeService, | 64 | ThemeService, |
diff --git a/client/src/app/core/index.ts b/client/src/app/core/index.ts index a0c34543d..9245ff6fc 100644 --- a/client/src/app/core/index.ts +++ b/client/src/app/core/index.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | export * from './auth' | 1 | export * from './auth' |
2 | export * from './scoped-tokens' | ||
2 | export * from './confirm' | 3 | export * from './confirm' |
3 | export * from './hotkeys' | 4 | export * from './hotkeys' |
4 | export * from './menu' | 5 | export * from './menu' |
diff --git a/client/src/app/core/scoped-tokens/index.ts b/client/src/app/core/scoped-tokens/index.ts new file mode 100644 index 000000000..c9a48ffcd --- /dev/null +++ b/client/src/app/core/scoped-tokens/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './scoped-tokens.service' | |||
diff --git a/client/src/app/core/scoped-tokens/scoped-tokens.service.ts b/client/src/app/core/scoped-tokens/scoped-tokens.service.ts new file mode 100644 index 000000000..8e3697c31 --- /dev/null +++ b/client/src/app/core/scoped-tokens/scoped-tokens.service.ts | |||
@@ -0,0 +1,33 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { HttpClient } from '@angular/common/http' | ||
3 | import { environment } from '../../../environments/environment' | ||
4 | import { AuthService } from '../auth' | ||
5 | import { ScopedToken } from '@shared/models/users/user-scoped-token' | ||
6 | import { catchError } from 'rxjs/operators' | ||
7 | import { RestExtractor } from '../rest' | ||
8 | |||
9 | @Injectable() | ||
10 | export class ScopedTokensService { | ||
11 | private static BASE_SCOPED_TOKENS_URL = environment.apiUrl + '/api/v1/users/scoped-tokens' | ||
12 | |||
13 | constructor ( | ||
14 | private authHttp: HttpClient, | ||
15 | private restExtractor: RestExtractor | ||
16 | ) {} | ||
17 | |||
18 | getScopedTokens () { | ||
19 | return this.authHttp | ||
20 | .get<ScopedToken>(ScopedTokensService.BASE_SCOPED_TOKENS_URL) | ||
21 | .pipe( | ||
22 | catchError(res => this.restExtractor.handleError(res)) | ||
23 | ) | ||
24 | } | ||
25 | |||
26 | renewScopedTokens () { | ||
27 | return this.authHttp | ||
28 | .post<ScopedToken>(ScopedTokensService.BASE_SCOPED_TOKENS_URL, {}) | ||
29 | .pipe( | ||
30 | catchError(res => this.restExtractor.handleError(res)) | ||
31 | ) | ||
32 | } | ||
33 | } | ||