forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionMonitor.js
More file actions
129 lines (111 loc) · 4.99 KB
/
SessionMonitor.js
File metadata and controls
129 lines (111 loc) · 4.99 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
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
// 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 { Log } from './Log.js';
import { CheckSessionIFrame } from './CheckSessionIFrame.js';
export class SessionMonitor {
constructor(userManager, CheckSessionIFrameCtor = CheckSessionIFrame) {
if (!userManager) {
Log.error("SessionMonitor.ctor: No user manager passed to SessionMonitor");
throw new Error("userManager");
}
this._userManager = userManager;
this._CheckSessionIFrameCtor = CheckSessionIFrameCtor;
this._userManager.events.addUserLoaded(this._start.bind(this));
this._userManager.events.addUserUnloaded(this._stop.bind(this));
this._userManager.getUser().then(user => {
// doing this manually here since calling getUser
// doesn't trigger load event.
if (user) {
this._start(user);
}
}).catch(err => {
// catch to suppress errors since we're in a ctor
Log.error("SessionMonitor ctor: error from getUser:", err.message);
});
}
get _settings() {
return this._userManager.settings;
}
get _metadataService() {
return this._userManager.metadataService;
}
get _client_id() {
return this._settings.client_id;
}
get _checkSessionInterval() {
return this._settings.checkSessionInterval;
}
get _stopCheckSessionOnError() {
return this._settings.stopCheckSessionOnError;
}
_start(user) {
let session_state = user.session_state;
if (session_state) {
this._sub = user.profile.sub;
this._sid = user.profile.sid;
Log.debug("SessionMonitor._start: session_state:", session_state, ", sub:", this._sub);
if (!this._checkSessionIFrame) {
this._metadataService.getCheckSessionIframe().then(url => {
if (url) {
Log.debug("SessionMonitor._start: Initializing check session iframe")
let client_id = this._client_id;
let interval = this._checkSessionInterval;
let stopOnError = this._stopCheckSessionOnError;
this._checkSessionIFrame = new this._CheckSessionIFrameCtor(this._callback.bind(this), client_id, url, interval, stopOnError);
this._checkSessionIFrame.load().then(() => {
this._checkSessionIFrame.start(session_state);
});
}
else {
Log.warn("SessionMonitor._start: No check session iframe found in the metadata");
}
}).catch(err => {
// catch to suppress errors since we're in non-promise callback
Log.error("SessionMonitor._start: Error from getCheckSessionIframe:", err.message);
});
}
else {
this._checkSessionIFrame.start(session_state);
}
}
}
_stop() {
this._sub = null;
this._sid = null;
if (this._checkSessionIFrame) {
Log.debug("SessionMonitor._stop");
this._checkSessionIFrame.stop();
}
}
_callback() {
this._userManager.querySessionStatus().then(session => {
var raiseUserSignedOutEvent = true;
if (session) {
if (session.sub === this._sub) {
raiseUserSignedOutEvent = false;
this._checkSessionIFrame.start(session.session_state);
if (session.sid === this._sid) {
Log.debug("SessionMonitor._callback: Same sub still logged in at OP, restarting check session iframe; session_state:", session.session_state);
}
else {
Log.debug("SessionMonitor._callback: Same sub still logged in at OP, session state has changed, restarting check session iframe; session_state:", session.session_state);
this._userManager.events._raiseUserSessionChanged();
}
}
else {
Log.debug("SessionMonitor._callback: Different subject signed into OP:", session.sub);
}
}
else {
Log.debug("SessionMonitor._callback: Subject no longer signed into OP");
}
if (raiseUserSignedOutEvent) {
Log.debug("SessionMonitor._callback: SessionMonitor._callback; raising signed out event");
this._userManager.events._raiseUserSignedOut();
}
}).catch(err => {
Log.debug("SessionMonitor._callback: Error calling queryCurrentSigninSession; raising signed out event", err.message);
this._userManager.events._raiseUserSignedOut();
});
}
}