diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-06 15:25:19 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-07 08:55:02 +0200 |
commit | a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f (patch) | |
tree | 1744c9db32b765b1511d4a69a7ba7b525dc0f619 /client/src/root-helpers/pure-auth-user.model.ts | |
parent | a02b93ce756d646a59cef57b5e4ff53c2bb30bec (diff) | |
download | PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.gz PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.zst PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.zip |
Cleanup tokens logic in embed
Diffstat (limited to 'client/src/root-helpers/pure-auth-user.model.ts')
-rw-r--r-- | client/src/root-helpers/pure-auth-user.model.ts | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/client/src/root-helpers/pure-auth-user.model.ts b/client/src/root-helpers/pure-auth-user.model.ts deleted file mode 100644 index 81226da01..000000000 --- a/client/src/root-helpers/pure-auth-user.model.ts +++ /dev/null | |||
@@ -1,123 +0,0 @@ | |||
1 | // pure version of auth-user, that doesn't import app packages | ||
2 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | ||
3 | import { | ||
4 | MyUser as ServerMyUserModel, | ||
5 | MyUserSpecialPlaylist, | ||
6 | NSFWPolicyType, | ||
7 | UserRole | ||
8 | } from '@shared/models' | ||
9 | import { UserKeys } from '@root-helpers/user-keys' | ||
10 | |||
11 | export type TokenOptions = { | ||
12 | accessToken: string | ||
13 | refreshToken: string | ||
14 | tokenType: string | ||
15 | } | ||
16 | |||
17 | // Private class only used by User | ||
18 | export class Tokens { | ||
19 | private static KEYS = { | ||
20 | ACCESS_TOKEN: 'access_token', | ||
21 | REFRESH_TOKEN: 'refresh_token', | ||
22 | TOKEN_TYPE: 'token_type' | ||
23 | } | ||
24 | |||
25 | accessToken: string | ||
26 | refreshToken: string | ||
27 | tokenType: string | ||
28 | |||
29 | static load () { | ||
30 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) | ||
31 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) | ||
32 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) | ||
33 | |||
34 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | ||
35 | return new Tokens({ | ||
36 | accessToken: accessTokenLocalStorage, | ||
37 | refreshToken: refreshTokenLocalStorage, | ||
38 | tokenType: tokenTypeLocalStorage | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | return null | ||
43 | } | ||
44 | |||
45 | static flush () { | ||
46 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) | ||
47 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | ||
48 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | ||
49 | } | ||
50 | |||
51 | constructor (hash?: TokenOptions) { | ||
52 | if (hash) { | ||
53 | this.accessToken = hash.accessToken | ||
54 | this.refreshToken = hash.refreshToken | ||
55 | |||
56 | if (hash.tokenType === 'bearer') { | ||
57 | this.tokenType = 'Bearer' | ||
58 | } else { | ||
59 | this.tokenType = hash.tokenType | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | save () { | ||
65 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | ||
66 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | ||
67 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | export class PureAuthUser { | ||
72 | tokens: Tokens | ||
73 | specialPlaylists: MyUserSpecialPlaylist[] | ||
74 | |||
75 | canSeeVideosLink = true | ||
76 | |||
77 | static load () { | ||
78 | const usernameLocalStorage = peertubeLocalStorage.getItem(UserKeys.USERNAME) | ||
79 | if (usernameLocalStorage) { | ||
80 | return new PureAuthUser( | ||
81 | { | ||
82 | id: parseInt(peertubeLocalStorage.getItem(UserKeys.ID), 10), | ||
83 | username: peertubeLocalStorage.getItem(UserKeys.USERNAME), | ||
84 | email: peertubeLocalStorage.getItem(UserKeys.EMAIL), | ||
85 | role: parseInt(peertubeLocalStorage.getItem(UserKeys.ROLE), 10) as UserRole, | ||
86 | nsfwPolicy: peertubeLocalStorage.getItem(UserKeys.NSFW_POLICY) as NSFWPolicyType, | ||
87 | webTorrentEnabled: peertubeLocalStorage.getItem(UserKeys.WEBTORRENT_ENABLED) === 'true', | ||
88 | autoPlayVideo: peertubeLocalStorage.getItem(UserKeys.AUTO_PLAY_VIDEO) === 'true', | ||
89 | videosHistoryEnabled: peertubeLocalStorage.getItem(UserKeys.VIDEOS_HISTORY_ENABLED) === 'true' | ||
90 | }, | ||
91 | Tokens.load() | ||
92 | ) | ||
93 | } | ||
94 | |||
95 | return null | ||
96 | } | ||
97 | |||
98 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { | ||
99 | this.tokens = new Tokens(hashTokens) | ||
100 | this.specialPlaylists = userHash.specialPlaylists | ||
101 | } | ||
102 | |||
103 | getAccessToken () { | ||
104 | return this.tokens.accessToken | ||
105 | } | ||
106 | |||
107 | getRefreshToken () { | ||
108 | return this.tokens.refreshToken | ||
109 | } | ||
110 | |||
111 | getTokenType () { | ||
112 | return this.tokens.tokenType | ||
113 | } | ||
114 | |||
115 | refreshTokens (accessToken: string, refreshToken: string) { | ||
116 | this.tokens.accessToken = accessToken | ||
117 | this.tokens.refreshToken = refreshToken | ||
118 | } | ||
119 | |||
120 | save () { | ||
121 | this.tokens.save() | ||
122 | } | ||
123 | } | ||