From c64bdc72d4c26d2723e328743e360c54ca47e24c Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Wed, 8 May 2024 18:12:52 +0200 Subject: [PATCH] Add authorization to instance-calls (IOS-264) This will improve using the app with `LIMITED_FEDERATION_INSTANCES` --- .../MastodonPickServerViewController.swift | 2 +- .../MastodonPickServerViewModel.swift | 2 +- ...astodonServerRulesViewController+Debug.swift | 2 +- .../Welcome/WelcomeViewController.swift | 2 +- .../Scene/Report/Report/ReportViewModel.swift | 2 +- .../Scene/Settings/SettingsCoordinator.swift | 4 ++-- .../Service/API/APIService+Instance.swift | 17 +++++++++++------ .../MastodonCore/Service/InstanceService.swift | 6 +++--- .../MastodonSDK/API/Mastodon+API+Instance.swift | 6 ++++-- .../API/Mastodon+API+V2+Instance.swift | 3 ++- .../Sources/MastodonSDK/API/Mastodon+API.swift | 2 +- .../Mastodon+Entity+ExtendedDescription.swift | 2 +- .../ActionRequestHandler.swift | 8 +++++++- 13 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift index 886b138b1..4395d1104 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift @@ -272,7 +272,7 @@ extension MastodonPickServerViewController { authenticationViewModel.isAuthenticating.send(true) - context.apiService.instance(domain: server.domain) + context.apiService.instance(domain: server.domain, authenticationBox: nil) .compactMap { [weak self] response -> AnyPublisher? in guard let self = self else { return nil } guard response.value.registrations != false else { diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift index a9f760a52..f0278bb3e 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift @@ -167,7 +167,7 @@ extension MastodonPickServerViewModel { self.unindexedServers.value = nil return self.context.apiService.webFinger(domain: domain) .flatMap { domain -> AnyPublisher, Error>, Never> in - return self.context.apiService.instance(domain: domain) + return self.context.apiService.instance(domain: domain, authenticationBox: nil) .map { response -> Result, Error>in let newResponse = response.map { [Mastodon.Entity.Server(domain: domain, instance: $0)] } return Result.success(newResponse) diff --git a/Mastodon/Scene/Onboarding/ServerRules/MastodonServerRulesViewController+Debug.swift b/Mastodon/Scene/Onboarding/ServerRules/MastodonServerRulesViewController+Debug.swift index 7477d3bc5..0cfde37dd 100644 --- a/Mastodon/Scene/Onboarding/ServerRules/MastodonServerRulesViewController+Debug.swift +++ b/Mastodon/Scene/Onboarding/ServerRules/MastodonServerRulesViewController+Debug.swift @@ -22,7 +22,7 @@ extension MastodonRegisterViewController { viewController.context = context viewController.coordinator = coordinator - let instanceResponse = try await context.apiService.instance(domain: domain).singleOutput() + let instanceResponse = try await context.apiService.instance(domain: domain, authenticationBox: nil).singleOutput() let applicationResponse = try await context.apiService.createApplication(domain: domain).singleOutput() let accessTokenResponse = try await context.apiService.applicationAccessToken( domain: domain, diff --git a/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift b/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift index da97a7888..498e99d53 100644 --- a/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift +++ b/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift @@ -283,7 +283,7 @@ extension WelcomeViewController { authenticationViewModel.isAuthenticating.send(true) - context.apiService.instance(domain: server.domain) + context.apiService.instance(domain: server.domain, authenticationBox: nil) .compactMap { [weak self] response -> AnyPublisher? in guard let self = self else { return nil } guard response.value.registrations != false else { diff --git a/Mastodon/Scene/Report/Report/ReportViewModel.swift b/Mastodon/Scene/Report/Report/ReportViewModel.swift index cff16063a..c8896ef4f 100644 --- a/Mastodon/Scene/Report/Report/ReportViewModel.swift +++ b/Mastodon/Scene/Report/Report/ReportViewModel.swift @@ -67,7 +67,7 @@ class ReportViewModel { // bind server rules Task { @MainActor in do { - let response = try await context.apiService.instance(domain: authContext.mastodonAuthenticationBox.domain) + let response = try await context.apiService.instance(domain: authContext.mastodonAuthenticationBox.domain, authenticationBox: authContext.mastodonAuthenticationBox) .timeout(3, scheduler: DispatchQueue.main) .singleOutput() let rules = response.value.rules ?? [] diff --git a/Mastodon/Scene/Settings/SettingsCoordinator.swift b/Mastodon/Scene/Settings/SettingsCoordinator.swift index c465ca139..502598a2f 100644 --- a/Mastodon/Scene/Settings/SettingsCoordinator.swift +++ b/Mastodon/Scene/Settings/SettingsCoordinator.swift @@ -75,7 +75,7 @@ extension SettingsCoordinator: SettingsViewControllerDelegate { let serverDetailsViewController = ServerDetailsViewController(domain: domain, appContext: appContext, authContext: authContext, sceneCoordinator: sceneCoordinator) serverDetailsViewController.delegate = self - appContext.apiService.instanceV2(domain: domain) + appContext.apiService.instanceV2(domain: domain, authenticationBox: authContext.mastodonAuthenticationBox) .sink { _ in } receiveValue: { content in @@ -83,7 +83,7 @@ extension SettingsCoordinator: SettingsViewControllerDelegate { } .store(in: &disposeBag) - appContext.apiService.extendedDescription(domain: domain) + appContext.apiService.extendedDescription(domain: domain, authenticationBox: authContext.mastodonAuthenticationBox) .sink { _ in } receiveValue: { content in diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift index 03a63461e..140c37157 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Instance.swift @@ -14,18 +14,23 @@ import MastodonSDK extension APIService { public func instance( - domain: String + domain: String, + authenticationBox: MastodonAuthenticationBox? ) -> AnyPublisher, Error> { - return Mastodon.API.Instance.instance(session: session, domain: domain) + return Mastodon.API.Instance.instance(session: session, authorization: authenticationBox?.userAuthorization, domain: domain) } public func instanceV2( - domain: String + domain: String, + authenticationBox: MastodonAuthenticationBox? ) -> AnyPublisher, Error> { - return Mastodon.API.V2.Instance.instance(session: session, domain: domain) + return Mastodon.API.V2.Instance.instance(session: session, authorization: authenticationBox?.userAuthorization, domain: domain) } - public func extendedDescription(domain: String) -> AnyPublisher, Error> { - return Mastodon.API.Instance.extendedDescription(session: session, domain: domain) + public func extendedDescription( + domain: String, + authenticationBox: MastodonAuthenticationBox? + ) -> AnyPublisher, Error> { + return Mastodon.API.Instance.extendedDescription(session: session, authorization: authenticationBox?.userAuthorization, domain: domain) } } diff --git a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift index fb4335cc3..5088d8124 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift @@ -45,11 +45,11 @@ public final class InstanceService { extension InstanceService { func updateInstance(domain: String) { - guard let apiService = self.apiService else { return } - apiService.instance(domain: domain) + guard let apiService else { return } + apiService.instance(domain: domain, authenticationBox: authenticationService?.mastodonAuthenticationBoxes.first) .flatMap { [unowned self] response -> AnyPublisher in if response.value.version?.majorServerVersion(greaterThanOrEquals: 4) == true { - return apiService.instanceV2(domain: domain) + return apiService.instanceV2(domain: domain, authenticationBox: authenticationService?.mastodonAuthenticationBoxes.first) .flatMap { return self.updateInstanceV2(domain: domain, response: $0) } .eraseToAnyPublisher() } else { diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift index 871f43632..8cf9430cd 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Instance.swift @@ -28,9 +28,10 @@ extension Mastodon.API.Instance { /// - Returns: `AnyPublisher` contains `Instance` nested in the response public static func instance( session: URLSession, + authorization: Mastodon.API.OAuth.Authorization?, domain: String ) -> AnyPublisher, Error> { - let request = Mastodon.API.get(url: instanceEndpointURL(domain: domain)) + let request = Mastodon.API.get(url: instanceEndpointURL(domain: domain), authorization: authorization) return session.dataTaskPublisher(for: request) .tryMap { data, response in let value: Mastodon.Entity.Instance @@ -62,9 +63,10 @@ extension Mastodon.API.Instance { /// [Document](https://docs.joinmastodon.org/methods/instance/#extended_description) public static func extendedDescription( session: URLSession, + authorization: Mastodon.API.OAuth.Authorization?, domain: String ) -> AnyPublisher, Error> { - let request = Mastodon.API.get(url: extendedDescriptionEndpointURL(domain: domain)) + let request = Mastodon.API.get(url: extendedDescriptionEndpointURL(domain: domain), authorization: authorization) return session.dataTaskPublisher(for: request) .tryMap { data, response in let value = try Mastodon.API.decode(type: Mastodon.Entity.ExtendedDescription.self, from: data, response: response) diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift index e276fddba..1077b285e 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+V2+Instance.swift @@ -21,12 +21,13 @@ extension Mastodon.API.V2.Instance { /// - Returns: `AnyPublisher` contains `Instance` nested in the response public static func instance( session: URLSession, + authorization: Mastodon.API.OAuth.Authorization?, domain: String ) -> AnyPublisher, Error> { let request = Mastodon.API.get( url: instanceEndpointURL(domain: domain), query: nil, - authorization: nil + authorization: authorization ) return session.dataTaskPublisher(for: request) .tryMap { data, response in diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift index 8005ebfa1..b163a364b 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift @@ -211,7 +211,7 @@ extension Mastodon.API { return try Mastodon.API.decoder.decode(type, from: data) } catch let decodeError { #if DEBUG - debugPrint("\(response.url), Data: \(String(data: data, encoding: .utf8)), \(decodeError)") + debugPrint("URL: \(String(describing: response.url))\nData: \(String(data: data, encoding: .utf8) ?? "-")\nError:\(decodeError)\n----\n") #endif guard let httpURLResponse = response as? HTTPURLResponse else { diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ExtendedDescription.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ExtendedDescription.swift index c42fe6ab1..337e54af9 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ExtendedDescription.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ExtendedDescription.swift @@ -8,7 +8,7 @@ extension Mastodon.Entity { /// ## Reference: /// [Document](https://docs.joinmastodon.org/entities/ExtendedDescription/) public struct ExtendedDescription: Codable { - public let updatedAt: Date + public let updatedAt: Date? public let content: String enum CodingKeys: String, CodingKey { diff --git a/OpenInActionExtension/ActionRequestHandler.swift b/OpenInActionExtension/ActionRequestHandler.swift index 69215dcb2..813913027 100644 --- a/OpenInActionExtension/ActionRequestHandler.swift +++ b/OpenInActionExtension/ActionRequestHandler.swift @@ -110,7 +110,12 @@ private extension ActionRequestHandler { func continueWithSearch(_ query: String) { guard let url = URL(string: query), - let host = url.host + let host = url.host, + let activeAuthenticationBox = Self.appContext + .authenticationService + .mastodonAuthenticationBoxes + .first + else { return doneWithInvalidLink() } @@ -119,6 +124,7 @@ private extension ActionRequestHandler { .Instance .instance( session: .shared, + authorization: activeAuthenticationBox.userAuthorization, domain: host ) .receive(on: DispatchQueue.main)