From afe57f28244eed3d11f5358a01c0a471f1c94f86 Mon Sep 17 00:00:00 2001 From: lmcmz Date: Fri, 24 Sep 2021 02:07:44 +1000 Subject: [PATCH] Remove combine temporary --- Example/FCLDemo/ViewModel.swift | 10 -- .../FlowAuthCombine.swift | 141 ------------------ 2 files changed, 151 deletions(-) delete mode 100644 Sources/FlowAuthenticationService/FlowAuthCombine.swift diff --git a/Example/FCLDemo/ViewModel.swift b/Example/FCLDemo/ViewModel.swift index 4ee1c3f..577071b 100644 --- a/Example/FCLDemo/ViewModel.swift +++ b/Example/FCLDemo/ViewModel.swift @@ -32,16 +32,6 @@ class ViewModel: ObservableObject { } } } - -// FAuthentication.shared.authenticate() -// .receive(on: DispatchQueue.main) -// .sink { completion in -// if case let .failure(error) = completion { -// self.address = error.localizedDescription -// } -// } receiveValue: { data in -// self.address = data.address -// }.store(in: &cancellables) } } diff --git a/Sources/FlowAuthenticationService/FlowAuthCombine.swift b/Sources/FlowAuthenticationService/FlowAuthCombine.swift deleted file mode 100644 index 26ccecf..0000000 --- a/Sources/FlowAuthenticationService/FlowAuthCombine.swift +++ /dev/null @@ -1,141 +0,0 @@ -import AuthenticationServices -import Combine -import Foundation - -// Using combine, only available for iOS 13+ -public class FAuthentication: NSObject { - public static let shared = FAuthentication() - private var cancellables = Set() - private var canContinue = true - private var session: ASWebAuthenticationSession? - - public func authenticate() -> AnyPublisher { - let url = URL(string: "https://dapper-http-post.vercel.app/api/authn")! - return execHttpPost(url: url) - .tryMap { response in - guard let address = response.data?.addr else { - throw FlowError.invalidResponse - } - return FlowData(address: address) - } - .eraseToAnyPublisher() - } - - private func fetchService(url: URL) -> AnyPublisher { - var request = URLRequest(url: url) - request.httpMethod = "POST" - let decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - // TODO: Need to check extract config - let config = URLSessionConfiguration.default - return URLSession(configuration: config).dataTaskPublisher(for: request) - .map { $0.data } - .decode(type: AuthnResponse.self, decoder: decoder) - .eraseToAnyPublisher() - } - - private func execHttpPost(url: URL) -> Future { - return Future { promise in - self.fetchService(url: url) - .sink { completion in - if case let .failure(error) = completion { - print(error) - } - } receiveValue: { result in - switch result.status { - case .approved: - promise(.success(result)) - case .declined: - promise(.failure(FlowError.declined)) - case .pending: - self.canContinue = true - guard let local = result.local, let updates = result.updates else { - promise(.failure(FlowError.generic)) - return - } - guard let url = URL(string: local.endpoint) else { - promise(.failure(FlowError.urlInvaild)) - return - } - self.openAuthenticationSession(url: url) - self.poll(service: updates).sink { completion in - if case let .failure(error) = completion { - promise(.failure(error)) - } - } receiveValue: { result in - promise(.success(result)) - }.store(in: &self.cancellables) - } - }.store(in: &self.cancellables) - } - } - - private func poll(service: Service) -> Future { - return Future { promise in - - if !self.canContinue { - promise(.failure(FlowError.declined)) - return - } - - guard let url = URL(string: service.endpoint) else { - promise(.failure(FlowError.urlInvaild)) - return - } - - self.fetchService(url: url) - .sink { completion in - if case let .failure(error) = completion { - print(error) - } - } receiveValue: { result in - print("polling ---> \(result.status.rawValue)") - switch result.status { - case .approved: - self.closeSession() - promise(.success(result)) - case .declined: - promise(.success(result)) - case .pending: - // TODO: Improve this - DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(500)) { - self.poll(service: service) - .sink { completion in - if case let .failure(error) = completion { - promise(.failure(error)) - } - } receiveValue: { result in - promise(.success(result)) - } - .store(in: &self.cancellables) - } - } - }.store(in: &self.cancellables) - } - } - - private func openAuthenticationSession(url: URL) { - DispatchQueue.main.async { - let session = ASWebAuthenticationSession(url: url, - callbackURLScheme: "fclDemo") { _, _ in - self.canContinue = false - } - self.session = session - session.presentationContextProvider = self - session.prefersEphemeralWebBrowserSession = true - session.start() - } - } - - private func closeSession() { - DispatchQueue.main.async { - self.session?.cancel() - } - } -} - -extension FAuthentication: ASWebAuthenticationPresentationContextProviding { - public func presentationAnchor(for _: ASWebAuthenticationSession) -> ASPresentationAnchor { - return ASPresentationAnchor() - } -}