diff --git a/Sources/KukaiCoreSwift/Services/NetworkService.swift b/Sources/KukaiCoreSwift/Services/NetworkService.swift index 3fabf40..f51fd5d 100644 --- a/Sources/KukaiCoreSwift/Services/NetworkService.swift +++ b/Sources/KukaiCoreSwift/Services/NetworkService.swift @@ -216,6 +216,42 @@ public class NetworkService { }.eraseToAnyPublisher() } + /** + Send a HTTP DELETE to a given URL + */ + public func delete(url: URL, completion: @escaping ((Result) -> Void)) { + var request = URLRequest(url: url) + request.addValue("application/json", forHTTPHeaderField: "Accept") + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpMethod = "DELETE" + + urlSession.dataTask(with: request) { (data, response, error) in + if let err = error { + completion(Result.failure(KukaiError.internalApplicationError(error: err))) + } else { + completion(Result.success(true)) + } + }.resume() + NetworkService.logRequestStart(loggingConfig: loggingConfig, fullURL: url) + } + + /** + Send a HTTP DELETE to a given URL + */ + public func delete(url: URL) -> AnyPublisher { + return Future { [weak self] promise in + self?.delete(url: url, completion: { result in + guard let output = try? result.get() else { + let error = (try? result.getError()) ?? KukaiError.unknown() + promise(.failure(error)) + return + } + + promise(.success(output)) + }) + }.eraseToAnyPublisher() + } + func checkForRPCOperationErrors(parsedResponse: Any, withRequestURL: URL?, requestPayload: Data?, responsePayload: Data?, httpStatusCode: Int?) -> KukaiError? { var operations: [OperationResponse] = [] diff --git a/Sources/KukaiCoreSwift/Services/TorusAuthService.swift b/Sources/KukaiCoreSwift/Services/TorusAuthService.swift index ac2f966..a64c80f 100644 --- a/Sources/KukaiCoreSwift/Services/TorusAuthService.swift +++ b/Sources/KukaiCoreSwift/Services/TorusAuthService.swift @@ -250,21 +250,22 @@ public class TorusAuthService: NSObject { completion(Result.failure(KukaiError.internalApplicationError(error: TorusAuthError.missingVerifier))) } + let accessToken = data.userInfo["accessToken"] as? String // Twitter API doesn't give us the bloody "@" handle for some reason. Fetch that first and overwrite the username property with the handle, if found if authType == .twitter { twitterHandleLookup(id: userId ?? "") { [weak self] result in switch result { case .success(let actualUsername): - self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: actualUsername, userId: userId, profile: profile, completion: completion) + self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: actualUsername, userId: userId, profile: profile, accessToken: accessToken, completion: completion) case .failure(_): - self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, completion: completion) + self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, accessToken: accessToken, completion: completion) } } } else { - createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, completion: completion) + createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, accessToken: accessToken, completion: completion) } } catch { @@ -275,14 +276,20 @@ public class TorusAuthService: NSObject { } } - private func createTorusWalletAndContinue(pk: String?, authType: TorusAuthProvider, username: String?, userId: String?, profile: String?, completion: @escaping ((Result) -> Void)) { + private func createTorusWalletAndContinue(pk: String?, authType: TorusAuthProvider, username: String?, userId: String?, profile: String?, accessToken: String?, completion: @escaping ((Result) -> Void)) { guard let privateKeyString = pk, let wallet = TorusWallet(authProvider: authType, username: username, userId: userId, profilePicture: profile, torusPrivateKey: privateKeyString) else { Logger.torus.error("Error torus contained no, or invlaid private key") completion(Result.failure(KukaiError.internalApplicationError(error: TorusAuthError.invalidTorusResponse))) return } - completion(Result.success(wallet)) + if authType == .facebook, let token = accessToken, let url = URL(string: "https://graph.facebook.com/me/permissions?access_token=\(token)") { + networkService.delete(url: url) { result in + completion(Result.success(wallet)) + } + } else { + completion(Result.success(wallet)) + } }