-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebViewController.swift
More file actions
595 lines (475 loc) · 24.8 KB
/
WebViewController.swift
File metadata and controls
595 lines (475 loc) · 24.8 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//
// WebViewController.swift
// xCreds
//
// Created by Timothy Perfitt on 4/5/22.
// Modified for ClassLink OIDC tenant support
//
import Foundation
import Cocoa
@preconcurrency import WebKit
import OIDCLite
@available(macOS, deprecated: 11)
class WebViewController: NSViewController, TokenManagerFeedbackDelegate {
struct WebViewControllerError:Error {
var errorDescription: String
}
func invalidCredentials() {
}
func authenticationSuccessful() {
}
func credentialsUpdated(_ credentials: Creds) {
TCSLogWithMark()
var credWithPass = credentials
credWithPass.password = self.password
// NotificationCenter.default.post(name: Notification.Name("TCSTokensUpdated"), object: self, userInfo:["credentials":credWithPass]
// )
updateCredentialsFeedbackDelegate?.credentialsUpdated(credWithPass)
}
@IBOutlet weak var refreshTitleTextField: NSTextField?
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var cancelButton: NSButton!
@available(macOS, deprecated: 11)
var tokenManager=TokenManager()
var password:String?
var updateCredentialsFeedbackDelegate: UpdateCredentialsFeedbackProtocol?
override func viewWillAppear() {
if let refreshTitleTextField = self.refreshTitleTextField {
refreshTitleTextField.isHidden = !DefaultsOverride.standardOverride.bool(forKey: PrefKeys.shouldShowRefreshBanner.rawValue)
if let refreshBannerText = DefaultsOverride.standardOverride.string(forKey: PrefKeys.refreshBannerText.rawValue) {
self.refreshTitleTextField?.stringValue = refreshBannerText
}
}
}
func loadPage() {
Task{ @MainActor in
TCSLogWithMark("Clearing cookies")
self.webView.cleanAllCookies()
TCSLogWithMark()
let licenseState = LicenseChecker().currentLicenseState()
self.webView.navigationDelegate = self
self.tokenManager.feedbackDelegate=self
// TokenManager.shared.oidc().delegate = self
self.clearCookies()
TCSLogWithMark()
switch licenseState {
case .valid(let sec):
let daysRemaining = Int(sec/(24*60*60))
TCSLogWithMark("valid license. Days remaining: \(daysRemaining) (\(sec) seconds)")
if daysRemaining < 14 {
}
break;
case .trial(_):
break
case .invalid,.trialExpired, .expired:
let bundle = Bundle.findBundleWithName(name: "XCreds")
if let bundle = bundle {
let loadPageURL = bundle.url(forResource: "errorpage", withExtension: "html")
if let loadPageURL = loadPageURL {
self.webView.load(URLRequest(url:loadPageURL))
}
}
return
}
NotificationCenter.default.addObserver(self, selector: #selector(self.connectivityStatusHandler(notification:)), name: NSNotification.Name.connectivityStatus, object: nil)
// let discoveryURL = DefaultsOverride.standardOverride.string(forKey: PrefKeys.discoveryURL.rawValue)
NetworkMonitor.shared.startMonitoring()
TCSLogWithMark("Network monitor: adding connectivity status change observer")
do {
// guard let discoveryURL = discoveryURL else {
// TCSLogWithMark("discoveryURL not defined");
//
// throw WebViewControllerError(errorDescription: "The discovery URL not defined in settings. Verify that settings have been configured and scoped to the system (not user).")
// }
TCSLogWithMark("getOidcLoginURL");
let url = try await self.getOidcLoginURL()
TCSLogWithMark("URL: \(url)");
self.webView.load(URLRequest(url: url))
NetworkMonitor.shared.stopMonitoring()
}
catch {
TCSLogWithMark("error: \(error)");
let loadPageTitle = DefaultsOverride.standardOverride.string(forKey: PrefKeys.loadPageTitle.rawValue)?.stripped ?? "loadPageTitle"
var loadPageInfo = DefaultsOverride.standardOverride.string(forKey: PrefKeys.loadPageInfo.rawValue)?.stripped ?? "loadPageInfo"
loadPageInfo = loadPageInfo + "<br><br>" + (error as? WebViewControllerError ?? WebViewControllerError(errorDescription: error.localizedDescription)).errorDescription
let html = "<!DOCTYPE html><html><head><style>.center-screen { display: flex;flex-direction: column;justify-content: center;align-items: center;text-align: center;min-height: 100vh;}</style></head><body><div class=\"center-screen\"> <h1>\(loadPageTitle)</h1><p>\(loadPageInfo)</p></div></body></html>"
self.webView.loadHTMLString(html, baseURL: nil)
}
}
}
@objc func connectivityStatusHandler(notification: Notification) {
TCSLogWithMark("Network monitor: handling connectivity status update")
Task {
try? await tokenManager.oidc().getEndpoints()
TCSLogWithMark("Refresh webview login")
loadPage()
}
}
private func getOidcLoginURL() async throws -> URL {
if let url = try await tokenManager.oidc().createLoginURL() {
return url
}
throw WebViewControllerError(errorDescription: "Error getting OIDC URL")
}
private func clearCookies() {
let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
for: records,
completionHandler: {
print("Removing Cookie")
})
}
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
}
func showErrorMessageAndDeny(_ message:String){
}
func tokenError(_ err: String) {
TCSLogErrorWithMark("authFailure: \(err)")
XCredsAudit().auditError(err)
//TODO: need to post this?
NotificationCenter.default.post(name: Notification.Name("TCSTokensUpdated"), object: self, userInfo:["error":err])
}
}
@available(macOS, deprecated: 11)
extension WebViewController: WKNavigationDelegate {
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// ClassLink redirect interceptor: catches the OIDC redirect back to our
// redirectURI, cancels the navigation (so we don't load the school website),
// extracts the auth code, and exchanges it for tokens.
if let url = navigationAction.request.url,
let targetRedirectURI = DefaultsOverride.standardOverride.string(forKey: PrefKeys.redirectURI.rawValue) {
if url.absoluteString.starts(with: targetRedirectURI) {
if let queryItems = URLComponents(string: url.absoluteString)?.queryItems,
let code = queryItems.first(where: { $0.name == "code" })?.value {
TCSLogWithMark("Intercepted authorization code. Exchanging for tokens.")
decisionHandler(.cancel)
let processingHTML = """
<html><body style='font-family:-apple-system,system-ui;display:flex;justify-content:center;align-items:center;height:100vh;'>
<div style='text-align:center;'>
<h3>Signing in...</h3>
<p>Please wait while we finish setting up your session.</p>
</div>
</body></html>
"""
webView.loadHTMLString(processingHTML, baseURL: nil)
Task {
do {
let shouldUseBasicAuth = DefaultsOverride.standardOverride.bool(forKey: PrefKeys.shouldUseBasicAuth.rawValue)
let tokenResponse = try await self.tokenManager.oidc().getToken(code: code, basicAuth: shouldUseBasicAuth)
TCSLogWithMark("Tokens received successfully.")
DispatchQueue.main.async {
self.tokenManager.tokenResponse(tokens: tokenResponse)
}
} catch {
TCSLogErrorWithMark("Error exchanging code for token: \(error.localizedDescription)")
let errHTML = "<html><body><h3>Login Failed</h3><p>\(error.localizedDescription)</p></body></html>"
webView.loadHTMLString(errHTML, baseURL: nil)
}
}
return
}
}
}
let idpHostName = DefaultsOverride.standardOverride.value(forKey: PrefKeys.idpHostName.rawValue)
var idpHostNames = DefaultsOverride.standardOverride.value(forKey: PrefKeys.idpHostNames.rawValue)
if idpHostNames == nil && idpHostName != nil {
idpHostNames=[idpHostName]
}
let passwordElementID:String? = DefaultsOverride.standardOverride.value(forKey: PrefKeys.passwordElementID.rawValue) as? String
TCSLogWithMark("inserting javascript to get password")
webView.evaluateJavaScript("result", completionHandler: { response, error in
if error != nil {
// TCSLogWithMark(error?.localizedDescription ?? "unknown error")
TCSLogWithMark("password not found")
}
else {
if let responseDict = response as? NSDictionary, let ids = responseDict["ids"] as? Array<String>, let passwords = responseDict["passwords"] as? Array<String> {
guard passwords.count > 0 else {
TCSLogWithMark("No passwords set")
return
}
TCSLogWithMark("found password elements with ids:\(ids)")
guard let host = navigationAction.request.url?.host else {
return
}
var foundHostname = ""
if let idpHostNames = idpHostNames as? Array<String?>,
idpHostNames.contains(host) {
foundHostname=host
}
else if ["login.microsoftonline.com", "login.live.com", "accounts.google.com"].contains(host) || host.contains("okta.com"){
foundHostname=host
}
else {
TCSLogWithMark("hostname (\(host)) not matched so not looking for password")
return
}
TCSLogWithMark("host matches custom idpHostName \(foundHostname)")
if passwords.count==3, passwords[1]==passwords[2] {
TCSLogWithMark("found 3 password fields. so it is a reset password situation")
TCSLogWithMark("========= password set===========")
self.password=passwords[2]
}
else if passwords.count==2, passwords[0]==passwords[1] {
TCSLogWithMark("found 2 password fields. so it is a reset password situation")
TCSLogWithMark("========= password set===========")
self.password=passwords[1]
}
else if let passwordElementID = passwordElementID{
TCSLogWithMark("the id is defined in prefs (\(passwordElementID)) so seeing if that field is on the page.")
// we have a mapped field defined in prefs so only check this.
if ids.count==1, ids[0]==passwordElementID, passwords.count==1 {
TCSLogWithMark("========= password set===========")
self.password=passwords[0]
}
else {
TCSLogWithMark("did not find a single password field on the page with the specified ID so not setting password")
}
}
//
else if passwords.count==1 {
TCSLogWithMark("found 1 password field on the specified page with the set idpHostName. setting password.")
TCSLogWithMark("========= password set===========")
self.password=passwords[0]
}
else {
TCSLogWithMark("No passwords found on page")
}
}
else {
TCSLogWithMark("password not set")
}
}
})
decisionHandler(.allow)
}
// func setupAppearance() {
// let screenRect = NSScreen.screens[0].frame
//
// let screenWidth = screenRect.width
// let screenHeight = screenRect.height
//
//
// self.view.frame=NSMakeRect((screenWidth-CGFloat(loginWindowWidth))/2,(screenHeight-CGFloat(loginWindowHeight))/2, CGFloat(loginWindowWidth), CGFloat(loginWindowHeight))
// TCSLogWithMark()
//
// }
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// ClassLink tenant auto-navigation: when classLinkTenant is set and the
// webview loads the generic ClassLink search page, inject JS to search for
// and click the tenant button automatically.
if let tenant = DefaultsOverride.standardOverride.string(forKey: "classLinkTenant"),
!tenant.isEmpty,
let url = webView.url?.absoluteString {
let safeTenant = tenant.filter { $0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" }
guard !safeTenant.isEmpty else {
TCSLogErrorWithMark("classLinkTenant contains only invalid characters, skipping injection")
// Fall through to password listener below
return
}
let rawSearchTerm = DefaultsOverride.standardOverride.string(forKey: "classLinkSearchTerm")
let safeSearchTerm: String
if let rawSearchTerm = rawSearchTerm, !rawSearchTerm.isEmpty {
let filtered = rawSearchTerm.filter { $0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" || $0 == " " }
safeSearchTerm = filtered.isEmpty ? safeTenant : filtered
} else {
safeSearchTerm = safeTenant
}
let isClassLink = url.contains("launchpad.classlink.com") || url.contains("login.classlink.com")
let isAlreadyOnTenant = url.contains(safeTenant)
let isMFA = url.contains("twoformauth")
let isProcessingAuth = url.contains("code=") || url.contains("state=")
if isClassLink && !isAlreadyOnTenant && !isMFA && !isProcessingAuth {
let rawDisplayName = DefaultsOverride.standardOverride.string(forKey: "classLinkTenantDisplayName") ?? safeSearchTerm
let safeDisplayName = rawDisplayName.filter { $0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" || $0 == " " }
TCSLogWithMark("ClassLink tenant injection: navigating to \(safeTenant) (search: \(safeSearchTerm))")
// Overlay + auto-click JS. All interpolated values are sanitized above
// (alphanumeric + hyphens + underscores + spaces only).
let js = """
(function() {
var overlay = document.createElement('div');
overlay.id = 'xcreds-redirect-mask';
overlay.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:#ffffff;z-index:2147483647;display:flex;align-items:center;justify-content:center;font-family:-apple-system,system-ui,sans-serif;font-size:18px;color:#555;';
overlay.textContent = '';
var spinner = document.createElement('span');
spinner.style.cssText = 'display:inline-block;width:20px;height:20px;border:3px solid rgba(0,0,0,.3);border-radius:50%;border-top-color:#000;animation:spin 1s ease-in-out infinite;margin-right:10px;vertical-align:middle;';
var style = document.createElement('style');
style.textContent = '@keyframes spin { to { transform: rotate(360deg); } }';
var wrapper = document.createElement('div');
wrapper.appendChild(spinner);
wrapper.appendChild(document.createTextNode('Loading \(safeDisplayName) login...'));
overlay.appendChild(wrapper);
overlay.appendChild(style);
document.body.appendChild(overlay);
setTimeout(function() { if(overlay && overlay.parentNode) overlay.remove(); }, 5000);
function waitFor(selector, callback) {
var startTime = Date.now();
var interval = setInterval(function() {
var el = document.querySelector(selector);
if (el) {
clearInterval(interval);
callback(el);
}
if (Date.now() - startTime > 4000) clearInterval(interval);
}, 50);
}
waitFor('.search-bar-input', function(input) {
var setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
setter.call(input, "\(safeSearchTerm)");
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
input.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true }));
waitFor('.dropdown-list-item', function() {
var targetButton = document.querySelector('button[data-code="\(safeTenant)"]');
if (targetButton) {
targetButton.click();
} else {
var headers = document.querySelectorAll('.item-header');
for (var i = 0; i < headers.length; i++) {
if (headers[i].textContent.toLowerCase().includes('\(safeSearchTerm.lowercased())')) {
headers[i].click();
break;
}
}
}
});
});
})();
"""
webView.evaluateJavaScript(js, completionHandler: nil)
}
}
// Password listener injection: attaches to keydown/keyup to capture
// password fields for local keychain sync.
TCSLogWithMark("adding listener for password")
var pathURL:URL?
let bundle = Bundle.findBundleWithName(name: "XCreds")
if let bundle = bundle {
TCSLogWithMark()
pathURL = bundle.url(forResource: "get_pw", withExtension: "js")
}
guard let pathURL = pathURL else {
TCSLogErrorWithMark("get_pw.js not found")
return
}
let javascript = try? String(contentsOf: pathURL, encoding: .utf8)
guard let javascript = javascript else {
return
}
webView.evaluateJavaScript(javascript, completionHandler: { response, error in
if (error != nil){
TCSLogWithMark(error?.localizedDescription ?? "unknown listener error")
if UserDefaults.standard.bool(forKey: "reloadPageOnError")==true {
TCSLogWithMark("reloading page")
self.loadPage()
}
}
else {
TCSLogWithMark("inserted javascript for password setup")
}
})
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
TCSLogErrorWithMark(error.localizedDescription)
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
TCSLogWithMark("Redirect error. if the error is \"Could not connect to the server.\", it is probably safe to ignore. If the error is \"unsupported URL\", please check your redirectURL in prefs matches the one defined in your OIDC app. Error: \(error.localizedDescription)")
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
Task{
guard let url = webView.url else {
return
}
TCSLogWithMark("WebDel:: Did Receive Redirect for: \(url.absoluteString)")
TCSLogWithMark("URL: \(url.absoluteString)")
let redirectURI = try await tokenManager.oidc().redirectURI
TCSLogWithMark("URL: \(url.absoluteString)")
TCSLogWithMark("redirectURI: \(redirectURI)")
if (url.absoluteString.starts(with: (redirectURI))) {
TCSLogWithMark("got redirect URI match. separating URL")
var code = ""
let fullCommand = url.absoluteString
let pathParts = fullCommand.components(separatedBy: "&")
for part in pathParts {
if part.contains("code=") {
TCSLogWithMark("found code=. cleaning up.")
code = part.replacingOccurrences(of: redirectURI + "?" , with: "").replacingOccurrences(of: "code=", with: "")
TCSLogWithMark("getting tokens")
do {
let shouldUseBasicAuth = DefaultsOverride.standardOverride.bool(forKey: PrefKeys.shouldUseBasicAuth.rawValue)
let tokenResponse = try await tokenManager.oidc().getToken(code: code, basicAuth: shouldUseBasicAuth)
TCSLogWithMark("got token. Token ID: \(tokenResponse.idToken ?? "" )")
tokenManager.tokenResponse(tokens: tokenResponse)
}
catch{
TCSLogWithMark("error: \(error)")
}
return
}
}
}
}
}
private func queryToDict(query: String) -> [String:String]? {
let components = query.components(separatedBy: "&")
var dictionary = [String:String]()
for pairs in components {
let pair = pairs.components(separatedBy: "=")
if pair.count == 2 {
dictionary[pair[0]] = pair[1]
}
}
if dictionary.count == 0 {
return nil
}
return dictionary
}
}
//TODO: Integrate?
//extension WebViewController: OIDCLiteDelegate {
//
//// func authFailure(message: String) {
//// TCSLogErrorWithMark("authFailure: \(message)")
//// NotificationCenter.default.post(name: Notification.Name("TCSTokensUpdated"), object: self, userInfo:[:])
////
//// }
//
//
//}
extension String {
func sanitized() -> String {
// see for ressoning on charachrer sets https://superuser.com/a/358861
let invalidCharacters = CharacterSet(charactersIn: "\\/:*?\"<>| ")
.union(.newlines)
.union(.illegalCharacters)
.union(.controlCharacters)
return self
.components(separatedBy: invalidCharacters)
.joined(separator: "")
}
mutating func sanitize() -> Void {
self = self.sanitized()
}
}
extension WKWebView {
func cleanAllCookies() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
records.forEach { record in
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
print("Cookie ::: \(record) deleted")
}
}
}
func refreshCookies() {
self.configuration.processPool = WKProcessPool()
}
}