-
Notifications
You must be signed in to change notification settings - Fork 1
/
token-state.ts
207 lines (179 loc) · 5.14 KB
/
token-state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
getCurrentAccessToken,
getRefreshToken,
resetAllTokens,
storeAccessToken,
storeCurrentAccessToken,
storeRefreshToken,
} from "../utils/storage";
import { TokenPayload, getTokenPayload, isTokenExpired } from "../utils/token";
type State = {
refreshToken: string | null;
refreshTokenPayload: TokenPayload | null;
accessToken: string | null;
accessTokenPayload: TokenPayload | null;
};
type Subscriber = (token: TokenPayload | null) => void;
type Unsubscribe = () => void;
/**
* A facade to manage the OAuth refresh & access tokens.
*
* The tokens will be parsed and their payload is available via a
* member of this class.
*
* If assigned, the tokens will be stored in browser storage:
* - The refresh token is stored in localStorage
* - The access token is stored in sessionStorage and cached per scope in localStorage (see "Authentication via OAuth 2.0" in doc/auth.md)
*
* Use `onRefreshTokenUpdate` and `onAccessTokenUpdate` to subscribe
* to token changes, e.g. for token renewal or other token-dependnant
* logic.
*/
class TokenState {
private state: State = {
refreshToken: getRefreshToken(),
refreshTokenPayload: null,
accessToken: getCurrentAccessToken(),
accessTokenPayload: null,
};
private refreshTokenSubscribers: Subscriber[] = [];
private accessTokenSubscribers: Subscriber[] = [];
constructor() {
// Call after-functions for initial token values
this.afterRefreshTokenUpdate(this.refreshToken, false);
this.afterAccessTokenUpdate(this.accessToken, false);
}
/**
* The raw refresh token
*/
get refreshToken() {
return this.state.refreshToken;
}
set refreshToken(refreshToken: string | null) {
this.state.refreshToken = refreshToken;
this.afterRefreshTokenUpdate(refreshToken);
}
/**
* The refresh token's parsed payload
*/
get refreshTokenPayload() {
return this.state.refreshTokenPayload;
}
/**
* The current raw access token
*/
get accessToken() {
return this.state.accessToken;
}
set accessToken(accessToken: string | null) {
this.state.accessToken = accessToken;
this.afterAccessTokenUpdate(accessToken);
}
/**
* The current access token's parsed payload
*/
get accessTokenPayload() {
return this.state.accessTokenPayload;
}
/**
* Returns true if an access token is present
*/
get authenticated(): boolean {
return Boolean(this.accessToken);
}
/**
* The scope of the current access token
*/
get scope(): string | null {
return this.accessTokenPayload?.scope ?? null;
}
/**
* The locale of the current access token
*/
get locale(): string | null {
return this.accessTokenPayload?.locale ?? null;
}
/**
* The instanceId of the current access token
*/
get instanceId(): string | null {
return this.accessTokenPayload?.instanceId ?? null;
}
/**
* Returns true if the refresh token is expired
*/
isRefreshTokenExpired(): boolean {
return isTokenExpired(this.refreshTokenPayload);
}
/**
* Deletes all tokens, including the ones in localStorage of other scopes
*/
resetAllTokens(): void {
this.state.refreshToken = null;
this.state.refreshTokenPayload = null;
this.state.accessToken = null;
this.state.accessTokenPayload = null;
resetAllTokens();
}
/**
* Calls callback whenever the refresh token updates & returns an
* unsubscribe function.
*/
onRefreshTokenUpdate(callback: Subscriber): Unsubscribe {
this.refreshTokenSubscribers.push(callback);
return () => {
const index = this.refreshTokenSubscribers.findIndex(
(s) => s === callback,
);
this.refreshTokenSubscribers.splice(index, 1);
};
}
/**
* Calls callback whenever the current access token updates &
* returns an unsubscribe function.
*/
onAccessTokenUpdate(callback: Subscriber): Unsubscribe {
this.accessTokenSubscribers.push(callback);
return () => {
const index = this.accessTokenSubscribers.findIndex(
(s) => s === callback,
);
this.accessTokenSubscribers.splice(index, 1);
};
}
private afterRefreshTokenUpdate(
refreshToken: string | null,
store = true,
): void {
this.state.refreshTokenPayload = refreshToken
? getTokenPayload(refreshToken)
: null;
if (refreshToken && store) {
storeRefreshToken(refreshToken);
}
this.notifyRefreshTokenSubscribers();
}
private afterAccessTokenUpdate(
accessToken: string | null,
store = true,
): void {
const payload = accessToken ? getTokenPayload(accessToken) : null;
this.state.accessTokenPayload = payload;
if (accessToken && payload && store) {
storeAccessToken(payload.scope, accessToken);
storeCurrentAccessToken(accessToken);
}
this.notifyAccessTokenSubscribers();
}
private notifyRefreshTokenSubscribers(): void {
this.refreshTokenSubscribers.forEach((callback) =>
callback(this.refreshTokenPayload),
);
}
private notifyAccessTokenSubscribers(): void {
this.accessTokenSubscribers.forEach((callback) =>
callback(this.accessTokenPayload),
);
}
}
export const tokenState = new TokenState();