-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
auth.ts
185 lines (146 loc) · 4.58 KB
/
auth.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
import { IAuth, AuthCredentials, AuthResult, AuthToken, AuthOptions, AuthTokenType } from '../auth';
import { PasswordsHandler } from '../handlers/passwords';
import { IStorage } from '../storage';
import { ITransport } from '../transport';
export type AuthStorage<T extends AuthTokenType = 'DynamicToken'> = {
access_token: T extends 'DynamicToken' | 'StaticToken' ? string : null;
expires: T extends 'DynamicToken' ? number : null;
refresh_token?: T extends 'DynamicToken' ? string : null;
};
export class Auth extends IAuth {
autoRefresh = true;
msRefreshBeforeExpires = 30000;
staticToken = '';
private _storage: IStorage;
private _transport: ITransport;
private passwords?: PasswordsHandler;
private _refreshPromise?: Promise<AuthResult | false>;
constructor(options: AuthOptions) {
super();
this._transport = options.transport;
this._storage = options.storage;
this.autoRefresh = options?.autoRefresh ?? this.autoRefresh;
this.mode = options?.mode ?? this.mode;
this.msRefreshBeforeExpires = options?.msRefreshBeforeExpires ?? this.msRefreshBeforeExpires;
if (options?.staticToken) {
this.staticToken = options?.staticToken;
this.updateStorage<'StaticToken'>({
access_token: this.staticToken,
expires: null,
refresh_token: null,
});
}
}
get storage(): IStorage {
return this._storage;
}
get transport(): ITransport {
return this._transport;
}
get token(): Promise<string | null> {
return (async () => {
if (this._refreshPromise) {
try {
await this._refreshPromise;
} finally {
this._refreshPromise = undefined;
}
}
return this._storage.auth_token;
})();
}
get password(): PasswordsHandler {
return (this.passwords = this.passwords || new PasswordsHandler(this._transport));
}
private resetStorage() {
this._storage.auth_token = null;
this._storage.auth_refresh_token = null;
this._storage.auth_expires = null;
this._storage.auth_expires_at = null;
}
private updateStorage<T extends AuthTokenType>(result: AuthStorage<T>) {
const expires = result.expires ?? null;
this._storage.auth_token = result.access_token;
this._storage.auth_refresh_token = result.refresh_token ?? null;
this._storage.auth_expires = expires;
this._storage.auth_expires_at = new Date().getTime() + (expires ?? 0);
}
async refreshIfExpired() {
if (this.staticToken) return;
if (!this.autoRefresh) return;
if (!this._storage.auth_expires_at) {
// wait because resetStorage() call in refresh()
try {
await this._refreshPromise;
} finally {
this._refreshPromise = undefined;
}
return;
}
if (this._storage.auth_expires_at < new Date().getTime() + this.msRefreshBeforeExpires) {
this.refresh();
}
try {
await this._refreshPromise; // wait for refresh
} finally {
this._refreshPromise = undefined;
}
}
refresh(): Promise<AuthResult | false> {
const refreshPromise = async () => {
const refresh_token = this._storage.auth_refresh_token;
this.resetStorage();
const response = await this._transport.post<AuthResult>('/auth/refresh', {
refresh_token: this.mode === 'json' ? refresh_token : undefined,
});
this.updateStorage<'DynamicToken'>(response.data!);
return {
access_token: response.data!.access_token,
...(response.data?.refresh_token && { refresh_token: response.data.refresh_token }),
expires: response.data!.expires,
};
};
return (this._refreshPromise = refreshPromise());
}
async login(credentials: AuthCredentials): Promise<AuthResult> {
this.resetStorage();
const response = await this._transport.post<AuthResult>(
'/auth/login',
{ mode: this.mode, ...credentials },
{ headers: { Authorization: null } }
);
this.updateStorage(response.data!);
return {
access_token: response.data!.access_token,
...(response.data?.refresh_token && {
refresh_token: response.data.refresh_token,
}),
expires: response.data!.expires,
};
}
async static(token: AuthToken): Promise<boolean> {
if (!this.staticToken) this.staticToken = token;
await this._transport.get('/users/me', {
params: { access_token: token },
headers: { Authorization: null },
});
this.updateStorage<'StaticToken'>({
access_token: token,
expires: null,
refresh_token: null,
});
return true;
}
async logout(): Promise<void> {
let refresh_token: string | undefined;
if (this.mode === 'json') {
refresh_token = this._storage.auth_refresh_token || undefined;
}
await this._transport.post('/auth/logout', { refresh_token });
this.updateStorage<null>({
access_token: null,
expires: null,
refresh_token: null,
});
}
}