Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SSO error handling when ignore authorization step. #294

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Swifter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class Swifter {

internal struct CallbackNotification {
static let optionsURLKey = "SwifterCallbackNotificationOptionsURLKey"
static let optionsUserCancelKey = "SwifterCallbackNotificationOptionsUserCancelKey"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: optionsUserCancelledKey

}

internal struct DataParameters {
Expand All @@ -107,6 +108,7 @@ public class Swifter {

public var client: SwifterClientProtocol
private var chunkBuffer: String?
internal var appSwitchingObserver: AppSwitchingObserver?

internal var swifterCallbackToken: NSObjectProtocol? {
willSet {
Expand Down
34 changes: 33 additions & 1 deletion Sources/SwifterAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ public extension Swifter {

let nc = NotificationCenter.default
self.swifterCallbackToken = nc.addObserver(forName: .swifterSSOCallback, object: nil, queue: .main) { notification in
if let isCanceled = notification.userInfo?[CallbackNotification.optionsUserCancelKey] as? Bool, isCanceled {
let error = SwifterError(message: "User cancelled login from Twitter App", kind: .cancelled)
failure?(error)
}
self.swifterCallbackToken = nil
self.appSwitchingObserver?.isEnabled = false
guard let url = notification.userInfo?[CallbackNotification.optionsURLKey] as? URL else { return }
guard url.scheme == urlScheme else { return }

Expand All @@ -145,7 +150,7 @@ public extension Swifter {
success?(credentialToken)
}
}

setupAppSwitchingObserver()
let url = URL(string: "twitterauth://authorize?consumer_key=\(client.consumerKey)&consumer_secret=\(client.consumerSecret)&oauth_callback=\(urlScheme)")!
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
if !success {
Expand Down Expand Up @@ -253,4 +258,31 @@ public extension Swifter {
}
}


class AppSwitchingObserver {
var token: NSObjectProtocol?
var isEnabled: Bool = true
var onTrigger: (() -> Void)? = nil

func startObserving() {
token = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { [weak self] (_) in
guard let `self` = self else { return }
guard self.isEnabled else { return }
self.onTrigger?()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if this was written:

guard let _self = self, _self.isEnabled else { return }
_self.onTrigger?()

Alternatively,

guard self?.isEnabled ?? false else { return } 
self?.onTrigger?()

}
}
}

private func setupAppSwitchingObserver() {
let observer = AppSwitchingObserver()
observer.onTrigger = {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a DispatchQueue.asyncAfter here?

let notification = Notification(name: .swifterSSOCallback, object: nil, userInfo: [CallbackNotification.optionsUserCancelKey: true])
NotificationCenter.default.post(notification)
}
}
appSwitchingObserver = observer

observer.startObserving()
}
}