forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessTokenEvents.ts
More file actions
90 lines (78 loc) · 3.15 KB
/
Copy pathAccessTokenEvents.ts
File metadata and controls
90 lines (78 loc) · 3.15 KB
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
// 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 type { User } from "./User";
/**
* @public
*/
export type AccessTokenCallback = (...ev: unknown[]) => (Promise<void> | void);
/**
* @public
*/
export class AccessTokenEvents {
protected readonly _logger = new Logger("AccessTokenEvents");
private readonly _expiringTimer = new Timer("Access token expiring");
private readonly _expiredTimer = new Timer("Access token expired");
private readonly _expiringNotificationTimeInSeconds: number;
public constructor(args: { expiringNotificationTimeInSeconds: number }) {
this._expiringNotificationTimeInSeconds = args.expiringNotificationTimeInSeconds;
}
public load(container: User): void {
const logger = this._logger.create("load");
// only register events if there's an access token and it has an expiration
if (container.access_token && container.expires_in !== undefined) {
const duration = container.expires_in;
logger.debug("access token present, remaining duration:", duration);
if (duration > 0) {
// only register expiring if we still have time
let expiring = duration - this._expiringNotificationTimeInSeconds;
if (expiring <= 0) {
expiring = 1;
}
logger.debug("registering expiring timer, raising in", expiring, "seconds");
this._expiringTimer.init(expiring);
}
else {
logger.debug("canceling existing expiring timer because we're past expiration.");
this._expiringTimer.cancel();
}
// if it's negative, it will still fire
const expired = duration + 1;
logger.debug("registering expired timer, raising in", expired, "seconds");
this._expiredTimer.init(expired);
}
else {
this._expiringTimer.cancel();
this._expiredTimer.cancel();
}
}
public unload(): void {
this._logger.debug("unload: canceling existing access token timers");
this._expiringTimer.cancel();
this._expiredTimer.cancel();
}
/**
* Add callback: Raised prior to the access token expiring.
*/
public addAccessTokenExpiring(cb: AccessTokenCallback): () => void {
return this._expiringTimer.addHandler(cb);
}
/**
* Remove callback: Raised prior to the access token expiring.
*/
public removeAccessTokenExpiring(cb: AccessTokenCallback): void {
this._expiringTimer.removeHandler(cb);
}
/**
* Add callback: Raised after the access token has expired.
*/
public addAccessTokenExpired(cb: AccessTokenCallback): () => void {
return this._expiredTimer.addHandler(cb);
}
/**
* Remove callback: Raised after the access token has expired.
*/
public removeAccessTokenExpired(cb: AccessTokenCallback): void {
this._expiredTimer.removeHandler(cb);
}
}