2020-08-02 22:23:01 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import AuthenticationServices
|
|
|
|
import Combine
|
2020-09-05 02:31:43 +00:00
|
|
|
import Foundation
|
2020-08-02 22:23:01 +00:00
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public protocol WebAuthSession: AnyObject {
|
2020-08-02 22:23:01 +00:00
|
|
|
init(url URL: URL,
|
|
|
|
callbackURLScheme: String?,
|
|
|
|
completionHandler: @escaping WebAuthSessionCompletionHandler)
|
|
|
|
var presentationContextProvider: WebAuthPresentationContextProviding? { get set }
|
|
|
|
@discardableResult func start() -> Bool
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:01:21 +00:00
|
|
|
extension WebAuthSession {
|
2020-08-02 22:23:01 +00:00
|
|
|
static func publisher(
|
|
|
|
url: URL,
|
|
|
|
callbackURLScheme: String?,
|
2020-09-09 01:02:55 +00:00
|
|
|
presentationContextProvider: WebAuthPresentationContextProviding) -> AnyPublisher<URL, Error> {
|
|
|
|
Future<URL, Error> { promise in
|
2020-08-02 22:23:01 +00:00
|
|
|
let webAuthSession = Self(
|
|
|
|
url: url,
|
|
|
|
callbackURLScheme: callbackURLScheme) { oauthCallbackURL, error in
|
|
|
|
if let error = error {
|
2021-01-05 22:38:15 +00:00
|
|
|
promise(.failure(error))
|
|
|
|
} else if let oauthCallbackURL = oauthCallbackURL {
|
|
|
|
promise(.success(oauthCallbackURL))
|
|
|
|
} else {
|
|
|
|
promise(.failure(URLError(.unknown)))
|
2020-08-02 22:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:33:49 +00:00
|
|
|
DispatchQueue.main.async {
|
2022-11-08 22:04:34 +00:00
|
|
|
webAuthSession.presentationContextProvider = presentationContextProvider
|
2020-09-01 07:33:49 +00:00
|
|
|
webAuthSession.start()
|
|
|
|
}
|
2020-08-02 22:23:01 +00:00
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:22:20 +00:00
|
|
|
final class WebAuthSessionContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
|
2020-08-02 22:23:01 +00:00
|
|
|
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
|
|
ASPresentationAnchor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public typealias WebAuthSessionCompletionHandler = ASWebAuthenticationSession.CompletionHandler
|
|
|
|
public typealias WebAuthSessionError = ASWebAuthenticationSessionError
|
|
|
|
public typealias WebAuthPresentationContextProviding = ASWebAuthenticationPresentationContextProviding
|
|
|
|
public typealias LiveWebAuthSession = ASWebAuthenticationSession
|
2020-08-02 22:23:01 +00:00
|
|
|
|
2020-08-12 09:01:21 +00:00
|
|
|
extension LiveWebAuthSession: WebAuthSession {}
|