Skip to content

Commit

Permalink
Split useremail into username and domain
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Dec 5, 2018
1 parent 228ff3a commit d97f139
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 58 deletions.
4 changes: 2 additions & 2 deletions KeepMeConnected/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
53 changes: 43 additions & 10 deletions KeepMeConnected/DataManager.swift
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion KeepMeConnected/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0-beta.1</string>
<string>1.0.0-beta.2</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
61 changes: 40 additions & 21 deletions KeepMeConnected/Preferences.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,20 @@
<objects>
<viewController title="User" id="EgL-bS-oFV" customClass="PreferencesViewController" customModule="KeepMeConnected" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="B5o-WI-wOZ">
<rect key="frame" x="0.0" y="0.0" width="418" height="164"/>
<rect key="frame" x="0.0" y="0.0" width="418" height="217"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="h0a-NN-HdO">
<rect key="frame" x="18" y="127" width="41" height="17"/>
<rect key="frame" x="18" y="182" width="66" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Email:" id="VTF-MX-adL">
<textFieldCell key="cell" lineBreakMode="clipping" title="Username" id="VTF-MX-adL">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField identifier="Email" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hPs-85-taz">
<rect key="frame" x="20" y="97" width="265" height="22"/>
<rect key="frame" x="20" y="153" width="265" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" identifier="Email" placeholderString="[email protected]" drawsBackground="YES" id="fVd-Ww-qZa">
<font key="font" metaFont="system"/>
Expand All @@ -177,7 +177,7 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="P9a-1k-W7L">
<rect key="frame" x="18" y="63" width="66" height="17"/>
<rect key="frame" x="19" y="121" width="66" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Password:" id="k6T-jt-zbZ">
<font key="font" metaFont="system"/>
Expand All @@ -186,7 +186,7 @@
</textFieldCell>
</textField>
<secureTextField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="SWT-vg-z02">
<rect key="frame" x="20" y="36" width="265" height="22"/>
<rect key="frame" x="21" y="93" width="265" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<secureTextFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="Kcd-Ll-Oyd">
<font key="font" metaFont="system"/>
Expand All @@ -198,25 +198,16 @@
</secureTextFieldCell>
</secureTextField>
<progressIndicator hidden="YES" wantsLayer="YES" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" maxValue="100" bezeled="NO" indeterminate="YES" controlSize="small" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="eWn-rp-QXC">
<rect key="frame" x="291" y="39" width="16" height="16"/>
<rect key="frame" x="293" y="96" width="16" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</progressIndicator>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mFC-t1-ehv">
<rect key="frame" x="291" y="39" width="16" height="16"/>
<rect key="frame" x="293" y="96" width="16" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="iaO-0K-51I"/>
</imageView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="v6h-mu-TcX">
<rect key="frame" x="20" y="11" width="380" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" id="1as-34-KAh">
<font key="font" metaFont="system"/>
<color key="textColor" name="systemRedColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="d0v-fy-anO">
<rect key="frame" x="316" y="29" width="88" height="32"/>
<rect key="frame" x="315" y="87" width="88" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Test" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="OtU-DZ-ZZz">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -226,19 +217,47 @@
</connections>
</buttonCell>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mZ4-jJ-UrG">
<rect key="frame" x="19" y="62" width="54" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Domain:" id="cgZ-Bf-4rC">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="lwG-Sm-sv7">
<rect key="frame" x="21" y="34" width="127" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="5GO-Rm-psc">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="v6h-mu-TcX">
<rect key="frame" x="19" y="8" width="380" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" id="1as-34-KAh">
<font key="font" metaFont="system"/>
<color key="textColor" name="systemRedColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
<connections>
<outlet property="email" destination="hPs-85-taz" id="VeV-32-4nB"/>
<outlet property="testFailureReason" destination="1as-34-KAh" id="IdC-w4-5or"/>
<outlet property="testResultImage" destination="mFC-t1-ehv" id="SHz-sD-O55"/>
<outlet property="testSpinner" destination="eWn-rp-QXC" id="Pby-Q8-so1"/>
<outlet property="userDomain" destination="lwG-Sm-sv7" id="0vx-4f-Bwd"/>
<outlet property="userName" destination="hPs-85-taz" id="VeV-32-4nB"/>
<outlet property="userPassword" destination="SWT-vg-z02" id="hXj-Fc-ich"/>
</connections>
</viewController>
<customObject id="y7J-Mm-PlG" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="501" y="333"/>
<point key="canvasLocation" x="501" y="359.5"/>
</scene>
<!--Advanced-->
<scene sceneID="zw1-3z-qMZ">
Expand All @@ -265,7 +284,7 @@
</view>
</viewController>
</objects>
<point key="canvasLocation" x="501" y="546"/>
<point key="canvasLocation" x="501" y="667"/>
</scene>
</scenes>
<resources>
Expand Down
31 changes: 22 additions & 9 deletions KeepMeConnected/PreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit d97f139

Please sign in to comment.