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