aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/auth
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-03-04 11:45:47 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-03-04 11:45:47 +0100
commite2a2d6c86c7ca39074fdff3b545947d1d58dc008 (patch)
treea3420493460f67472acceae2a5746612a30943f0 /client/src/app/shared/auth
parenta7449e74f9105839fa03f0b2e23b435f37a1fc2f (diff)
downloadPeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.tar.gz
PeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.tar.zst
PeerTube-e2a2d6c86c7ca39074fdff3b545947d1d58dc008.zip
Client: check token valitidy at startup
Diffstat (limited to 'client/src/app/shared/auth')
-rw-r--r--client/src/app/shared/auth/auth-status.model.ts4
-rw-r--r--client/src/app/shared/auth/auth-user.model.ts117
-rw-r--r--client/src/app/shared/auth/index.ts2
3 files changed, 0 insertions, 123 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 @@
1export 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 @@
1import { User } from '../users';
2
3export 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
66class 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 @@
1export * from './auth-http.service'; export * from './auth-http.service';
2export * from './auth-status.model';
3export * from './auth-user.model';