]>
Commit | Line | Data |
---|---|---|
1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' | |
2 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | |
3 | import { User as ServerUserModel } from '../../../../../shared/models/users/user.model' | |
4 | // Do not use the barrel (dependency loop) | |
5 | import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' | |
6 | import { User } from '../../shared/users/user.model' | |
7 | import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' | |
8 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
9 | ||
10 | export type TokenOptions = { | |
11 | accessToken: string | |
12 | refreshToken: string | |
13 | tokenType: string | |
14 | } | |
15 | ||
16 | // Private class only used by User | |
17 | class Tokens { | |
18 | private static KEYS = { | |
19 | ACCESS_TOKEN: 'access_token', | |
20 | REFRESH_TOKEN: 'refresh_token', | |
21 | TOKEN_TYPE: 'token_type' | |
22 | } | |
23 | ||
24 | accessToken: string | |
25 | refreshToken: string | |
26 | tokenType: string | |
27 | ||
28 | static load () { | |
29 | const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN) | |
30 | const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN) | |
31 | const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE) | |
32 | ||
33 | if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) { | |
34 | return new Tokens({ | |
35 | accessToken: accessTokenLocalStorage, | |
36 | refreshToken: refreshTokenLocalStorage, | |
37 | tokenType: tokenTypeLocalStorage | |
38 | }) | |
39 | } | |
40 | ||
41 | return null | |
42 | } | |
43 | ||
44 | static flush () { | |
45 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) | |
46 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | |
47 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | |
48 | } | |
49 | ||
50 | constructor (hash?: TokenOptions) { | |
51 | if (hash) { | |
52 | this.accessToken = hash.accessToken | |
53 | this.refreshToken = hash.refreshToken | |
54 | ||
55 | if (hash.tokenType === 'bearer') { | |
56 | this.tokenType = 'Bearer' | |
57 | } else { | |
58 | this.tokenType = hash.tokenType | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | save () { | |
64 | peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken) | |
65 | peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken) | |
66 | peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType) | |
67 | } | |
68 | } | |
69 | ||
70 | export class AuthUser extends User { | |
71 | private static KEYS = { | |
72 | ID: 'id', | |
73 | ROLE: 'role', | |
74 | EMAIL: 'email', | |
75 | VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', | |
76 | USERNAME: 'username', | |
77 | NSFW_POLICY: 'nsfw_policy', | |
78 | WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', | |
79 | AUTO_PLAY_VIDEO: 'auto_play_video' | |
80 | } | |
81 | ||
82 | tokens: Tokens | |
83 | specialPlaylists: Partial<VideoPlaylist>[] | |
84 | ||
85 | static load () { | |
86 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) | |
87 | if (usernameLocalStorage) { | |
88 | return new AuthUser( | |
89 | { | |
90 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), | |
91 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), | |
92 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), | |
93 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | |
94 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, | |
95 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', | |
96 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', | |
97 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | |
98 | }, | |
99 | Tokens.load() | |
100 | ) | |
101 | } | |
102 | ||
103 | return null | |
104 | } | |
105 | ||
106 | static flush () { | |
107 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) | |
108 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
109 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
110 | peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) | |
111 | peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED) | |
112 | peertubeLocalStorage.removeItem(this.KEYS.VIDEOS_HISTORY_ENABLED) | |
113 | peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) | |
114 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) | |
115 | Tokens.flush() | |
116 | } | |
117 | ||
118 | constructor (userHash: Partial<ServerUserModel>, hashTokens: TokenOptions) { | |
119 | super(userHash) | |
120 | this.tokens = new Tokens(hashTokens) | |
121 | } | |
122 | ||
123 | getAccessToken () { | |
124 | return this.tokens.accessToken | |
125 | } | |
126 | ||
127 | getRefreshToken () { | |
128 | return this.tokens.refreshToken | |
129 | } | |
130 | ||
131 | getTokenType () { | |
132 | return this.tokens.tokenType | |
133 | } | |
134 | ||
135 | refreshTokens (accessToken: string, refreshToken: string) { | |
136 | this.tokens.accessToken = accessToken | |
137 | this.tokens.refreshToken = refreshToken | |
138 | } | |
139 | ||
140 | hasRight (right: UserRight) { | |
141 | return hasUserRight(this.role, right) | |
142 | } | |
143 | ||
144 | canManage (user: ServerUserModel) { | |
145 | const myRole = this.role | |
146 | ||
147 | if (myRole === UserRole.ADMINISTRATOR) return true | |
148 | ||
149 | // I'm a moderator: I can only manage users | |
150 | return user.role === UserRole.USER | |
151 | } | |
152 | ||
153 | save () { | |
154 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | |
155 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
156 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
157 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | |
158 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) | |
159 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) | |
160 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) | |
161 | this.tokens.save() | |
162 | } | |
163 | } |