]>
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 | AUTO_PLAY_VIDEO: 'auto_play_video' | |
76 | } | |
77 | ||
78 | tokens: Tokens | |
79 | ||
80 | static load () { | |
81 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) | |
82 | if (usernameLocalStorage) { | |
83 | return new AuthUser( | |
84 | { | |
85 | id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), | |
86 | username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), | |
87 | email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), | |
88 | role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, | |
89 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, | |
90 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' | |
91 | }, | |
92 | Tokens.load() | |
93 | ) | |
94 | } | |
95 | ||
96 | return null | |
97 | } | |
98 | ||
99 | static flush () { | |
100 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) | |
101 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
102 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
103 | peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) | |
104 | peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) | |
105 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) | |
106 | Tokens.flush() | |
107 | } | |
108 | ||
109 | constructor (userHash: UserConstructorHash, hashTokens: TokenOptions) { | |
110 | super(userHash) | |
111 | this.tokens = new Tokens(hashTokens) | |
112 | } | |
113 | ||
114 | getAccessToken () { | |
115 | return this.tokens.accessToken | |
116 | } | |
117 | ||
118 | getRefreshToken () { | |
119 | return this.tokens.refreshToken | |
120 | } | |
121 | ||
122 | getTokenType () { | |
123 | return this.tokens.tokenType | |
124 | } | |
125 | ||
126 | refreshTokens (accessToken: string, refreshToken: string) { | |
127 | this.tokens.accessToken = accessToken | |
128 | this.tokens.refreshToken = refreshToken | |
129 | } | |
130 | ||
131 | hasRight (right: UserRight) { | |
132 | return hasUserRight(this.role, right) | |
133 | } | |
134 | ||
135 | save () { | |
136 | peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | |
137 | peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | |
138 | peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | |
139 | peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) | |
140 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) | |
141 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) | |
142 | this.tokens.save() | |
143 | } | |
144 | } |