Skip to content

Commit

Permalink
Merge pull request #2 from maxlaverse/fix_param_encoding
Browse files Browse the repository at this point in the history
Encode URL parameters
  • Loading branch information
maxlaverse authored Feb 17, 2020
2 parents e3748e7 + f738bf2 commit cbe61d7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ DerivedData
Pods

*.pem
KeepMeConnected/KeepMeConnected.app/
2 changes: 2 additions & 0 deletions KeepMeConnected.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = net.laverse.KeepMeConnected;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
Expand All @@ -344,6 +345,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = net.laverse.KeepMeConnected;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
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.1</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
55 changes: 30 additions & 25 deletions KeepMeConnected/WatchGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,42 @@ class WatchGuard: NSObject {
request.timeoutInterval = 3
request.httpMethod = "POST"

// Body
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")
request.addValue("*/*", forHTTPHeaderField: "Accept")
request.addValue(WatchGuard.UserAgent, forHTTPHeaderField: "User-Agent")

// Execute the request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
handler(WatchGuardLoginResponse.Error(error!.localizedDescription.dropSuffix(".")))
} else {
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.url == portalUrl || httpResponse.url?.absoluteString.range(of:"success.shtml") != nil {
handler(WatchGuardLoginResponse.Success)
}else{
if let errcode = URLComponents(string: httpResponse.url!.absoluteString)?.queryItems?.filter({$0.name == "errcode"}).first?.value {
os_log("Error on logon. Error (%@) '%@'",errcode,WatchGuard.getErrorCodeStr(errcode))
handler(WatchGuardLoginResponse.Failed(WatchGuard.getErrorCodeStr(errcode)))
if let encodedUserDomain = userDomain.addingPercentEncoding(withAllowedCharacters:.alphanumerics),
let encodedUserName = userName.addingPercentEncoding(withAllowedCharacters:.alphanumerics),
let encodedUserPassword = userPassword.addingPercentEncoding(withAllowedCharacters:.alphanumerics) {

// Body
request.httpBody = "action=fw_logon&fw_domain=\(encodedUserDomain)&fw_logon_type=logon&fw_username=\(encodedUserName)&fw_password=\(encodedUserPassword)&lang=en-US&submit=Login".data(using: String.Encoding.ascii, allowLossyConversion: false)

// Headers
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("*/*", forHTTPHeaderField: "Accept")
request.addValue(WatchGuard.UserAgent, forHTTPHeaderField: "User-Agent")

// Execute the request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
handler(WatchGuardLoginResponse.Error(error!.localizedDescription.dropSuffix(".")))
} else {
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.url == portalUrl || httpResponse.url?.absoluteString.range(of:"success.shtml") != nil {
handler(WatchGuardLoginResponse.Success)
}else{
os_log("Error on logon. Wrong base '%@'",httpResponse.url!.absoluteString)
handler(WatchGuardLoginResponse.Error(String(format: "Missing errcode in redirection to '%@'",httpResponse.url!.absoluteString)))
if let errcode = URLComponents(string: httpResponse.url!.absoluteString)?.queryItems?.filter({$0.name == "errcode"}).first?.value {
os_log("Error on logon. Error (%@) '%@'",errcode,WatchGuard.getErrorCodeStr(errcode))
handler(WatchGuardLoginResponse.Failed(WatchGuard.getErrorCodeStr(errcode)))
}else{
os_log("Error on logon. Wrong base '%@'",httpResponse.url!.absoluteString)
handler(WatchGuardLoginResponse.Error(String(format: "Missing errcode in redirection to '%@'",httpResponse.url!.absoluteString)))
}
}
}else{
handler(WatchGuardLoginResponse.Error("No HTTPURLResponse was returned"))
}
}else{
handler(WatchGuardLoginResponse.Error("No HTTPURLResponse was returned"))
}
}
task.resume()
}
task.resume()
}

func logout(portalUrl: URL,handler: @escaping (_: WatchGuardLogoutResponse) -> Void){
Expand Down

0 comments on commit cbe61d7

Please sign in to comment.