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