]>
Commit | Line | Data |
---|---|---|
88a7f93f | 1 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
0bd78bf3 | 2 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' |
a6051ba7 | 3 | import { MyUser as ServerMyUserModel, User as ServerUserModel, MyUserSpecialPlaylist } 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 | 68 | |
b7819090 | 69 | export class AuthUser extends User implements ServerMyUserModel { |
df98563e | 70 | tokens: Tokens |
b7819090 | 71 | specialPlaylists: MyUserSpecialPlaylist[] |
bd5c83a8 | 72 | |
df98563e | 73 | static load () { |
0bd78bf3 | 74 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) |
bd5c83a8 | 75 | if (usernameLocalStorage) { |
7da18e44 C |
76 | return new AuthUser( |
77 | { | |
0bd78bf3 C |
78 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), |
79 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), | |
80 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), | |
81 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | |
2243730c | 82 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, |
ed638e53 | 83 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', |
276d9652 C |
84 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', |
85 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | |
7da18e44 | 86 | }, |
629d8d6f | 87 | Tokens.load() |
df98563e | 88 | ) |
bd5c83a8 C |
89 | } |
90 | ||
df98563e | 91 | return null |
bd5c83a8 C |
92 | } |
93 | ||
df98563e | 94 | static flush () { |
0bd78bf3 C |
95 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) |
96 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
97 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
0bd78bf3 | 98 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) |
df98563e | 99 | Tokens.flush() |
bd5c83a8 C |
100 | } |
101 | ||
b7819090 | 102 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { |
df98563e | 103 | super(userHash) |
b7819090 | 104 | |
df98563e | 105 | this.tokens = new Tokens(hashTokens) |
b7819090 | 106 | this.specialPlaylists = userHash.specialPlaylists |
bd5c83a8 C |
107 | } |
108 | ||
df98563e C |
109 | getAccessToken () { |
110 | return this.tokens.accessToken | |
bd5c83a8 | 111 | } |
bd5c83a8 | 112 | |
df98563e C |
113 | getRefreshToken () { |
114 | return this.tokens.refreshToken | |
bd5c83a8 C |
115 | } |
116 | ||
df98563e C |
117 | getTokenType () { |
118 | return this.tokens.tokenType | |
bd5c83a8 C |
119 | } |
120 | ||
df98563e C |
121 | refreshTokens (accessToken: string, refreshToken: string) { |
122 | this.tokens.accessToken = accessToken | |
123 | this.tokens.refreshToken = refreshToken | |
bd5c83a8 C |
124 | } |
125 | ||
757f0da3 | 126 | hasRight (right: UserRight) { |
954605a8 C |
127 | return hasUserRight(this.role, right) |
128 | } | |
129 | ||
a6051ba7 | 130 | canManage (user: ServerUserModel) { |
a95a4cc8 C |
131 | const myRole = this.role |
132 | ||
133 | if (myRole === UserRole.ADMINISTRATOR) return true | |
134 | ||
135 | // I'm a moderator: I can only manage users | |
136 | return user.role === UserRole.USER | |
137 | } | |
138 | ||
df98563e | 139 | save () { |
0bd78bf3 C |
140 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
141 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
142 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
143 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | |
2243730c | 144 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) |
ed638e53 | 145 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) |
0bd78bf3 | 146 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) |
df98563e | 147 | this.tokens.save() |
bd5c83a8 C |
148 | } |
149 | } |