/
SessionMonitor.ts
179 lines (158 loc) · 6.52 KB
/
SessionMonitor.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
// 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 } from "./utils";
import { CheckSessionIFrame } from "./CheckSessionIFrame";
import type { UserManager } from "./UserManager";
import type { User } from "./User";
/**
* @public
*/
export class SessionMonitor {
private readonly _logger = new Logger("SessionMonitor");
private _sub: string | undefined;
private _checkSessionIFrame?: CheckSessionIFrame;
public constructor(private readonly _userManager: UserManager) {
if (!_userManager) {
this._logger.throw(new Error("No user manager passed"));
}
this._userManager.events.addUserLoaded(this._start);
this._userManager.events.addUserUnloaded(this._stop);
this._init().catch((err: unknown) => {
// catch to suppress errors since we're in a ctor
this._logger.error(err);
});
}
protected async _init(): Promise<void> {
this._logger.create("_init");
const user = await this._userManager.getUser();
// doing this manually here since calling getUser
// doesn't trigger load event.
if (user) {
void this._start(user);
}
else if (this._userManager.settings.monitorAnonymousSession) {
const session = await this._userManager.querySessionStatus();
if (session) {
const tmpUser = {
session_state: session.session_state,
profile: session.sub ? {
sub: session.sub,
} : null,
};
void this._start(tmpUser);
}
}
}
protected _start = async (
user: User | {
session_state: string;
profile: { sub: string } | null;
},
): Promise<void> => {
const session_state = user.session_state;
if (!session_state) {
return;
}
const logger = this._logger.create("_start");
if (user.profile) {
this._sub = user.profile.sub;
logger.debug("session_state", session_state, ", sub", this._sub);
}
else {
this._sub = undefined;
logger.debug("session_state", session_state, ", anonymous user");
}
if (this._checkSessionIFrame) {
this._checkSessionIFrame.start(session_state);
return;
}
try {
const url = await this._userManager.metadataService.getCheckSessionIframe();
if (url) {
logger.debug("initializing check session iframe");
const client_id = this._userManager.settings.client_id;
const intervalInSeconds = this._userManager.settings.checkSessionIntervalInSeconds;
const stopOnError = this._userManager.settings.stopCheckSessionOnError;
const checkSessionIFrame = new CheckSessionIFrame(this._callback, client_id, url, intervalInSeconds, stopOnError);
await checkSessionIFrame.load();
this._checkSessionIFrame = checkSessionIFrame;
checkSessionIFrame.start(session_state);
}
else {
logger.warn("no check session iframe found in the metadata");
}
}
catch (err) {
// catch to suppress errors since we're in non-promise callback
logger.error("Error from getCheckSessionIframe:", err instanceof Error ? err.message : err);
}
};
protected _stop = (): void => {
const logger = this._logger.create("_stop");
this._sub = undefined;
if (this._checkSessionIFrame) {
this._checkSessionIFrame.stop();
}
if (this._userManager.settings.monitorAnonymousSession) {
// using a timer to delay re-initialization to avoid race conditions during signout
// TODO rewrite to use promise correctly
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const timerHandle = setInterval(async () => {
clearInterval(timerHandle);
try {
const session = await this._userManager.querySessionStatus();
if (session) {
const tmpUser = {
session_state: session.session_state,
profile: session.sub ? {
sub: session.sub,
} : null,
};
void this._start(tmpUser);
}
}
catch (err) {
// catch to suppress errors since we're in a callback
logger.error("error from querySessionStatus", err instanceof Error ? err.message : err);
}
}, 1000);
}
};
protected _callback = async (): Promise<void> => {
const logger = this._logger.create("_callback");
try {
const session = await this._userManager.querySessionStatus();
let raiseEvent = true;
if (session && this._checkSessionIFrame) {
if (session.sub === this._sub) {
raiseEvent = false;
this._checkSessionIFrame.start(session.session_state);
logger.debug("same sub still logged in at OP, session state has changed, restarting check session iframe; session_state", session.session_state);
await this._userManager.events._raiseUserSessionChanged();
}
else {
logger.debug("different subject signed into OP", session.sub);
}
}
else {
logger.debug("subject no longer signed into OP");
}
if (raiseEvent) {
if (this._sub) {
await this._userManager.events._raiseUserSignedOut();
}
else {
await this._userManager.events._raiseUserSignedIn();
}
} else {
logger.debug("no change in session detected, no event to raise");
}
}
catch (err) {
if (this._sub) {
logger.debug("Error calling queryCurrentSigninSession; raising signed out event", err);
await this._userManager.events._raiseUserSignedOut();
}
}
};
}