Skip to content

Commit

Permalink
Merge pull request OAuthSwift#658 from OAuthSwift/fix/macCatalyst
Browse files Browse the repository at this point in the history
Fix Mac catalyst compilation
  • Loading branch information
phimage authored May 19, 2021
2 parents dbb74fa + 2fc57d0 commit 89e821a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ jobs:
swift build
- name: Xcode build macOS
run: |
xcodebuild -scheme "OAuthSwiftMacOS"
xcodebuild -scheme "OAuthSwiftMacOS" | xcpretty -c; exit ${PIPESTATUS[0]}
- name: Swift build iOS
run: |
sdk=`xcrun -sdk iphonesimulator -show-sdk-path`
sdkVersion=`echo $sdk | sed -E 's/.*iPhoneSimulator(.*)\.sdk/\1/'`
swift build -Xswiftc "-sdk" -Xswiftc "$sdk" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios$sdkVersion-simulator"
- name: Xcode build iOS
run: |
xcodebuild -scheme "OAuthSwift"
xcodebuild -scheme "OAuthSwift" -sdk iphonesimulator | xcpretty -c; exit ${PIPESTATUS[0]}
- name: Xcode build mac Catalyst
run: |
xcodebuild -scheme "OAuthSwift" | xcpretty -c; exit ${PIPESTATUS[0]}
50 changes: 27 additions & 23 deletions Sources/OAuth2Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ open class OAuth2Swift: OAuthSwift {
}

// MARK: functions

/// Handling SFAuthenticationSession/ASWebAuthenticationSession canceledLogin errors
fileprivate func isCancelledError(_ responseParameters: [String: String]) -> Bool {
guard let domain = responseParameters["error_domain"],
let codeString = responseParameters["error_code"],
let code = Int(codeString) else {
return false
}

#if targetEnvironment(macCatalyst) || os(iOS)
if #available(iOS 13.0, macCatalyst 13.0, *),
ASWebAuthenticationURLHandler.isCancelledError(domain: domain, code: code) {
return true
}
#if !targetEnvironment(macCatalyst)
if #available(iOS 11, *),
SFAuthenticationURLHandler.isCancelledError(domain: domain, code: code) {
return true
}
#endif
#endif
return false
}

@discardableResult
open func authorize(withCallbackURL callbackURL: URLConvertible?, scope: String, state: String, parameters: Parameters = [:], headers: OAuthSwift.Headers? = nil, completionHandler completion: @escaping TokenCompletionHandler) -> OAuthSwiftRequestHandle? {

Expand Down Expand Up @@ -125,34 +149,14 @@ open class OAuth2Swift: OAuthSwift {
this.putHandle(handle, withKey: UUID().uuidString)
}
} else if let error = responseParameters["error"] {
let otherErrorBlock = {
if this.isCancelledError(responseParameters) {
completion(.failure(.cancelled))
} else {
let description = responseParameters["error_description"] ?? ""
let message = NSLocalizedString(error, comment: description)
OAuthSwift.log?.error("Authorization failed with: \(description)")
completion(.failure(.serverError(message: message)))
}

// handling SFAuthenticationSession/ASWebAuthenticationSession canceledLogin errors
if let domain = responseParameters["error_domain"],
let codeString = responseParameters["error_code"],
let code = Int(codeString) {

#if targetEnvironment(macCatalyst) || os(iOS)
if #available(iOS 13.0, macCatalyst 13.0, *),
ASWebAuthenticationURLHandler.isCancelledError(domain: domain, code: code) {
completion(.failure(.cancelled))
} else if #available(iOS 11, *),
SFAuthenticationURLHandler.isCancelledError(domain: domain, code: code) {
completion(.failure(.cancelled))
} else {
otherErrorBlock()
}
#else
otherErrorBlock()
#endif
} else {
otherErrorBlock()
}
} else {
let message = "No access_token, no code and no error provided by server"
OAuthSwift.log?.error("Authorization failed with: \(message)")
Expand Down

0 comments on commit 89e821a

Please sign in to comment.