]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/auth/auth-user.model.ts
Proxify local storage and handle if it is unavailable
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth-user.model.ts
1 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
2 import { UserRight } from '../../../../../shared/models/users/user-right.enum'
3 // Do not use the barrel (dependency loop)
4 import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role'
5 import { User, UserConstructorHash } from '../../shared/users/user.model'
6
7 export type TokenOptions = {
8 accessToken: string
9 refreshToken: string
10 tokenType: string
11 }
12
13 // Private class only used by User
14 class Tokens {
15 private static KEYS = {
16 ACCESS_TOKEN: 'access_token',
17 REFRESH_TOKEN: 'refresh_token',
18 TOKEN_TYPE: 'token_type'
19 }
20
21 accessToken: string
22 refreshToken: string
23 tokenType: string
24
25 static load () {
26 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
27 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
28 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
29
30 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
31 return new Tokens({
32 accessToken: accessTokenLocalStorage,
33 refreshToken: refreshTokenLocalStorage,
34 tokenType: tokenTypeLocalStorage
35 })
36 }
37
38 return null
39 }
40
41 static flush () {
42 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
43 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
44 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
45 }
46
47 constructor (hash?: TokenOptions) {
48 if (hash) {
49 this.accessToken = hash.accessToken
50 this.refreshToken = hash.refreshToken
51
52 if (hash.tokenType === 'bearer') {
53 this.tokenType = 'Bearer'
54 } else {
55 this.tokenType = hash.tokenType
56 }
57 }
58 }
59
60 save () {
61 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
62 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
63 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
64 }
65 }
66
67 export class AuthUser extends User {
68 private static KEYS = {
69 ID: 'id',
70 ROLE: 'role',
71 EMAIL: 'email',
72 USERNAME: 'username',
73 DISPLAY_NSFW: 'display_nsfw',
74 AUTO_PLAY_VIDEO: 'auto_play_video'
75 }
76
77 tokens: Tokens
78
79 static load () {
80 const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
81 if (usernameLocalStorage) {
82 return new AuthUser(
83 {
84 id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10),
85 username: peertubeLocalStorage.getItem(this.KEYS.USERNAME),
86 email: peertubeLocalStorage.getItem(this.KEYS.EMAIL),
87 role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole,
88 displayNSFW: peertubeLocalStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true',
89 autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true'
90 },
91 Tokens.load()
92 )
93 }
94
95 return null
96 }
97
98 static flush () {
99 peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
100 peertubeLocalStorage.removeItem(this.KEYS.ID)
101 peertubeLocalStorage.removeItem(this.KEYS.ROLE)
102 peertubeLocalStorage.removeItem(this.KEYS.DISPLAY_NSFW)
103 peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO)
104 peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
105 Tokens.flush()
106 }
107
108 constructor (userHash: UserConstructorHash, hashTokens: TokenOptions) {
109 super(userHash)
110 this.tokens = new Tokens(hashTokens)
111 }
112
113 getAccessToken () {
114 return this.tokens.accessToken
115 }
116
117 getRefreshToken () {
118 return this.tokens.refreshToken
119 }
120
121 getTokenType () {
122 return this.tokens.tokenType
123 }
124
125 refreshTokens (accessToken: string, refreshToken: string) {
126 this.tokens.accessToken = accessToken
127 this.tokens.refreshToken = refreshToken
128 }
129
130 hasRight (right: UserRight) {
131 return hasUserRight(this.role, right)
132 }
133
134 save () {
135 peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
136 peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
137 peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
138 peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString())
139 peertubeLocalStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW))
140 peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
141 this.tokens.save()
142 }
143 }