Skip to content

Commit

Permalink
Merge pull request #27 in MI/yandex-checkout-payments-swift from rele…
Browse files Browse the repository at this point in the history
…ase/2.0.7 to master

* commit '67c90efb1a44a414e18c62b9cdcc45db9771b80c':
  [BIOS-367] Fix YandexLogin authorization again
  • Loading branch information
AutoMergeBot authored and AutoMergeBot committed Jun 18, 2019
2 parents 43664e4 + 67c90ef commit 5bbe536
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,23 @@ extension YandexAuthPresenter: YandexAuthInteractorOutput {
}

func didAuthorizeInYandex(error: Error) {
let event: AnalyticsEvent
if case YandexLoginProcessingError.accessDenied = error {
event = .actionYaLoginAuthorization(.canceled)
} else {
event = .actionYaLoginAuthorization(.fail)
}
if case YandexLoginProcessingError.applicationDidBecomeActive = error {
moduleOutput?.didCancelAuthorizeInYandex(on: self)

} else if case YandexLoginProcessingError.accessDenied = error {
moduleOutput?.didCancelAuthorizeInYandex(on: self)

DispatchQueue.global().async { [weak self] in
guard let interactor = self?.interactor else { return }
interactor.trackEvent(event)
DispatchQueue.global().async { [weak self] in
guard let interactor = self?.interactor else { return }
interactor.trackEvent(.actionYaLoginAuthorization(.canceled))
}

} else {
DispatchQueue.global().async { [weak self] in
guard let interactor = self?.interactor else { return }
interactor.trackEvent(.actionYaLoginAuthorization(.fail))
}
}
moduleOutput?.didCancelAuthorizeInYandex(on: self)
}

func didFetchYamoneyPaymentMethods(_ paymentMethods: [PaymentOption]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum YandexLoginProcessingError: Error {
case common(Error)
case wasCanceled
case accessDenied
case applicationDidBecomeActive
}

protocol YandexLoginSdkProcessing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import YandexLoginSDK
/// Service for authorization with Yandex account
public final class YandexLoginService: YandexLoginProcessing, YandexLoginSdkProcessing {

// MARK: - YandexLoginSdkProcessing

/// Activates the Service. Should be called from applicationDidFinishLaunching.
///
/// - Parameter appId: Application ID received on `https://oauth.yandex.ru`
Expand All @@ -25,18 +27,16 @@ public final class YandexLoginService: YandexLoginProcessing, YandexLoginSdkProc
/// - url: Parameter received from `application(:open:options:)`
/// - sourceApplication: Parameter received from `application(:open:options:)`
public static func handleOpen(_ url: URL, sourceApplication: String?) -> Bool {
YandexLoginService.shared.shouldRejectAuthorization = false
return YXLSdk.shared.handleOpen(url, sourceApplication: sourceApplication)
}

private var observer: Observer? {
didSet {
oldValue?.promise.reject(YandexLoginProcessingError.wasCanceled)
oldValue.map(YXLSdk.shared.remove)
observer.map(YXLSdk.shared.add)
}
}
// MARK: - YandexLoginProcessing

func authorize() -> Promise<YandexLoginResponse> {
YandexLoginService.shared.shouldRejectAuthorization = true
subscribeOnNotifications()

let observer = Observer()
self.observer = observer
YXLSdk.shared.authorize()
Expand All @@ -47,6 +47,39 @@ public final class YandexLoginService: YandexLoginProcessing, YandexLoginSdkProc
YXLSdk.shared.logout()
}

// MARK: - Private

private static var shared = YandexLoginService()

private var shouldRejectAuthorization: Bool?

private var observer: Observer? {
didSet {
oldValue?.promise.reject(YandexLoginProcessingError.wasCanceled)
oldValue.map(YXLSdk.shared.remove)
observer.map(YXLSdk.shared.add)
}
}

private func subscribeOnNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}

private func unsubscribeFromNotifications() {
NotificationCenter.default.removeObserver(self)
}

@objc
private func applicationDidBecomeActive() {
unsubscribeFromNotifications()
if YandexLoginService.shared.shouldRejectAuthorization == true {
observer?.promise.reject(YandexLoginProcessingError.applicationDidBecomeActive)
}
}

private final class Observer: NSObject, YXLObserver {
var promise = Promise<YandexLoginResponse>()

Expand Down Expand Up @@ -90,11 +123,11 @@ private func makeDisplayNameFromJwt(_ jwt: String) -> String? {
let data = base64UrlDecode(components[payloadJwtFragment]),
let json = try? JSONSerialization.jsonObject(with: data),
let payloadJson = json as? [String: Any],
let dispayName = payloadJson[displayNameKey] as? String else {
let displayName = payloadJson[displayNameKey] as? String else {
return nil
}

return dispayName
return displayName
}

private func base64UrlDecode(_ value: String) -> Data? {
Expand Down

0 comments on commit 5bbe536

Please sign in to comment.