diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:24:18 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-07-20 16:25:06 +0200 |
commit | bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869 (patch) | |
tree | 66df283a1554f27b92e392fca36b8e272d7535bc /client/src/app/shared/auth/auth-http.service.ts | |
parent | 2f372a865487427ff97ad17edd0e6adfbb478c80 (diff) | |
download | PeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.tar.gz PeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.tar.zst PeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.zip |
Client: Add authHttp service that authentificates the http request and
optionally refresh the access token if needed
Diffstat (limited to 'client/src/app/shared/auth/auth-http.service.ts')
-rw-r--r-- | client/src/app/shared/auth/auth-http.service.ts | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts new file mode 100644 index 000000000..ff8099a46 --- /dev/null +++ b/client/src/app/shared/auth/auth-http.service.ts | |||
@@ -0,0 +1,77 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { | ||
3 | ConnectionBackend, | ||
4 | Headers, | ||
5 | Http, | ||
6 | Request, | ||
7 | RequestMethod, | ||
8 | RequestOptions, | ||
9 | RequestOptionsArgs, | ||
10 | Response | ||
11 | } from '@angular/http'; | ||
12 | import { Observable } from 'rxjs/Observable'; | ||
13 | |||
14 | import { AuthService } from './auth.service'; | ||
15 | |||
16 | @Injectable() | ||
17 | export class AuthHttp extends Http { | ||
18 | constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private authService: AuthService) { | ||
19 | super(backend, defaultOptions); | ||
20 | } | ||
21 | |||
22 | request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { | ||
23 | if (!options) options = {}; | ||
24 | |||
25 | options.headers = new Headers(); | ||
26 | this.setAuthorizationHeader(options.headers); | ||
27 | |||
28 | return super.request(url, options) | ||
29 | .catch((err) => { | ||
30 | if (err.status === 401) { | ||
31 | return this.handleTokenExpired(err, url, options); | ||
32 | } | ||
33 | |||
34 | return Observable.throw(err); | ||
35 | }); | ||
36 | } | ||
37 | |||
38 | delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
39 | if (!options) options = {}; | ||
40 | options.method = RequestMethod.Delete; | ||
41 | |||
42 | return this.request(url, options); | ||
43 | } | ||
44 | |||
45 | get(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
46 | if (!options) options = {}; | ||
47 | options.method = RequestMethod.Get; | ||
48 | |||
49 | return this.request(url, options); | ||
50 | } | ||
51 | |||
52 | post(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
53 | if (!options) options = {}; | ||
54 | options.method = RequestMethod.Post; | ||
55 | |||
56 | return this.request(url, options); | ||
57 | } | ||
58 | |||
59 | put(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
60 | if (!options) options = {}; | ||
61 | options.method = RequestMethod.Put; | ||
62 | |||
63 | return this.request(url, options); | ||
64 | } | ||
65 | |||
66 | private handleTokenExpired(err: Response, url: string | Request, options: RequestOptionsArgs) { | ||
67 | return this.authService.refreshAccessToken().flatMap(() => { | ||
68 | this.setAuthorizationHeader(options.headers); | ||
69 | |||
70 | return super.request(url, options); | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | private setAuthorizationHeader(headers: Headers) { | ||
75 | headers.set('Authorization', `${this.authService.getTokenType()} ${this.authService.getToken()}`); | ||
76 | } | ||
77 | } | ||