aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/pure-auth-user.model.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-08-03 18:06:49 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-08-06 15:08:58 +0200
commit4504f09f6e85f09b0489debb547a17209d7176ea (patch)
tree1e2fdcdd3a0982f6e873205f69bdf90de7f4a883 /client/src/root-helpers/pure-auth-user.model.ts
parent71ab65d02f359000f9ca6a00f163d66d56a33955 (diff)
downloadPeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.gz
PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.zst
PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.zip
deal with refresh token 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.ts123
1 files changed, 123 insertions, 0 deletions
diff --git a/client/src/root-helpers/pure-auth-user.model.ts b/client/src/root-helpers/pure-auth-user.model.ts
new file mode 100644
index 000000000..81226da01
--- /dev/null
+++ b/client/src/root-helpers/pure-auth-user.model.ts
@@ -0,0 +1,123 @@
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}