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