2020-07-30 01:50:30 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
2020-08-31 20:57:02 +02:00
|
|
|
import ServiceLayer
|
2020-07-30 01:50:30 +02:00
|
|
|
|
2020-08-31 20:57:02 +02:00
|
|
|
public class MockWebAuthSession: WebAuthSession {
|
2020-08-03 00:23:01 +02:00
|
|
|
let completionHandler: WebAuthSessionCompletionHandler
|
2020-07-30 01:50:30 +02:00
|
|
|
let url: URL
|
|
|
|
let callbackURLScheme: String?
|
2020-08-31 20:57:02 +02:00
|
|
|
public var presentationContextProvider: WebAuthPresentationContextProviding?
|
2020-07-30 01:50:30 +02:00
|
|
|
|
2020-08-31 20:57:02 +02:00
|
|
|
public required init(
|
2020-07-30 01:50:30 +02:00
|
|
|
url URL: URL,
|
|
|
|
callbackURLScheme: String?,
|
2020-08-03 00:23:01 +02:00
|
|
|
completionHandler: @escaping WebAuthSessionCompletionHandler) {
|
2020-07-30 01:50:30 +02:00
|
|
|
self.url = URL
|
|
|
|
self.callbackURLScheme = callbackURLScheme
|
|
|
|
self.completionHandler = completionHandler
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:57:02 +02:00
|
|
|
public func start() -> Bool {
|
2020-07-30 01:50:30 +02:00
|
|
|
completionHandler(completionHandlerURL, completionHandlerError)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var completionHandlerURL: URL? {
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var completionHandlerError: Error? {
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:57:02 +02:00
|
|
|
public class SuccessfulMockWebAuthSession: MockWebAuthSession {
|
2020-07-30 01:50:30 +02:00
|
|
|
private let redirectURL: URL
|
|
|
|
|
|
|
|
required init(
|
|
|
|
url URL: URL,
|
|
|
|
callbackURLScheme: String?,
|
2020-08-03 00:23:01 +02:00
|
|
|
completionHandler: @escaping WebAuthSessionCompletionHandler) {
|
2020-07-30 01:50:30 +02:00
|
|
|
redirectURL = Foundation.URL(
|
|
|
|
string: URLComponents(url: URL, resolvingAgainstBaseURL: true)!
|
|
|
|
.queryItems!.first(where: { $0.name == "redirect_uri" })!.value!)!
|
|
|
|
super.init(
|
|
|
|
url: URL,
|
|
|
|
callbackURLScheme: callbackURLScheme,
|
|
|
|
completionHandler: completionHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
override var completionHandlerURL: URL? {
|
|
|
|
var components = URLComponents(url: redirectURL, resolvingAgainstBaseURL: true)!
|
|
|
|
var queryItems = components.queryItems ?? []
|
|
|
|
|
|
|
|
queryItems.append(URLQueryItem(name: "code", value: UUID().uuidString))
|
|
|
|
components.queryItems = queryItems
|
|
|
|
|
|
|
|
return components.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:57:02 +02:00
|
|
|
public class CanceledLoginMockWebAuthSession: MockWebAuthSession {
|
2020-07-30 01:50:30 +02:00
|
|
|
override var completionHandlerError: Error? {
|
2020-08-03 00:23:01 +02:00
|
|
|
WebAuthSessionError(.canceledLogin)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|