From d97f13976f08ea6e7c3f7fd0498fea53eca84874 Mon Sep 17 00:00:00 2001 From: Max Laverse Date: Wed, 5 Dec 2018 18:15:41 +0100 Subject: [PATCH] Split useremail into username and domain --- KeepMeConnected/AuthManager.swift | 4 +- KeepMeConnected/DataManager.swift | 53 +++++++++++++--- KeepMeConnected/Info.plist | 2 +- KeepMeConnected/Preferences.storyboard | 61 ++++++++++++------- .../PreferencesViewController.swift | 31 +++++++--- KeepMeConnected/WatchGuard.swift | 18 +----- 6 files changed, 111 insertions(+), 58 deletions(-) diff --git a/KeepMeConnected/AuthManager.swift b/KeepMeConnected/AuthManager.swift index 4869305..8f38acf 100644 --- a/KeepMeConnected/AuthManager.swift +++ b/KeepMeConnected/AuthManager.swift @@ -103,10 +103,10 @@ class AuthManager: NSObject { func authenticate(){ // Check the required data are present for an authentication - if let portalUrl = DataManager.sharedData.getPortalURL(), let userEmail = DataManager.sharedData.getUserEmail(),let password = DataManager.sharedData.getUserPassword(){ + if let portalUrl = DataManager.sharedData.getPortalURL(), let userName = DataManager.sharedData.getUserName(),let password = DataManager.sharedData.getUserPassword(), let userDomain = DataManager.sharedData.getUserDomain(){ os_log("Will try to authenticate against the portal") - watchGuardClient.logon(portalUrl: portalUrl, userEmail: userEmail, userPassword: password) { response in + watchGuardClient.logon(portalUrl: portalUrl, userName: userName, userPassword: password,userDomain: userDomain) { response in switch(response){ case .Success: os_log("Authentication successful. Checking status") diff --git a/KeepMeConnected/DataManager.swift b/KeepMeConnected/DataManager.swift index 438253b..3e80569 100644 --- a/KeepMeConnected/DataManager.swift +++ b/KeepMeConnected/DataManager.swift @@ -1,9 +1,10 @@ import Cocoa import ServiceManagement -import os.log let KEY_AUTO_START = "KMCAutoStart" let KEY_EMAIL = "KMCEmail" +let KEY_USERNAME = "KMCUsername" +let KEY_DOMAIN = "KMCDomain" let KEY_PORTAL_URL = "KMCPortalURL" let KEY_SHOW_NOTIFICATIONS = "KMCShowNotifications" let KEY_POLLING_RATE = "KMCPollingRate" @@ -64,15 +65,41 @@ class DataManager : NSObject { return true } - func getUserEmail() -> String?{ - return UserDefaults.standard.string(forKey: KEY_EMAIL) + func getUserName() -> String?{ + if UserDefaults.standard.string(forKey: KEY_USERNAME) == nil{ + if let email = UserDefaults.standard.string(forKey: KEY_EMAIL){ + let username = email.components(separatedBy: "@")[0] + setUserName(username) + return username + } + } + return UserDefaults.standard.string(forKey: KEY_USERNAME) } - func setUserEmail(_ email : String) -> Bool { - if !isValidUserEmail(email){ + func setUserName(_ username : String) -> Bool { + if !isValidUserName(username){ return false } - UserDefaults.standard.set(email, forKey: KEY_EMAIL) + UserDefaults.standard.set(username, forKey: KEY_USERNAME) + return true + } + + func getUserDomain() -> String?{ + if UserDefaults.standard.string(forKey: KEY_DOMAIN) == nil{ + if let email = UserDefaults.standard.string(forKey: KEY_EMAIL){ + let domain = email.components(separatedBy: "@")[1] + setUserDomain(domain) + return domain + } + } + return UserDefaults.standard.string(forKey: KEY_DOMAIN) + } + + func setUserDomain(_ domain : String) -> Bool { + if !isValidUserDomain(domain){ + return false + } + UserDefaults.standard.set(domain, forKey: KEY_DOMAIN) return true } @@ -93,10 +120,16 @@ class DataManager : NSObject { UserDefaults.standard.set(showNotifications, forKey: KEY_SHOW_NOTIFICATIONS) } - func isValidUserEmail(_ textStr:String) -> Bool { - let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" - let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) - return emailTest.evaluate(with: textStr) + func isValidUserName(_ textStr:String) -> Bool { + let userNameRegexp = "[A-Z0-9a-z._%+-]+" + let userNameTest = NSPredicate(format:"SELF MATCHES %@", userNameRegexp) + return userNameTest.evaluate(with: textStr) + } + + func isValidUserDomain(_ textStr:String) -> Bool { + let userDomainRegexp = "[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" + let userDomainTest = NSPredicate(format:"SELF MATCHES %@", userDomainRegexp) + return userDomainTest.evaluate(with: textStr) } func isValidPortalURL(_ textStr:String) -> Bool { diff --git a/KeepMeConnected/Info.plist b/KeepMeConnected/Info.plist index 0598dae..f862dc6 100644 --- a/KeepMeConnected/Info.plist +++ b/KeepMeConnected/Info.plist @@ -17,7 +17,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.0.0-beta.1 + 1.0.0-beta.2 CFBundleVersion 1 LSMinimumSystemVersion diff --git a/KeepMeConnected/Preferences.storyboard b/KeepMeConnected/Preferences.storyboard index cdbb346..1fff73a 100644 --- a/KeepMeConnected/Preferences.storyboard +++ b/KeepMeConnected/Preferences.storyboard @@ -155,20 +155,20 @@ - + - + - + - + @@ -177,7 +177,7 @@ - + @@ -186,7 +186,7 @@ - + @@ -198,25 +198,16 @@ - + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + - + @@ -265,7 +284,7 @@ - + diff --git a/KeepMeConnected/PreferencesViewController.swift b/KeepMeConnected/PreferencesViewController.swift index 183be75..fa4517d 100644 --- a/KeepMeConnected/PreferencesViewController.swift +++ b/KeepMeConnected/PreferencesViewController.swift @@ -9,12 +9,13 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate{ let watchGuardClient = WatchGuard() @IBOutlet weak var portalURL: NSTextField! - @IBOutlet weak var email: NSTextField! + @IBOutlet weak var userDomain: NSTextField! + @IBOutlet weak var userName: NSTextField! @IBOutlet weak var userPassword: NSSecureTextField! @IBOutlet weak var testResultImage: NSImageView! @IBOutlet weak var testSpinner: NSProgressIndicator! @IBOutlet weak var testFailureReason: NSTextFieldCell! - + @objc dynamic var runAtLogin = DataManager.sharedData.getRunAtLogin(){ didSet { DataManager.sharedData.setRunAtLogin(runAtLogin) @@ -36,8 +37,9 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate{ super.viewDidAppear() self.portalURL?.delegate = self - self.email?.delegate = self + self.userName?.delegate = self self.userPassword?.delegate = self + self.userDomain?.delegate = self loadValues() @@ -54,13 +56,17 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate{ self.portalURL?.stringValue = p.absoluteString } - if let p = DataManager.sharedData.getUserEmail(){ - self.email?.stringValue = p + if let p = DataManager.sharedData.getUserName(){ + self.userName?.stringValue = p } if let p = DataManager.sharedData.getUserPassword(){ self.userPassword?.stringValue = p } + + if let p = DataManager.sharedData.getUserDomain(){ + self.userDomain?.stringValue = p + } } func controlTextDidChange(_ notification: Notification) { @@ -72,8 +78,15 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate{ textField.textColor = NSColor.red } } - if textField == self.email{ - if DataManager.sharedData.setUserEmail(textField.stringValue){ + if textField == self.userName{ + if DataManager.sharedData.setUserName(textField.stringValue){ + textField.textColor = NSColor.black + }else{ + textField.textColor = NSColor.red + } + } + if textField == self.userDomain{ + if DataManager.sharedData.setUserDomain(textField.stringValue){ textField.textColor = NSColor.black }else{ textField.textColor = NSColor.red @@ -138,13 +151,13 @@ class PreferencesViewController: NSViewController, NSTextFieldDelegate{ func testLogon(){ // Check if the firewall is just accepting any credentials because the way the user is connected (e.g already authenticated through VPN) - watchGuardClient.logon(portalUrl: DataManager.sharedData.getPortalURL()!, userEmail: "crap@crap", userPassword: "crap") { response in + watchGuardClient.logon(portalUrl: DataManager.sharedData.getPortalURL()!, userName: "crap@crap", userPassword: DataManager.sharedData.getUserPassword()!, userDomain: DataManager.sharedData.getUserDomain()!) { response in DispatchQueue.main.async { switch(response){ case .Success: self.displayTestResult(image: ResultImage.Failure, title: "The portal is blindly accepting any credentials.") case .Error(_), .Failed(_): - self.watchGuardClient.logon(portalUrl: DataManager.sharedData.getPortalURL()!, userEmail: DataManager.sharedData.getUserEmail()!, userPassword: DataManager.sharedData.getUserPassword()!) { response in + self.watchGuardClient.logon(portalUrl: DataManager.sharedData.getPortalURL()!, userName: DataManager.sharedData.getUserName()!, userPassword: DataManager.sharedData.getUserPassword()!,userDomain: DataManager.sharedData.getUserDomain()!) { response in DispatchQueue.main.async { switch(response){ case .Success: diff --git a/KeepMeConnected/WatchGuard.swift b/KeepMeConnected/WatchGuard.swift index ddecc93..cd7e5ae 100644 --- a/KeepMeConnected/WatchGuard.swift +++ b/KeepMeConnected/WatchGuard.swift @@ -54,18 +54,14 @@ class WatchGuard: NSObject { task.resume() } - func logon(portalUrl: URL,userEmail: String,userPassword: String, handler: @escaping (_: WatchGuardLoginResponse) -> Void){ - // Check the required data are present - let username = WatchGuard.getUsername(userEmail) - let domain = WatchGuard.getDomain(userEmail) - + func logon(portalUrl: URL,userName: String,userPassword: String,userDomain: String, handler: @escaping (_: WatchGuardLoginResponse) -> Void){ // Prepare request var request = URLRequest(url: URL(string: "\(portalUrl)wgcgi.cgi")!) request.timeoutInterval = 3 request.httpMethod = "POST" // Body - request.httpBody = "action=fw_logon&fw_domain=\(domain)&fw_logon_type=logon&fw_username=\(username)&fw_password=\(userPassword)&lang=en-US&submit=Login".data(using: String.Encoding.ascii, allowLossyConversion: false) + request.httpBody = "action=fw_logon&fw_domain=\(userDomain)&fw_logon_type=logon&fw_username=\(userName)&fw_password=\(userPassword)&lang=en-US&submit=Login".data(using: String.Encoding.ascii, allowLossyConversion: false) // Headers request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") @@ -141,15 +137,7 @@ class WatchGuard: NSObject { } task.resume() } - - class func getUsername(_ email:String) -> String{ - return email.components(separatedBy: "@")[0] - } - - class func getDomain(_ email:String) -> String{ - return email.components(separatedBy: "@")[1] - } - + class func getErrorCodeStr(_ errcode:String) -> String{ switch(errcode){ case "501":