-
Notifications
You must be signed in to change notification settings - Fork 215
/
SilentRenewService.ts
65 lines (57 loc) · 2.33 KB
/
SilentRenewService.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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { Logger, Timer } from "./utils";
import { ErrorTimeout } from "./errors";
import type { UserManager } from "./UserManager";
import type { AccessTokenCallback } from "./AccessTokenEvents";
/**
* @internal
*/
export class SilentRenewService {
protected _logger = new Logger("SilentRenewService");
private _isStarted = false;
private readonly _retryTimer = new Timer("Retry Silent Renew");
public constructor(private _userManager: UserManager) {}
public async start(): Promise<void> {
const logger = this._logger.create("start");
if (!this._isStarted) {
this._isStarted = true;
this._userManager.events.addAccessTokenExpiring(this._tokenExpiring);
this._retryTimer.addHandler(this._tokenExpiring);
// this will trigger loading of the user so the expiring events can be initialized
try {
await this._userManager.getUser();
// deliberate nop
}
catch (err) {
// catch to suppress errors since we're in a ctor
logger.error("getUser error", err);
}
}
}
public stop(): void {
if (this._isStarted) {
this._retryTimer.cancel();
this._retryTimer.removeHandler(this._tokenExpiring);
this._userManager.events.removeAccessTokenExpiring(this._tokenExpiring);
this._isStarted = false;
}
}
protected _tokenExpiring: AccessTokenCallback = async () => {
const logger = this._logger.create("_tokenExpiring");
try {
await this._userManager.signinSilent();
logger.debug("silent token renewal successful");
}
catch (err) {
if (err instanceof ErrorTimeout) {
// no response from authority server, e.g. IFrame timeout, ...
logger.warn("ErrorTimeout from signinSilent:", err, "retry in 5s");
this._retryTimer.init(5);
return;
}
logger.error("Error from signinSilent:", err);
await this._userManager.events._raiseSilentRenewError(err as Error);
}
};
}