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