diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-03-04 11:45:47 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-03-04 11:45:47 +0100 |
commit | e2a2d6c86c7ca39074fdff3b545947d1d58dc008 (patch) | |
tree | a3420493460f67472acceae2a5746612a30943f0 /client/src/app/shared | |
parent | a7449e74f9105839fa03f0b2e23b435f37a1fc2f (diff) | |
download | PeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.tar.gz PeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.tar.zst PeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.zip |
Client: check token valitidy at startup
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/auth/auth-status.model.ts | 4 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth-user.model.ts | 117 | ||||
-rw-r--r-- | client/src/app/shared/auth/index.ts | 2 | ||||
-rw-r--r-- | client/src/app/shared/shared.module.ts | 4 | ||||
-rw-r--r-- | client/src/app/shared/users/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/users/user.service.ts | 36 |
6 files changed, 40 insertions, 124 deletions
diff --git a/client/src/app/shared/auth/auth-status.model.ts b/client/src/app/shared/auth/auth-status.model.ts deleted file mode 100644 index f646bd4cf..000000000 --- a/client/src/app/shared/auth/auth-status.model.ts +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | export enum AuthStatus { | ||
2 | LoggedIn, | ||
3 | LoggedOut | ||
4 | } | ||
diff --git a/client/src/app/shared/auth/auth-user.model.ts b/client/src/app/shared/auth/auth-user.model.ts deleted file mode 100644 index f560351f4..000000000 --- a/client/src/app/shared/auth/auth-user.model.ts +++ /dev/null | |||
@@ -1,117 +0,0 @@ | |||
1 | import { User } from '../users'; | ||
2 | |||
3 | export class AuthUser extends User { | ||
4 | private static KEYS = { | ||
5 | ID: 'id', | ||
6 | ROLE: 'role', | ||
7 | USERNAME: 'username' | ||
8 | }; | ||
9 | |||
10 | tokens: Tokens; | ||
11 | |||
12 | static load() { | ||
13 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | ||
14 | if (usernameLocalStorage) { | ||
15 | return new AuthUser( | ||
16 | { | ||
17 | id: parseInt(localStorage.getItem(this.KEYS.ID)), | ||
18 | username: localStorage.getItem(this.KEYS.USERNAME), | ||
19 | role: localStorage.getItem(this.KEYS.ROLE) | ||
20 | }, | ||
21 | Tokens.load() | ||
22 | ); | ||
23 | } | ||
24 | |||
25 | return null; | ||
26 | } | ||
27 | |||
28 | static flush() { | ||
29 | localStorage.removeItem(this.KEYS.USERNAME); | ||
30 | localStorage.removeItem(this.KEYS.ID); | ||
31 | localStorage.removeItem(this.KEYS.ROLE); | ||
32 | Tokens.flush(); | ||
33 | } | ||
34 | |||
35 | constructor(userHash: { id: number, username: string, role: string }, hashTokens: any) { | ||
36 | super(userHash); | ||
37 | this.tokens = new Tokens(hashTokens); | ||
38 | } | ||
39 | |||
40 | getAccessToken() { | ||
41 | return this.tokens.access_token; | ||
42 | } | ||
43 | |||
44 | getRefreshToken() { | ||
45 | return this.tokens.refresh_token; | ||
46 | } | ||
47 | |||
48 | getTokenType() { | ||
49 | return this.tokens.token_type; | ||
50 | } | ||
51 | |||
52 | refreshTokens(access_token: string, refresh_token: string) { | ||
53 | this.tokens.access_token = access_token; | ||
54 | this.tokens.refresh_token = refresh_token; | ||
55 | } | ||
56 | |||
57 | save() { | ||
58 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()); | ||
59 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); | ||
60 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role); | ||
61 | this.tokens.save(); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | // Private class only used by User | ||
66 | class Tokens { | ||
67 | private static KEYS = { | ||
68 | ACCESS_TOKEN: 'access_token', | ||
69 | REFRESH_TOKEN: 'refresh_token', | ||
70 | TOKEN_TYPE: 'token_type', | ||
71 | }; | ||
72 | |||
73 | access_token: string; | ||
74 | refresh_token: string; | ||
75 | token_type: string; | ||
76 | |||
77 | static load() { | ||
78 | const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN); | ||
79 | const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN); | ||
80 | const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE); | ||
81 | |||
82 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
83 | return new Tokens({ | ||
84 | access_token: accessTokenLocalStorage, | ||
85 | refresh_token: refreshTokenLocalStorage, | ||
86 | token_type: tokenTypeLocalStorage | ||
87 | }); | ||
88 | } | ||
89 | |||
90 | return null; | ||
91 | } | ||
92 | |||
93 | static flush() { | ||
94 | localStorage.removeItem(this.KEYS.ACCESS_TOKEN); | ||
95 | localStorage.removeItem(this.KEYS.REFRESH_TOKEN); | ||
96 | localStorage.removeItem(this.KEYS.TOKEN_TYPE); | ||
97 | } | ||
98 | |||
99 | constructor(hash?: any) { | ||
100 | if (hash) { | ||
101 | this.access_token = hash.access_token; | ||
102 | this.refresh_token = hash.refresh_token; | ||
103 | |||
104 | if (hash.token_type === 'bearer') { | ||
105 | this.token_type = 'Bearer'; | ||
106 | } else { | ||
107 | this.token_type = hash.token_type; | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | save() { | ||
113 | localStorage.setItem('access_token', this.access_token); | ||
114 | localStorage.setItem('refresh_token', this.refresh_token); | ||
115 | localStorage.setItem('token_type', this.token_type); | ||
116 | } | ||
117 | } | ||
diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts index ce0bd8adf..c488aed69 100644 --- a/client/src/app/shared/auth/index.ts +++ b/client/src/app/shared/auth/index.ts | |||
@@ -1,3 +1 @@ | |||
1 | export * from './auth-http.service'; | export * from './auth-http.service'; | |
2 | export * from './auth-status.model'; | ||
3 | export * from './auth-user.model'; | ||
diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts index 0f57ef078..84cc86c64 100644 --- a/client/src/app/shared/shared.module.ts +++ b/client/src/app/shared/shared.module.ts | |||
@@ -16,6 +16,7 @@ import { Ng2SmartTableModule } from 'ng2-smart-table'; | |||
16 | import { AUTH_HTTP_PROVIDERS } from './auth'; | 16 | import { AUTH_HTTP_PROVIDERS } from './auth'; |
17 | import { RestExtractor, RestService } from './rest'; | 17 | import { RestExtractor, RestService } from './rest'; |
18 | import { SearchComponent, SearchService } from './search'; | 18 | import { SearchComponent, SearchService } from './search'; |
19 | import { UserService } from './users'; | ||
19 | import { VideoAbuseService } from './video-abuse'; | 20 | import { VideoAbuseService } from './video-abuse'; |
20 | 21 | ||
21 | @NgModule({ | 22 | @NgModule({ |
@@ -65,7 +66,8 @@ import { VideoAbuseService } from './video-abuse'; | |||
65 | RestExtractor, | 66 | RestExtractor, |
66 | RestService, | 67 | RestService, |
67 | SearchService, | 68 | SearchService, |
68 | VideoAbuseService | 69 | VideoAbuseService, |
70 | UserService | ||
69 | ] | 71 | ] |
70 | }) | 72 | }) |
71 | export class SharedModule { } | 73 | export class SharedModule { } |
diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts index 5a670ce8f..ff009e89b 100644 --- a/client/src/app/shared/users/index.ts +++ b/client/src/app/shared/users/index.ts | |||
@@ -1 +1,2 @@ | |||
1 | export * from './user.model'; | 1 | export * from './user.model'; |
2 | export * from './user.service'; | ||
diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts new file mode 100644 index 000000000..4cf100f0d --- /dev/null +++ b/client/src/app/shared/users/user.service.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import 'rxjs/add/operator/catch'; | ||
3 | import 'rxjs/add/operator/map'; | ||
4 | |||
5 | import { AuthService } from '../../core'; | ||
6 | import { AuthHttp } from '../auth'; | ||
7 | import { RestExtractor } from '../rest'; | ||
8 | |||
9 | @Injectable() | ||
10 | export class UserService { | ||
11 | private static BASE_USERS_URL = '/api/v1/users/'; | ||
12 | |||
13 | constructor( | ||
14 | private authHttp: AuthHttp, | ||
15 | private authService: AuthService, | ||
16 | private restExtractor: RestExtractor | ||
17 | ) {} | ||
18 | |||
19 | checkTokenValidity() { | ||
20 | const url = UserService.BASE_USERS_URL + 'me'; | ||
21 | |||
22 | // AuthHttp will redirect us to the login page if the oken is not valid anymore | ||
23 | this.authHttp.get(url).subscribe(() => { ; }); | ||
24 | } | ||
25 | |||
26 | changePassword(newPassword: string) { | ||
27 | const url = UserService.BASE_USERS_URL + this.authService.getUser().id; | ||
28 | const body = { | ||
29 | password: newPassword | ||
30 | }; | ||
31 | |||
32 | return this.authHttp.put(url, body) | ||
33 | .map(this.restExtractor.extractDataBool) | ||
34 | .catch((res) => this.restExtractor.handleError(res)); | ||
35 | } | ||
36 | } | ||