forked from microsoft/adfsWebCustomization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonload.js
376 lines (323 loc) · 17.7 KB
/
onload.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Telemetry Manager is the App Insights telemetry management object
// Callers MUST call 'Initialize' before using the 'ProducePageDetectionTelemetry' method
//
TelemetryManager = {
/*
* Initialize the app state for the current page
*/
Initialize: function () {
_self = this;
// Collect some page details for later
var NOT_SET_CONST = 'NOTSET';
_self.currentUri = window.location.href.split('?')[0];
_self.mswtrealm = _self.getQueryString("wtrealm") || NOT_SET_CONST;
_self.decodedwtrealm = decodeURIComponent(_self.mswtrealm) || NOT_SET_CONST;
_self.requestID = _self.getQueryString("client-request-id") || NOT_SET_CONST;
_self.wfresh = _self.getQueryString("wfresh") || NOT_SET_CONST;
_self.wauth = _self.getQueryString("wauth") || NOT_SET_CONST;
_self.debugging = _self.getQueryString("debug") || NOT_SET_CONST;
_self.wauth = decodeURIComponent(_self.wauth);
_self.Username = NOT_SET_CONST;
_self.startedPfaWaiting = false;
_self.pfaTimestamp = null;
_self.pfaTimestampOldBrowser = null;
_self.startedAuthSelectionWaiting = false;
_self.authSelectionTimestamp = null;
_self.authSelectionTimestampOldBrowser = null;
_self.authSelectionLinkClicked = null;
_self.authSelectionMethod = null;
_self.startedFormsPage = false;
// Create App Insights object with settings
if (!window.appInsights) {
if(console && _self.debugging) console.log("TelemetryManager: Generating a new App Insights object");
var appInsights = _self.GenerateAppInsightsObject.call();
// Set App Insights object against the current window
window.appInsights = appInsights;
if(console && _self.debugging) console.log("TelemetryManager: Set new App Insights object against the current window");
}
//
// Add unload callback to window, so we can capture telemetry
//
if (window.addEventListener) {
window.addEventListener("unload", function () { _self.LeavingCurrentPageCallback(_self); }, false); // Modern browsers
} else if (window.attachEvent) {
window.attachEvent('onunload', function () { _self.LeavingCurrentPageCallback(_self); }); // Old IE
}
if(console && _self.debugging) console.log("Exit: TelemetryManager.Initialize");
},
/*
* Generate an App Insights object to use when
* sending telemetry.
*/
GenerateAppInsightsObject: function() {
return function (config) {
function r(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } } var t = { config: config }, u = document, e = window, o = "script", s = u.createElement(o), i, f; for (s.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace"]; i.length;) r("track" + i.pop()); return r("setAuthenticatedUserContext"), r("clearAuthenticatedUserContext"), config.disableExceptionTracking || (i = "onerror", r("_" + i), f = e[i], e[i] = function (config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t["_" + i](config, r, u, e, o), s }), t
}({
samplingPercentage: 100,
instrumentationKey: "YOUR-KEY-HERE"
});
},
/*
* Helper function to get a querystring parameter
*/
getQueryString: function(qsName) {
qsName = qsName.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + qsName + "(=([^&#]*)|&|#|$)"),
results = regex.exec(location.href);
if (!results) return "";
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
/*
* Produces all telemetry for the following pages:
* Forms Page
* AuthSelection Page
* Home Realm Discovery Page
* Phone Factor Authentication Page
* Phone Factor Error Page
* ADFS Error Page
* Phone Factor Authentication Options Page
*/
ProducePageDetectionTelemetry: function () {
var _self = this;
if(console && _self.debugging) console.log("Enter: TelemetryManager.ProducePageDetectionTelemetry");
//
// Generic Page view tracking
//
window.appInsights.trackPageView("Generic");
//
// Home Realm Discovery Page
//
var hrd = document.getElementById('hrd');
if (hrd) {
window.appInsights.trackPageView("HomeRealmDiscovery");
if(console && _self.debugging) console.log("ProducePageDetectionTelemetry: Found HRD Page");
return;
}
//
// Forms Page (before creds are entered)
// NOTE: This only works for pages presented in English
//
var pageloginForm = document.getElementById('loginForm');
if (!hrd && pageloginForm && document.title == 'Sign In') {
window.appInsights.trackPageView("FormsPage");
window.appInsights.trackEvent("FormsPageStart",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wauth: _self.wauth, wfresh: _self.wfresh, wtrealm: _self.decodedwtrealm }
);
_self.startedFormsPage = true;
if(console && _self.debugging) console.log("ProducePageDetectionTelemetry: Found Forms Page");
return;
}
//
// Error Page
//
var ierrorText = document.getElementById("errorText");
if (ierrorText) {
var ierrorCurrent = ierrorText.innerHTML;
if (ierrorCurrent.length > 0) {
var pageTitle = document.title;
//
// Try to gather more error information from the page
//
var erruserAccount = _self.GetUserNameFromAuthArea();
var erractivityId = (document.getElementById('activityId') || {innerText:''}).innerText;
var errcontextId = (document.getElementById('contextId')|| {innerText:''}).innerText;
var errtimestamp = (document.getElementById('timestamp')|| {innerText:''}).innerText;
if(erractivityId || errcontextId || errtimestamp){
window.appInsights.trackPageView("ErrorDetailedPage");
window.appInsights.trackEvent("ErrorDetailedPageStart",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, PageTitle: pageTitle, Username: erruserAccount, ActivityID: erractivityId, ContextId: errcontextId, ErrorTimestamp: errtimestamp }
);
if(console && _self.debugging) console.log("ProducePageDetectionTelemetry: Found Detailed Error Page");
}
return;
}
}
//
// AuthSelection Page
//
var authOptions = document.getElementById('authOptions')
var progress = document.getElementById('Progress')
if (authOptions && !progress) {
var foundUsername = _self.GetUserNameFromAuthArea();
_self.Username = foundUsername;
window.appInsights.trackPageView("AuthSelectionPage");
window.appInsights.trackEvent("AuthSelectionPageStart",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username }
);
if(console && _self.debugging) console.log("ProducePageDetectionTelemetry: Found Auth Selection Page");
//
// Add click callbacks to the auth selection options
// NOTE: If you have other options you wish to track, add them here
//
var certOption = document.getElementById('CertificateAuthentication');
if (certOption) {
certOption.addEventListener("click", function () { _self.AuthSelectionPageSubmitCallback("cert", "manual", _self); }, false);
}
var azureOption = document.getElementById('WindowsAzureMultiFactorAuthentication');
if (azureOption) {
azureOption.addEventListener("click", function () { _self.AuthSelectionPageSubmitCallback("phonefactor", "manual", _self); }, false);
}
return;
}
//
// Phone Factor Waiting Page
//
var workArea = document.getElementById('workArea');
var authArea = document.getElementById('authArea');
var progressDiv = document.getElementById('Progress');
var authMethod = document.getElementById('authMethod');
var errorDiv = document.getElementById('errorDiv');
if (workArea && authArea && progressDiv && authMethod && !errorDiv) {
var phonefactorUserID = _self.GetUserNameFromAuthArea();
var authchildren = authArea.childNodes;
for (var i = 0; i < authchildren.length; i++) {
if (authchildren[i].className === 'fieldMargin bigText') {
window.appInsights.trackPageView("PhoneFactorWaitingPage");
window.appInsights.trackEvent("PhoneFactorWaitingStart",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: phonefactorUserID }
);
if(console && _self.debugging) console.log("ProducePageDetectionTelemetry: Found PFA Waiting Page");
// Once we detect the pfa page, add a timer to collect the PFA latency
_self.startedPfaWaiting = true;
_self.Username = phonefactorUserID;
if (performance && performance.now()) {
_self.pfaTimestamp = performance.now();
}
if (Date && Date.now()) {
_self.pfaTimestampOldBrowser = Date.now();
}
return;
}
}
}
if(console && _self.debugging) console.log("Exit: TelemetryManager.ProducePageDetectionTelemetry");
},
/*
* Collect the username from the auth area message
* NOTE: This method only works for pages presented in English
*/
GetUserNameFromAuthArea: function () {
var authchildren = document.getElementById('authArea').childNodes;
for (var i = 0; i < authchildren.length; i++) {
if (authchildren[i].className === 'fieldMargin bigText') {
var tempuserAccount = authchildren[i].innerText;
return tempuserAccount.replace("Welcome ", "");
}
}
},
/*
* Callback function when the AuthSelection page is being submitted after an
* auth option was chosen.
*/
AuthSelectionPageSubmitCallback: function (linkClicked, selectionMethod, _self) {
if(console && _self.debugging) console.log("Enter: TelemetryManager.AuthSelectionPageSubmitCallback");
// Collect telemetry
window.appInsights.trackEvent("AuthSelectionPicked",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wauth: _self.wauth, wfresh: _self.wfresh, Type: linkClicked, SelectionMethod: selectionMethod, wtrealm: _self.decodedwtrealm, Username: _self.Username }
);
if(console && _self.debugging) console.log("AuthSelectionPageSubmitCallback: Link Clicked: " + linkClicked);
// Start the auth selection timer to time from page submit to page unload
_self.startedAuthSelectionWaiting = true;
_self.authSelectionLinkClicked = linkClicked;
_self.authSelectionMethod = selectionMethod;
if (performance && performance.now()) {
_self.authSelectionTimestamp = performance.now();
}
if (Date && Date.now()) {
_self.authSelectionTimestampOldBrowser = Date.now();
}
if(console && _self.debugging) console.log("Exit: TelemetryManager.AuthSelectionPageSubmitCallback");
},
/*
* Callback function when any page is being left
* NOTE: Due to browser unload calls, there is no guarantee that the
* processing in this method will complete. Some of the XHR requests made for
* trackEvent calls may not succeed. This telemetry is a best-effort collection
*/
LeavingCurrentPageCallback: function (_self) {
// Grab the window appInsights object for local use
var localAppInsights = window.appInsights;
var flushMePlease = false;
if (_self.startedFormsPage) {
_self.Username = document.getElementById(Login.userNameInput).value;
localAppInsights.trackEvent("FormsPageEnd",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username }
);
_self.startedFormsPage = false;
flushMePlease = true;
}
var pfaTime = null;
if (_self.pfaTimestamp) {
pfaTime = (performance.now() - _self.pfaTimestamp) / 1000.0;
}
var pfaTimeOldBrowser = null;
if (_self.pfaTimestampOldBrowser) {
pfaTimeOldBrowser = (Date.now() - _self.pfaTimestampOldBrowser) / 1000.0;
}
if (pfaTime) {
localAppInsights.trackEvent("PhoneFactorLatency",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username },
{ Latency: pfaTime }
);
flushMePlease = true;
} else if (pfaTimeOldBrowser) {
localAppInsights.trackEvent("PhoneFactorLatency",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username },
{ OldBrowserLatency: pfaTimeOldBrowser }
);
flushMePlease = true;
}
if (_self.startedPfaWaiting) {
localAppInsights.trackEvent("PhoneFactorWaitingEnd",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username }
);
_self.startedPfaWaiting = false;
flushMePlease = true;
}
if (_self.startedAuthSelectionWaiting) {
localAppInsights.trackEvent("AuthSelectionPageEnd",
{ CorrelationID: _self.requestID, CurrentUri: _self.currentUri, CurrentRealm: _self.mswtrealm, wtrealm: _self.decodedwtrealm, wfresh: _self.wfresh, wauth: _self.wauth, Username: _self.Username, Type: _self.authSelectionLinkClicked, SelectionMethod: _self.authSelectionMethod }
);
_self.startedAuthSelectionWaiting = false;
flushMePlease = true;
}
var authSelectionTime = null;
if (_self.authSelectionTimestamp) {
authSelectionTime = (performance.now() - _self.authSelectionTimestamp) / 1000.0;
}
var authSelectionTimeOldBrowser = null;
if (_self.authSelectionTimestampOldBrowser) {
authSelectionTimeOldBrowser = (Date.now() - _self.authSelectionTimestampOldBrowser) / 1000.0;
}
if (authSelectionTime ) {
localAppInsights.trackEvent("AuthSelectionLatency",
{ CorrelationID: _self.requestID, Username: _self.Username, wauth: _self.wauth, wfresh: _self.wfresh, wtrealm: _self.decodedwtrealm, Type: _self.authSelectionLinkClicked, SelectionMethod: _self.authSelectionMethod },
{ Latency: authSelectionTime }
);
flushMePlease = true;
} else if (authSelectionTimeOldBrowser) {
localAppInsights.trackEvent("AuthSelectionLatency",
{ CorrelationID: _self.requestID, Username: _self.Username, wauth: _self.wauth, wfresh: _self.wfresh, wtrealm: _self.decodedwtrealm, Type: _self.authSelectionLinkClicked, SelectionMethod: _self.authSelectionMethod },
{ OldBrowserLatency: authSelectionTimeOldBrowser }
);
flushMePlease = true;
}
if (flushMePlease) {
if (localAppInsights) {
if (localAppInsights.flush) {
localAppInsights.flush();
}
}
}
},
};
// Produce telemetry
if(console) console.log("TelemetryManager: Start trying to produce telemetry");
var pageTelemetryManager = TelemetryManager;
pageTelemetryManager.Initialize();
pageTelemetryManager.ProducePageDetectionTelemetry();
if(console) console.log("TelemetryManager: End trying to produce telemetry");