]>
Commit | Line | Data |
---|---|---|
0bd78bf3 C |
1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
2 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | |
276d9652 | 3 | import { User as ServerUserModel } from '../../../../../shared/models/users/user.model' |
e2a2d6c8 | 4 | // Do not use the barrel (dependency loop) |
954605a8 | 5 | import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' |
276d9652 | 6 | import { User } from '../../shared/users/user.model' |
0883b324 | 7 | import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' |
df98563e C |
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 () { | |
0bd78bf3 C |
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) | |
df98563e C |
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 () { | |
0bd78bf3 C |
44 | peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN) |
45 | peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN) | |
46 | peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE) | |
df98563e C |
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 () { | |
0bd78bf3 C |
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) | |
df98563e C |
66 | } |
67 | } | |
7da18e44 C |
68 | |
69 | export class AuthUser extends User { | |
bd5c83a8 | 70 | private static KEYS = { |
629d8d6f C |
71 | ID: 'id', |
72 | ROLE: 'role', | |
66dd264f | 73 | EMAIL: 'email', |
276d9652 | 74 | VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', |
92fb909c | 75 | USERNAME: 'username', |
2243730c | 76 | NSFW_POLICY: 'nsfw_policy', |
ed638e53 | 77 | WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', |
7efe153b | 78 | AUTO_PLAY_VIDEO: 'auto_play_video' |
df98563e | 79 | } |
bd5c83a8 | 80 | |
df98563e | 81 | tokens: Tokens |
bd5c83a8 | 82 | |
df98563e | 83 | static load () { |
0bd78bf3 | 84 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) |
bd5c83a8 | 85 | if (usernameLocalStorage) { |
7da18e44 C |
86 | return new AuthUser( |
87 | { | |
0bd78bf3 C |
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, | |
2243730c | 92 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, |
ed638e53 | 93 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', |
276d9652 C |
94 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', |
95 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | |
7da18e44 | 96 | }, |
629d8d6f | 97 | Tokens.load() |
df98563e | 98 | ) |
bd5c83a8 C |
99 | } |
100 | ||
df98563e | 101 | return null |
bd5c83a8 C |
102 | } |
103 | ||
df98563e | 104 | static flush () { |
0bd78bf3 C |
105 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) |
106 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
107 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
2243730c | 108 | peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) |
ed638e53 | 109 | peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED) |
276d9652 | 110 | peertubeLocalStorage.removeItem(this.KEYS.VIDEOS_HISTORY_ENABLED) |
0bd78bf3 C |
111 | peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) |
112 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) | |
df98563e | 113 | Tokens.flush() |
bd5c83a8 C |
114 | } |
115 | ||
276d9652 | 116 | constructor (userHash: Partial<ServerUserModel>, hashTokens: TokenOptions) { |
df98563e C |
117 | super(userHash) |
118 | this.tokens = new Tokens(hashTokens) | |
bd5c83a8 C |
119 | } |
120 | ||
df98563e C |
121 | getAccessToken () { |
122 | return this.tokens.accessToken | |
bd5c83a8 | 123 | } |
bd5c83a8 | 124 | |
df98563e C |
125 | getRefreshToken () { |
126 | return this.tokens.refreshToken | |
bd5c83a8 C |
127 | } |
128 | ||
df98563e C |
129 | getTokenType () { |
130 | return this.tokens.tokenType | |
bd5c83a8 C |
131 | } |
132 | ||
df98563e C |
133 | refreshTokens (accessToken: string, refreshToken: string) { |
134 | this.tokens.accessToken = accessToken | |
135 | this.tokens.refreshToken = refreshToken | |
bd5c83a8 C |
136 | } |
137 | ||
757f0da3 | 138 | hasRight (right: UserRight) { |
954605a8 C |
139 | return hasUserRight(this.role, right) |
140 | } | |
141 | ||
a95a4cc8 C |
142 | canManage (user: ServerUserModel) { |
143 | const myRole = this.role | |
144 | ||
145 | if (myRole === UserRole.ADMINISTRATOR) return true | |
146 | ||
147 | // I'm a moderator: I can only manage users | |
148 | return user.role === UserRole.USER | |
149 | } | |
150 | ||
df98563e | 151 | save () { |
0bd78bf3 C |
152 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
153 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
154 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
155 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | |
2243730c | 156 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) |
ed638e53 | 157 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) |
0bd78bf3 | 158 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) |
df98563e | 159 | this.tokens.save() |
bd5c83a8 C |
160 | } |
161 | } |