2020-08-03 00:23:01 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import AuthenticationServices
|
|
|
|
import Combine
|
|
|
|
|
2020-08-09 03:37:46 +02:00
|
|
|
protocol WebAuthSessionType: AnyObject {
|
2020-08-03 00:23:01 +02:00
|
|
|
init(url URL: URL,
|
|
|
|
callbackURLScheme: String?,
|
|
|
|
completionHandler: @escaping WebAuthSessionCompletionHandler)
|
|
|
|
var presentationContextProvider: WebAuthPresentationContextProviding? { get set }
|
|
|
|
@discardableResult func start() -> Bool
|
|
|
|
}
|
|
|
|
|
2020-08-09 03:37:46 +02:00
|
|
|
extension WebAuthSessionType {
|
2020-08-03 00:23:01 +02:00
|
|
|
static func publisher(
|
|
|
|
url: URL,
|
|
|
|
callbackURLScheme: String?,
|
|
|
|
presentationContextProvider: WebAuthPresentationContextProviding) -> AnyPublisher<URL?, Error> {
|
|
|
|
Future<URL?, Error> { promise in
|
|
|
|
let webAuthSession = Self(
|
|
|
|
url: url,
|
|
|
|
callbackURLScheme: callbackURLScheme) { oauthCallbackURL, error in
|
|
|
|
if let error = error {
|
|
|
|
return promise(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise(.success(oauthCallbackURL))
|
|
|
|
}
|
|
|
|
|
|
|
|
webAuthSession.presentationContextProvider = presentationContextProvider
|
|
|
|
webAuthSession.start()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebAuthSessionContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
|
|
|
|
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
|
|
ASPresentationAnchor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typealias WebAuthSessionCompletionHandler = ASWebAuthenticationSession.CompletionHandler
|
|
|
|
typealias WebAuthSessionError = ASWebAuthenticationSessionError
|
|
|
|
typealias WebAuthPresentationContextProviding = ASWebAuthenticationPresentationContextProviding
|
2020-08-09 03:37:46 +02:00
|
|
|
typealias WebAuthSession = ASWebAuthenticationSession
|
2020-08-03 00:23:01 +02:00
|
|
|
|
2020-08-09 03:37:46 +02:00
|
|
|
extension WebAuthSession: WebAuthSessionType {}
|