auth-vir 2.0.4 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type JsonCompatibleObject, type MaybePromise, type PartialWithUndefined, type SelectFrom } from '@augment-vir/common';
|
|
1
|
+
import { type JsonCompatibleObject, type MaybePromise, type PartialWithUndefined, type SelectFrom, type SetOptionalWithUndefined } from '@augment-vir/common';
|
|
2
|
+
import { type AnyDuration } from 'date-vir';
|
|
2
3
|
import { type EmptyObject } from 'type-fest';
|
|
3
4
|
/**
|
|
4
5
|
* Config for {@link FrontendAuthClient}.
|
|
@@ -13,6 +14,22 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
|
|
|
13
14
|
canAssumeUser: () => MaybePromise<boolean>;
|
|
14
15
|
/** Called whenever the current user becomes unauthorized and their CSRF token is wiped. */
|
|
15
16
|
authClearedCallback: () => MaybePromise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Performs automatic checks on an interval to see if the user is still authenticated. Omit this
|
|
19
|
+
* to turn off automatic checks.
|
|
20
|
+
*/
|
|
21
|
+
checkUser: {
|
|
22
|
+
/**
|
|
23
|
+
* Get a response from the backend to see if the user is still authenticated. If the
|
|
24
|
+
* response returns a non-authorized status, the user is wiped. Any other status is
|
|
25
|
+
* ignored.
|
|
26
|
+
*/
|
|
27
|
+
performCheck: () => MaybePromise<SelectFrom<Response, {
|
|
28
|
+
status: true;
|
|
29
|
+
}>>;
|
|
30
|
+
/** @default {minutes: 1} */
|
|
31
|
+
interval?: AnyDuration | undefined;
|
|
32
|
+
};
|
|
16
33
|
overrides: PartialWithUndefined<{
|
|
17
34
|
localStorage: Pick<Storage, 'setItem' | 'removeItem' | 'getItem'>;
|
|
18
35
|
csrfHeaderName: string;
|
|
@@ -28,7 +45,13 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
|
|
|
28
45
|
*/
|
|
29
46
|
export declare class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject = EmptyObject> {
|
|
30
47
|
protected readonly config: FrontendAuthClientConfig;
|
|
48
|
+
protected userCheckInterval: undefined | ReturnType<typeof globalThis.setInterval>;
|
|
31
49
|
constructor(config?: FrontendAuthClientConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Destroys the client and performs all necessary cleanup (like clearing the user check
|
|
52
|
+
* interval).
|
|
53
|
+
*/
|
|
54
|
+
destroy(): void;
|
|
32
55
|
/** Wraps {@link getCurrentCsrfToken} to automatically handle wiping an invalid CSRF token. */
|
|
33
56
|
getCurrentCsrfToken(): Promise<string | undefined>;
|
|
34
57
|
/**
|
|
@@ -62,8 +85,8 @@ export declare class FrontendAuthClient<AssumedUserParams extends JsonCompatible
|
|
|
62
85
|
* Use to verify _all_ responses received from the backend. Immediately logs the user out once
|
|
63
86
|
* an unauthorized response is detected.
|
|
64
87
|
*/
|
|
65
|
-
verifyResponseAuth(response: Readonly<SelectFrom<Response, {
|
|
88
|
+
verifyResponseAuth(response: Readonly<SetOptionalWithUndefined<SelectFrom<Response, {
|
|
66
89
|
status: true;
|
|
67
90
|
headers: true;
|
|
68
|
-
}>>): Promise<void>;
|
|
91
|
+
}>, 'headers'>>): Promise<void>;
|
|
69
92
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HttpStatus, } from '@augment-vir/common';
|
|
2
|
+
import { convertDuration } from 'date-vir';
|
|
2
3
|
import { CsrfTokenFailureReason, extractCsrfTokenHeader, getCurrentCsrfToken, storeCsrfToken, wipeCurrentCsrfToken, } from '../csrf-token.js';
|
|
3
4
|
import { AuthHeaderName } from '../headers.js';
|
|
4
5
|
/**
|
|
@@ -10,8 +11,29 @@ import { AuthHeaderName } from '../headers.js';
|
|
|
10
11
|
*/
|
|
11
12
|
export class FrontendAuthClient {
|
|
12
13
|
config;
|
|
14
|
+
userCheckInterval;
|
|
13
15
|
constructor(config = {}) {
|
|
14
16
|
this.config = config;
|
|
17
|
+
if (config.checkUser) {
|
|
18
|
+
const intervalDuration = convertDuration(config.checkUser.interval || { minutes: 1 }, {
|
|
19
|
+
milliseconds: true,
|
|
20
|
+
}).milliseconds;
|
|
21
|
+
this.userCheckInterval = globalThis.setInterval(async () => {
|
|
22
|
+
const response = await config.checkUser?.performCheck();
|
|
23
|
+
if (response) {
|
|
24
|
+
await this.verifyResponseAuth({
|
|
25
|
+
status: response.status,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}, intervalDuration);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Destroys the client and performs all necessary cleanup (like clearing the user check
|
|
33
|
+
* interval).
|
|
34
|
+
*/
|
|
35
|
+
destroy() {
|
|
36
|
+
globalThis.clearInterval(this.userCheckInterval);
|
|
15
37
|
}
|
|
16
38
|
/** Wraps {@link getCurrentCsrfToken} to automatically handle wiping an invalid CSRF token. */
|
|
17
39
|
async getCurrentCsrfToken() {
|
|
@@ -111,7 +133,7 @@ export class FrontendAuthClient {
|
|
|
111
133
|
*/
|
|
112
134
|
async verifyResponseAuth(response) {
|
|
113
135
|
if (response.status === HttpStatus.Unauthorized &&
|
|
114
|
-
!response.headers
|
|
136
|
+
!response.headers?.get(AuthHeaderName.IsSignUpAuth)) {
|
|
115
137
|
await this.logout();
|
|
116
138
|
}
|
|
117
139
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,9 @@ import {
|
|
|
4
4
|
type MaybePromise,
|
|
5
5
|
type PartialWithUndefined,
|
|
6
6
|
type SelectFrom,
|
|
7
|
+
type SetOptionalWithUndefined,
|
|
7
8
|
} from '@augment-vir/common';
|
|
9
|
+
import {convertDuration, type AnyDuration} from 'date-vir';
|
|
8
10
|
import {type EmptyObject} from 'type-fest';
|
|
9
11
|
import {
|
|
10
12
|
CsrfTokenFailureReason,
|
|
@@ -28,6 +30,29 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
|
|
|
28
30
|
canAssumeUser: () => MaybePromise<boolean>;
|
|
29
31
|
/** Called whenever the current user becomes unauthorized and their CSRF token is wiped. */
|
|
30
32
|
authClearedCallback: () => MaybePromise<void>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Performs automatic checks on an interval to see if the user is still authenticated. Omit this
|
|
36
|
+
* to turn off automatic checks.
|
|
37
|
+
*/
|
|
38
|
+
checkUser: {
|
|
39
|
+
/**
|
|
40
|
+
* Get a response from the backend to see if the user is still authenticated. If the
|
|
41
|
+
* response returns a non-authorized status, the user is wiped. Any other status is
|
|
42
|
+
* ignored.
|
|
43
|
+
*/
|
|
44
|
+
performCheck: () => MaybePromise<
|
|
45
|
+
SelectFrom<
|
|
46
|
+
Response,
|
|
47
|
+
{
|
|
48
|
+
status: true;
|
|
49
|
+
}
|
|
50
|
+
>
|
|
51
|
+
>;
|
|
52
|
+
/** @default {minutes: 1} */
|
|
53
|
+
interval?: AnyDuration | undefined;
|
|
54
|
+
};
|
|
55
|
+
|
|
31
56
|
overrides: PartialWithUndefined<{
|
|
32
57
|
localStorage: Pick<Storage, 'setItem' | 'removeItem' | 'getItem'>;
|
|
33
58
|
csrfHeaderName: string;
|
|
@@ -43,7 +68,32 @@ export type FrontendAuthClientConfig = PartialWithUndefined<{
|
|
|
43
68
|
* @category Client
|
|
44
69
|
*/
|
|
45
70
|
export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject = EmptyObject> {
|
|
46
|
-
|
|
71
|
+
protected userCheckInterval: undefined | ReturnType<typeof globalThis.setInterval>;
|
|
72
|
+
|
|
73
|
+
constructor(protected readonly config: FrontendAuthClientConfig = {}) {
|
|
74
|
+
if (config.checkUser) {
|
|
75
|
+
const intervalDuration = convertDuration(config.checkUser.interval || {minutes: 1}, {
|
|
76
|
+
milliseconds: true,
|
|
77
|
+
}).milliseconds;
|
|
78
|
+
|
|
79
|
+
this.userCheckInterval = globalThis.setInterval(async () => {
|
|
80
|
+
const response = await config.checkUser?.performCheck();
|
|
81
|
+
if (response) {
|
|
82
|
+
await this.verifyResponseAuth({
|
|
83
|
+
status: response.status,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}, intervalDuration);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Destroys the client and performs all necessary cleanup (like clearing the user check
|
|
92
|
+
* interval).
|
|
93
|
+
*/
|
|
94
|
+
public destroy() {
|
|
95
|
+
globalThis.clearInterval(this.userCheckInterval);
|
|
96
|
+
}
|
|
47
97
|
|
|
48
98
|
/** Wraps {@link getCurrentCsrfToken} to automatically handle wiping an invalid CSRF token. */
|
|
49
99
|
public async getCurrentCsrfToken(): Promise<string | undefined> {
|
|
@@ -176,18 +226,21 @@ export class FrontendAuthClient<AssumedUserParams extends JsonCompatibleObject =
|
|
|
176
226
|
*/
|
|
177
227
|
public async verifyResponseAuth(
|
|
178
228
|
response: Readonly<
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
229
|
+
SetOptionalWithUndefined<
|
|
230
|
+
SelectFrom<
|
|
231
|
+
Response,
|
|
232
|
+
{
|
|
233
|
+
status: true;
|
|
234
|
+
headers: true;
|
|
235
|
+
}
|
|
236
|
+
>,
|
|
237
|
+
'headers'
|
|
185
238
|
>
|
|
186
239
|
>,
|
|
187
240
|
): Promise<void> {
|
|
188
241
|
if (
|
|
189
242
|
response.status === HttpStatus.Unauthorized &&
|
|
190
|
-
!response.headers
|
|
243
|
+
!response.headers?.get(AuthHeaderName.IsSignUpAuth)
|
|
191
244
|
) {
|
|
192
245
|
await this.logout();
|
|
193
246
|
}
|