2020-07-30 01:50:30 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
class AddIdentityViewModel: ObservableObject {
|
|
|
|
@Published var urlFieldText = ""
|
|
|
|
@Published var alertItem: AlertItem?
|
|
|
|
@Published private(set) var loading = false
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentityID: AnyPublisher<String, Never>
|
2020-07-30 01:50:30 +02:00
|
|
|
|
2020-08-03 00:23:01 +02:00
|
|
|
private let environment: AppEnvironment
|
2020-08-03 17:20:51 +02:00
|
|
|
private let networkClient: MastodonClient
|
2020-08-03 00:23:01 +02:00
|
|
|
private let webAuthSessionContextProvider = WebAuthSessionContextProvider()
|
2020-08-03 17:20:51 +02:00
|
|
|
private let addedIdentityIDInput = PassthroughSubject<String, Never>()
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2020-07-30 01:50:30 +02:00
|
|
|
|
2020-08-03 17:20:51 +02:00
|
|
|
init(environment: AppEnvironment) {
|
2020-08-03 00:23:01 +02:00
|
|
|
self.environment = environment
|
2020-08-03 17:20:51 +02:00
|
|
|
self.networkClient = MastodonClient(configuration: environment.URLSessionConfiguration)
|
|
|
|
addedIdentityID = addedIdentityIDInput.eraseToAnyPublisher()
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func goTapped() {
|
|
|
|
let identityID = UUID().uuidString
|
|
|
|
let instanceURL: URL
|
|
|
|
let redirectURL: URL
|
|
|
|
|
|
|
|
do {
|
|
|
|
instanceURL = try urlFieldText.url()
|
|
|
|
redirectURL = try identityID.url(scheme: MastodonAPI.OAuth.callbackURLScheme)
|
|
|
|
} catch {
|
|
|
|
alertItem = AlertItem(error: error)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizeApp(
|
|
|
|
identityID: identityID,
|
|
|
|
instanceURL: instanceURL,
|
|
|
|
redirectURL: redirectURL,
|
2020-08-03 00:23:01 +02:00
|
|
|
secrets: environment.secrets)
|
2020-07-30 01:50:30 +02:00
|
|
|
.authenticationURL(instanceURL: instanceURL, redirectURL: redirectURL)
|
|
|
|
.authenticate(
|
2020-08-03 00:23:01 +02:00
|
|
|
webAuthSessionType: environment.webAuthSessionType,
|
|
|
|
contextProvider: webAuthSessionContextProvider,
|
2020-07-30 01:50:30 +02:00
|
|
|
callbackURLScheme: MastodonAPI.OAuth.callbackURLScheme)
|
|
|
|
.extractCode()
|
|
|
|
.requestAccessToken(
|
|
|
|
networkClient: networkClient,
|
|
|
|
identityID: identityID,
|
|
|
|
instanceURL: instanceURL,
|
2020-08-03 00:23:01 +02:00
|
|
|
redirectURL: redirectURL)
|
|
|
|
.createIdentity(id: identityID, instanceURL: instanceURL, environment: environment)
|
2020-07-30 01:50:30 +02:00
|
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
2020-08-02 09:02:03 +02:00
|
|
|
.receive(on: RunLoop.main)
|
2020-07-30 01:50:30 +02:00
|
|
|
.handleEvents(
|
|
|
|
receiveSubscription: { [weak self] _ in self?.loading = true },
|
|
|
|
receiveCompletion: { [weak self] _ in self?.loading = false })
|
2020-08-03 17:20:51 +02:00
|
|
|
.sink(receiveValue: addedIdentityIDInput.send)
|
|
|
|
.store(in: &cancellables)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension AddIdentityViewModel {
|
|
|
|
private func authorizeApp(
|
|
|
|
identityID: String,
|
|
|
|
instanceURL: URL,
|
|
|
|
redirectURL: URL,
|
|
|
|
secrets: Secrets) -> AnyPublisher<AppAuthorization, Error> {
|
|
|
|
let endpoint = AppAuthorizationEndpoint.apps(
|
|
|
|
clientName: MastodonAPI.OAuth.clientName,
|
|
|
|
redirectURI: redirectURL.absoluteString,
|
|
|
|
scopes: MastodonAPI.OAuth.scopes,
|
|
|
|
website: nil)
|
|
|
|
let target = MastodonTarget(baseURL: instanceURL, endpoint: endpoint, accessToken: nil)
|
|
|
|
|
|
|
|
return networkClient.request(target)
|
|
|
|
.tryMap {
|
|
|
|
try secrets.set($0.clientId, forItem: .clientID, forIdentityID: identityID)
|
|
|
|
try secrets.set($0.clientSecret, forItem: .clientSecret, forIdentityID: identityID)
|
|
|
|
|
|
|
|
return $0
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Publisher where Output == AppAuthorization {
|
2020-08-03 00:23:01 +02:00
|
|
|
func authenticationURL(instanceURL: URL, redirectURL: URL) -> AnyPublisher<(AppAuthorization, URL), Error> {
|
2020-07-30 01:50:30 +02:00
|
|
|
tryMap { appAuthorization in
|
|
|
|
guard var authorizationURLComponents = URLComponents(url: instanceURL, resolvingAgainstBaseURL: true) else {
|
|
|
|
throw URLError(.badURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizationURLComponents.path = "/oauth/authorize"
|
|
|
|
authorizationURLComponents.queryItems = [
|
|
|
|
"client_id": appAuthorization.clientId,
|
|
|
|
"scope": MastodonAPI.OAuth.scopes,
|
|
|
|
"response_type": "code",
|
|
|
|
"redirect_uri": redirectURL.absoluteString
|
|
|
|
].map { URLQueryItem(name: $0, value: $1) }
|
|
|
|
|
|
|
|
guard let authorizationURL = authorizationURLComponents.url else {
|
|
|
|
throw URLError(.badURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (appAuthorization, authorizationURL)
|
|
|
|
}
|
|
|
|
.mapError { $0 as Error }
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Publisher where Output == (AppAuthorization, URL), Failure == Error {
|
|
|
|
func authenticate(
|
2020-08-03 00:23:01 +02:00
|
|
|
webAuthSessionType: WebAuthSession.Type,
|
|
|
|
contextProvider: WebAuthSessionContextProvider,
|
2020-07-30 01:50:30 +02:00
|
|
|
callbackURLScheme: String) -> AnyPublisher<(AppAuthorization, URL), Error> {
|
|
|
|
flatMap { appAuthorization, url in
|
2020-08-03 00:23:01 +02:00
|
|
|
webAuthSessionType.publisher(
|
2020-07-30 01:50:30 +02:00
|
|
|
url: url,
|
|
|
|
callbackURLScheme: callbackURLScheme,
|
|
|
|
presentationContextProvider: contextProvider)
|
|
|
|
.tryCatch { error -> AnyPublisher<URL?, Error> in
|
2020-08-03 00:23:01 +02:00
|
|
|
if (error as? WebAuthSessionError)?.code == .canceledLogin {
|
2020-07-30 01:50:30 +02:00
|
|
|
return Just(nil).setFailureType(to: Error.self).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
.compactMap { $0 }
|
|
|
|
.map { (appAuthorization, $0) }
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Publisher where Output == (AppAuthorization, URL) {
|
2020-08-03 00:23:01 +02:00
|
|
|
func extractCode() -> AnyPublisher<(AppAuthorization, String), Error> {
|
|
|
|
tryMap { appAuthorization, url -> (AppAuthorization, String) in
|
2020-07-30 01:50:30 +02:00
|
|
|
guard let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: true)?.queryItems,
|
|
|
|
let code = queryItems.first(where: { $0.name == MastodonAPI.OAuth.codeCallbackQueryItemName })?.value
|
|
|
|
else { throw MastodonAPI.OAuthError.codeNotFound }
|
|
|
|
|
2020-08-03 00:23:01 +02:00
|
|
|
return (appAuthorization, code)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:23:01 +02:00
|
|
|
private extension Publisher where Output == (AppAuthorization, String), Failure == Error {
|
2020-07-30 01:50:30 +02:00
|
|
|
func requestAccessToken(
|
|
|
|
networkClient: HTTPClient,
|
|
|
|
identityID: String,
|
2020-08-03 00:23:01 +02:00
|
|
|
instanceURL: URL,
|
|
|
|
redirectURL: URL) -> AnyPublisher<AccessToken, Error> {
|
|
|
|
flatMap { appAuthorization, code -> AnyPublisher<AccessToken, Error> in
|
2020-07-30 01:50:30 +02:00
|
|
|
let endpoint = AccessTokenEndpoint.oauthToken(
|
|
|
|
clientID: appAuthorization.clientId,
|
|
|
|
clientSecret: appAuthorization.clientSecret,
|
|
|
|
code: code,
|
|
|
|
grantType: MastodonAPI.OAuth.grantType,
|
|
|
|
scopes: MastodonAPI.OAuth.scopes,
|
2020-08-03 00:23:01 +02:00
|
|
|
redirectURI: redirectURL.absoluteString)
|
2020-07-30 01:50:30 +02:00
|
|
|
let target = MastodonTarget(baseURL: instanceURL, endpoint: endpoint, accessToken: nil)
|
|
|
|
|
|
|
|
return networkClient.request(target)
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Publisher where Output == AccessToken {
|
2020-08-03 00:23:01 +02:00
|
|
|
func createIdentity(id: String, instanceURL: URL, environment: AppEnvironment) -> AnyPublisher<String, Error> {
|
2020-07-30 01:50:30 +02:00
|
|
|
tryMap { accessToken -> (String, URL) in
|
2020-08-03 00:23:01 +02:00
|
|
|
try environment.secrets.set(accessToken.accessToken, forItem: .accessToken, forIdentityID: id)
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
return (id, instanceURL)
|
|
|
|
}
|
2020-08-03 00:23:01 +02:00
|
|
|
.flatMap(environment.identityDatabase.createIdentity)
|
2020-08-04 22:26:09 +02:00
|
|
|
.map { id }
|
2020-07-30 01:50:30 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|