Skip to content

Commit

Permalink
MBL-1268: Check for 'canceled' param in login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-at-kickstarter committed Mar 11, 2024
1 parent 0dadd81 commit 2e2db3e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public final class LoginToutViewController: UIViewController, MFMailComposeViewC
)
alert.addAction(UIAlertAction(title: Strings.login_errors_button_ok(), style: .cancel))
self?.present(alert, animated: true)
case .cancelled:
case .canceled:
// Do nothing
break
}
Expand Down
20 changes: 18 additions & 2 deletions Library/OAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import KsApi
public enum OAuthAuthorizationResult {
case loggedIn
case failure(errorMessage: String)
case cancelled
case canceled
}

public struct OAuth {
Expand Down Expand Up @@ -76,7 +76,7 @@ public struct OAuth {
if let authenticationError = error as? ASWebAuthenticationSessionError,
authenticationError.code == .canceledLogin {
DispatchQueue.main.async {
onComplete(.cancelled)
onComplete(.canceled)
}
} else {
DispatchQueue.main.async {
Expand All @@ -87,6 +87,13 @@ public struct OAuth {
return
}

guard !self.isRedirectURLCanceled(url) else {
DispatchQueue.main.async {
onComplete(.canceled)
}
return
}

guard let code = codeFromRedirectURL(url) else {
DispatchQueue.main.async {
onComplete(.failure(errorMessage: Strings.Something_went_wrong_please_try_again()))
Expand Down Expand Up @@ -132,4 +139,13 @@ public struct OAuth {
let components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: false)
return components?.queryItems?.first(where: { $0.name == "code" })?.value
}

private static func isRedirectURLCanceled(_ url: URL?) -> Bool {
guard let redirectURL = url else {
return false
}

let components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: false)
return components?.queryItems?.first(where: { $0.name == "canceled" && $0.value == "true" }) != nil
}
}
18 changes: 16 additions & 2 deletions Library/OAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class OAuthTests: XCTestCase {
}
}

func testHandleRedirect_missingRedirectCode_fails() {
func testHandleRedirect_missingRedirectCodeWithNoCancelParam_fails() {
self.verifyRedirectAsync(
redirectURL: URL(string: "ksrauth2://authenticate?foo=bar"),
error: nil,
Expand All @@ -43,10 +43,24 @@ final class OAuthTests: XCTestCase {
}
}

func testHandleRedirect_missingRedirectCodeAndIncludesCancelParam_cancels() {
self.verifyRedirectAsync(
redirectURL: URL(string: "ksrauth2://authenticate?canceled=true"),
error: nil,
verifier: ""
) { result in
if case .canceled = result {
// Success
} else {
XCTFail("Expected call to be cancelled")
}
}
}

func testHandleRedirect_cancellationError_cancels() {
let cancelledError = ASWebAuthenticationSessionError(.canceledLogin)
verifyRedirectAsync(redirectURL: nil, error: cancelledError, verifier: "") { result in
if case .cancelled = result {
if case .canceled = result {
// Success
} else {
XCTFail("Expected call to be canceled")
Expand Down

0 comments on commit 2e2db3e

Please sign in to comment.