aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-06 15:25:19 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-08-07 08:55:02 +0200
commita4ff3100d36f2fe9a4dfc00e8487c28a94433c4f (patch)
tree1744c9db32b765b1511d4a69a7ba7b525dc0f619 /client/src/root-helpers
parenta02b93ce756d646a59cef57b5e4ff53c2bb30bec (diff)
downloadPeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.gz
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.tar.zst
PeerTube-a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f.zip
Cleanup tokens logic in embed
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/index.ts3
-rw-r--r--client/src/root-helpers/pure-auth-user.model.ts123
-rw-r--r--client/src/root-helpers/users/index.ts3
-rw-r--r--client/src/root-helpers/users/user-local-storage-keys.ts (renamed from client/src/root-helpers/user-keys.ts)2
-rw-r--r--client/src/root-helpers/users/user-local-storage-manager.ts55
-rw-r--r--client/src/root-helpers/users/user-tokens.ts61
6 files changed, 121 insertions, 126 deletions
diff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.ts
index 5ed4933f1..59468b31c 100644
--- a/client/src/root-helpers/index.ts
+++ b/client/src/root-helpers/index.ts
@@ -1,4 +1,3 @@
1export * from './users'
1export * from './peertube-web-storage' 2export * from './peertube-web-storage'
2export * from './utils' 3export * from './utils'
3export * from './user-keys'
4export * from './pure-auth-user.model'
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
2import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
3import {
4 MyUser as ServerMyUserModel,
5 MyUserSpecialPlaylist,
6 NSFWPolicyType,
7 UserRole
8} from '@shared/models'
9import { UserKeys } from '@root-helpers/user-keys'
10
11export type TokenOptions = {
12 accessToken: string
13 refreshToken: string
14 tokenType: string
15}
16
17// Private class only used by User
18export 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
71export 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}
diff --git a/client/src/root-helpers/users/index.ts b/client/src/root-helpers/users/index.ts
new file mode 100644
index 000000000..8fbaca9e3
--- /dev/null
+++ b/client/src/root-helpers/users/index.ts
@@ -0,0 +1,3 @@
1export * from './user-local-storage-keys'
2export * from './user-local-storage-manager'
3export * from './user-tokens'
diff --git a/client/src/root-helpers/user-keys.ts b/client/src/root-helpers/users/user-local-storage-keys.ts
index 897be8c43..5f915899c 100644
--- a/client/src/root-helpers/user-keys.ts
+++ b/client/src/root-helpers/users/user-local-storage-keys.ts
@@ -1,4 +1,4 @@
1export const UserKeys = { 1export const UserLocalStorageKeys = {
2 ID: 'id', 2 ID: 'id',
3 ROLE: 'role', 3 ROLE: 'role',
4 EMAIL: 'email', 4 EMAIL: 'email',
diff --git a/client/src/root-helpers/users/user-local-storage-manager.ts b/client/src/root-helpers/users/user-local-storage-manager.ts
new file mode 100644
index 000000000..c75cea127
--- /dev/null
+++ b/client/src/root-helpers/users/user-local-storage-manager.ts
@@ -0,0 +1,55 @@
1import { NSFWPolicyType, UserRole } from '@shared/models'
2import { peertubeLocalStorage } from '../peertube-web-storage'
3import { UserLocalStorageKeys } from './user-local-storage-keys'
4
5function getUserInfoFromLocalStorage () {
6 const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)
7
8 if (!usernameLocalStorage) return undefined
9
10 return {
11 id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),
12 username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),
13 email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),
14 role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
15 nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,
16 webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',
17 autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
18 videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'
19 }
20}
21
22function flushUserInfoFromLocalStorage () {
23 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)
24 peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)
25 peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)
26 peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)
27 peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)
28 peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)
29 peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
30 peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)
31}
32
33function saveUserInfoIntoLocalStorage (info: {
34 id: number
35 username: string
36 email: string
37 role: UserRole
38 nsfwPolicy: NSFWPolicyType
39 webTorrentEnabled: boolean
40 autoPlayVideo: boolean
41}) {
42 peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())
43 peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)
44 peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)
45 peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())
46 peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())
47 peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))
48 peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))
49}
50
51export {
52 getUserInfoFromLocalStorage,
53 saveUserInfoIntoLocalStorage,
54 flushUserInfoFromLocalStorage
55}
diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.ts
new file mode 100644
index 000000000..d42e1c8f3
--- /dev/null
+++ b/client/src/root-helpers/users/user-tokens.ts
@@ -0,0 +1,61 @@
1import { peertubeLocalStorage } from '../peertube-web-storage'
2
3export type TokenOptions = {
4 accessToken: string
5 refreshToken: string
6 tokenType: string
7}
8
9// Private class only used by User
10export class Tokens {
11 private static KEYS = {
12 ACCESS_TOKEN: 'access_token',
13 REFRESH_TOKEN: 'refresh_token',
14 TOKEN_TYPE: 'token_type'
15 }
16
17 accessToken: string
18 refreshToken: string
19 tokenType: string
20
21 static load () {
22 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
23 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
24 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
25
26 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
27 return new Tokens({
28 accessToken: accessTokenLocalStorage,
29 refreshToken: refreshTokenLocalStorage,
30 tokenType: tokenTypeLocalStorage
31 })
32 }
33
34 return null
35 }
36
37 static flush () {
38 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
39 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
40 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
41 }
42
43 constructor (hash?: TokenOptions) {
44 if (hash) {
45 this.accessToken = hash.accessToken
46 this.refreshToken = hash.refreshToken
47
48 if (hash.tokenType === 'bearer') {
49 this.tokenType = 'Bearer'
50 } else {
51 this.tokenType = hash.tokenType
52 }
53 }
54 }
55
56 save () {
57 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
58 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
59 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
60 }
61}