forked from shadowfacts/Tusker
Use async/await for login
This commit is contained in:
parent
27e05cc72d
commit
c36a239f46
|
@ -101,6 +101,19 @@ public class Client {
|
||||||
return task
|
return task
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func run<Result>(_ request: Request<Result>) async throws -> (Result, Pagination?) {
|
||||||
|
return try await withCheckedThrowingContinuation { continuation in
|
||||||
|
run(request) { response in
|
||||||
|
switch response {
|
||||||
|
case let .failure(error):
|
||||||
|
continuation.resume(throwing: error)
|
||||||
|
case let .success(result, pagination):
|
||||||
|
continuation.resume(returning: (result, pagination))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createURLRequest<Result>(request: Request<Result>) -> URLRequest? {
|
func createURLRequest<Result>(request: Request<Result>) -> URLRequest? {
|
||||||
guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true) else { return nil }
|
guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true) else { return nil }
|
||||||
components.path = request.path
|
components.path = request.path
|
||||||
|
@ -117,24 +130,21 @@ public class Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Authorization
|
// MARK: - Authorization
|
||||||
public func registerApp(name: String, redirectURI: String, scopes: [Scope], website: URL? = nil, completion: @escaping Callback<RegisteredApplication>) {
|
public func registerApp(name: String, redirectURI: String, scopes: [Scope], website: URL? = nil) async throws -> RegisteredApplication {
|
||||||
let request = Request<RegisteredApplication>(method: .post, path: "/api/v1/apps", body: ParametersBody([
|
let request = Request<RegisteredApplication>(method: .post, path: "/api/v1/apps", body: ParametersBody([
|
||||||
"client_name" => name,
|
"client_name" => name,
|
||||||
"redirect_uris" => redirectURI,
|
"redirect_uris" => redirectURI,
|
||||||
"scopes" => scopes.scopeString,
|
"scopes" => scopes.scopeString,
|
||||||
"website" => website?.absoluteString
|
"website" => website?.absoluteString
|
||||||
]))
|
]))
|
||||||
run(request) { result in
|
let (application, _) = try await run(request)
|
||||||
defer { completion(result) }
|
self.appID = application.id
|
||||||
guard case let .success(application, _) = result else { return }
|
self.clientID = application.clientID
|
||||||
|
self.clientSecret = application.clientSecret
|
||||||
self.appID = application.id
|
return application
|
||||||
self.clientID = application.clientID
|
|
||||||
self.clientSecret = application.clientSecret
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getAccessToken(authorizationCode: String, redirectURI: String, completion: @escaping Callback<LoginSettings>) {
|
public func getAccessToken(authorizationCode: String, redirectURI: String) async throws -> LoginSettings {
|
||||||
let request = Request<LoginSettings>(method: .post, path: "/oauth/token", body: ParametersBody([
|
let request = Request<LoginSettings>(method: .post, path: "/oauth/token", body: ParametersBody([
|
||||||
"client_id" => clientID,
|
"client_id" => clientID,
|
||||||
"client_secret" => clientSecret,
|
"client_secret" => clientSecret,
|
||||||
|
@ -142,12 +152,9 @@ public class Client {
|
||||||
"code" => authorizationCode,
|
"code" => authorizationCode,
|
||||||
"redirect_uri" => redirectURI
|
"redirect_uri" => redirectURI
|
||||||
]))
|
]))
|
||||||
run(request) { result in
|
let (settings, _) = try await run(request)
|
||||||
defer { completion(result) }
|
self.accessToken = settings.accessToken
|
||||||
guard case let .success(loginSettings, _) = result else { return }
|
return settings
|
||||||
|
|
||||||
self.accessToken = loginSettings.accessToken
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Self
|
// MARK: - Self
|
||||||
|
|
|
@ -2342,6 +2342,7 @@
|
||||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||||
INFOPLIST_FILE = Pachyderm/Info.plist;
|
INFOPLIST_FILE = Pachyderm/Info.plist;
|
||||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||||
|
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
|
||||||
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
LD_RUNPATH_SEARCH_PATHS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
|
@ -2373,6 +2374,7 @@
|
||||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||||
INFOPLIST_FILE = Pachyderm/Info.plist;
|
INFOPLIST_FILE = Pachyderm/Info.plist;
|
||||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||||
|
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
|
||||||
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
LD_RUNPATH_SEARCH_PATHS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
|
@ -2560,7 +2562,7 @@
|
||||||
CURRENT_PROJECT_VERSION = 19;
|
CURRENT_PROJECT_VERSION = 19;
|
||||||
DEVELOPMENT_TEAM = V4WK9KR9U2;
|
DEVELOPMENT_TEAM = V4WK9KR9U2;
|
||||||
INFOPLIST_FILE = Tusker/Info.plist;
|
INFOPLIST_FILE = Tusker/Info.plist;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
|
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
|
||||||
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
LD_RUNPATH_SEARCH_PATHS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
|
@ -2593,7 +2595,7 @@
|
||||||
CURRENT_PROJECT_VERSION = 19;
|
CURRENT_PROJECT_VERSION = 19;
|
||||||
DEVELOPMENT_TEAM = V4WK9KR9U2;
|
DEVELOPMENT_TEAM = V4WK9KR9U2;
|
||||||
INFOPLIST_FILE = Tusker/Info.plist;
|
INFOPLIST_FILE = Tusker/Info.plist;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
|
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
|
||||||
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
"IPHONEOS_DEPLOYMENT_TARGET[sdk=macosx*]" = 14.3;
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
LD_RUNPATH_SEARCH_PATHS = (
|
||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"object": {
|
"object": {
|
||||||
"pins": [
|
"pins": [
|
||||||
{
|
{
|
||||||
"package": "PLCrashReporter",
|
"package": "plcrashreporter",
|
||||||
"repositoryURL": "https://github.com/microsoft/plcrashreporter",
|
"repositoryURL": "https://github.com/microsoft/plcrashreporter",
|
||||||
"state": {
|
"state": {
|
||||||
"branch": null,
|
"branch": null,
|
||||||
|
|
|
@ -65,28 +65,22 @@ class MastodonController: ObservableObject {
|
||||||
return client.run(request, completion: completion)
|
return client.run(request, completion: completion)
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerApp(completion: @escaping (_ clientID: String, _ clientSecret: String) -> Void) {
|
@discardableResult
|
||||||
guard client.clientID == nil,
|
func run<Result>(_ request: Request<Result>) async throws -> (Result, Pagination?) {
|
||||||
client.clientSecret == nil else {
|
return try await client.run(request)
|
||||||
|
|
||||||
completion(client.clientID!, client.clientSecret!)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client.registerApp(name: "Tusker", redirectURI: "tusker://oauth", scopes: [.read, .write, .follow]) { response in
|
|
||||||
guard case let .success(app, _) = response else { fatalError() }
|
|
||||||
self.client.clientID = app.clientID
|
|
||||||
self.client.clientSecret = app.clientSecret
|
|
||||||
completion(app.clientID, app.clientSecret)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func authorize(authorizationCode: String, completion: @escaping (_ accessToken: String) -> Void) {
|
func registerApp() async -> (String, String) {
|
||||||
client.getAccessToken(authorizationCode: authorizationCode, redirectURI: "tusker://oauth") { response in
|
guard client.clientID == nil,
|
||||||
guard case let .success(settings, _) = response else { fatalError() }
|
client.clientSecret == nil else {
|
||||||
self.client.accessToken = settings.accessToken
|
return (client.clientID!, client.clientSecret!)
|
||||||
completion(settings.accessToken)
|
}
|
||||||
}
|
let app = try! await client.registerApp(name: "Tusker", redirectURI: "tusker://oauth", scopes: [.read, .write, .follow])
|
||||||
|
return (app.clientID, app.clientSecret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func authorize(authorizationCode: String) async -> String {
|
||||||
|
return try! await client.getAccessToken(authorizationCode: authorizationCode, redirectURI: "tusker://oauth").accessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOwnAccount(completion: ((Result<Account, Client.Error>) -> Void)? = nil) {
|
func getOwnAccount(completion: ((Result<Account, Client.Error>) -> Void)? = nil) {
|
||||||
|
@ -118,6 +112,12 @@ class MastodonController: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getOwnAccount() async throws -> Account {
|
||||||
|
return try await withCheckedThrowingContinuation { continuation in
|
||||||
|
getOwnAccount(completion: continuation.resume)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getOwnInstance(completion: ((Instance) -> Void)? = nil) {
|
func getOwnInstance(completion: ((Instance) -> Void)? = nil) {
|
||||||
getOwnInstanceInternal(retryAttempt: 0, completion: completion)
|
getOwnInstanceInternal(retryAttempt: 0, completion: completion)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import AuthenticationServices
|
import AuthenticationServices
|
||||||
|
import Pachyderm
|
||||||
|
|
||||||
protocol OnboardingViewControllerDelegate {
|
protocol OnboardingViewControllerDelegate {
|
||||||
func didFinishOnboarding(account: LocalData.UserAccountInfo)
|
func didFinishOnboarding(account: LocalData.UserAccountInfo)
|
||||||
|
@ -41,53 +42,19 @@ class OnboardingViewController: UINavigationController {
|
||||||
instanceSelector.delegate = self
|
instanceSelector.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private func doAuthenticationSession(url: URL) async throws -> URL {
|
||||||
|
return try await withCheckedThrowingContinuation { continuation in
|
||||||
extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate {
|
self.authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "tusker") { url, error in
|
||||||
func didSelectInstance(url instanceURL: URL) {
|
defer {
|
||||||
let mastodonController = MastodonController(instanceURL: instanceURL)
|
DispatchQueue.main.async {
|
||||||
mastodonController.registerApp { (clientID, clientSecret) in
|
self.authenticationSession = nil
|
||||||
|
|
||||||
var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)!
|
|
||||||
components.path = "/oauth/authorize"
|
|
||||||
components.queryItems = [
|
|
||||||
URLQueryItem(name: "client_id", value: clientID),
|
|
||||||
URLQueryItem(name: "response_type", value: "code"),
|
|
||||||
URLQueryItem(name: "scope", value: "read write follow"),
|
|
||||||
URLQueryItem(name: "redirect_uri", value: "tusker://oauth")
|
|
||||||
]
|
|
||||||
let authorizeURL = components.url!
|
|
||||||
|
|
||||||
self.authenticationSession = ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: "tusker") { url, error in
|
|
||||||
guard error == nil,
|
|
||||||
let url = url,
|
|
||||||
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
|
|
||||||
let item = components.queryItems?.first(where: { $0.name == "code" }),
|
|
||||||
let authCode = item.value else { return }
|
|
||||||
|
|
||||||
mastodonController.authorize(authorizationCode: authCode) { (accessToken) in
|
|
||||||
// construct a temporary UserAccountInfo instance for the MastodonController to use to fetch it's own account
|
|
||||||
let tempAccountInfo = LocalData.UserAccountInfo(id: "temp", instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken)
|
|
||||||
mastodonController.accountInfo = tempAccountInfo
|
|
||||||
|
|
||||||
mastodonController.getOwnAccount { (result) in
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
switch result {
|
|
||||||
case let .failure(error):
|
|
||||||
let alert = UIAlertController(title: "Unable to Verify Credentials", message: "Your account could not be fetched at this time: \(error.localizedDescription)", preferredStyle: .alert)
|
|
||||||
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
|
|
||||||
self.present(alert, animated: true)
|
|
||||||
|
|
||||||
case let .success(account):
|
|
||||||
// this needs to happen on the main thread because it publishes a new value for the ObservableObject
|
|
||||||
let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken)
|
|
||||||
mastodonController.accountInfo = accountInfo
|
|
||||||
|
|
||||||
self.onboardingDelegate?.didFinishOnboarding(account: accountInfo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let url = url {
|
||||||
|
continuation.resume(returning: url)
|
||||||
|
} else {
|
||||||
|
continuation.resume(throwing: error!)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
// Prefer ephemeral sessions to make it easier to sign into multiple accounts on the same instance.
|
// Prefer ephemeral sessions to make it easier to sign into multiple accounts on the same instance.
|
||||||
|
@ -97,6 +64,53 @@ extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate {
|
||||||
|
func didSelectInstance(url instanceURL: URL) {
|
||||||
|
async {
|
||||||
|
let mastodonController = MastodonController(instanceURL: instanceURL)
|
||||||
|
let (clientID, clientSecret) = await mastodonController.registerApp()
|
||||||
|
|
||||||
|
var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)!
|
||||||
|
components.path = "/oauth/authorize"
|
||||||
|
components.queryItems = [
|
||||||
|
URLQueryItem(name: "client_id", value: clientID),
|
||||||
|
URLQueryItem(name: "response_type", value: "code"),
|
||||||
|
URLQueryItem(name: "scope", value: "read write follow"),
|
||||||
|
URLQueryItem(name: "redirect_uri", value: "tusker://oauth")
|
||||||
|
]
|
||||||
|
|
||||||
|
guard let url = try? await self.doAuthenticationSession(url: components.url!),
|
||||||
|
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
|
||||||
|
let item = components.queryItems?.first(where: { $0.name == "code" }),
|
||||||
|
let authCode = item.value else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let accessToken = await mastodonController.authorize(authorizationCode: authCode)
|
||||||
|
|
||||||
|
// construct a temporary UserAccountInfo instance for the MastodonController to use to fetch its own account
|
||||||
|
let tempAccountInfo = LocalData.UserAccountInfo(id: "temp", instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken)
|
||||||
|
mastodonController.accountInfo = tempAccountInfo
|
||||||
|
|
||||||
|
let account: Account
|
||||||
|
do {
|
||||||
|
account = try await mastodonController.getOwnAccount()
|
||||||
|
} catch {
|
||||||
|
let alert = UIAlertController(title: "Unable to Verify Credentials", message: "Your account fcould not be fetcheda this time: \(error.localizedDescription)", preferredStyle: .alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
|
||||||
|
self.present(alert, animated: true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken)
|
||||||
|
mastodonController.accountInfo = accountInfo
|
||||||
|
|
||||||
|
self.onboardingDelegate?.didFinishOnboarding(account: accountInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension OnboardingViewController: ASWebAuthenticationPresentationContextProviding {
|
extension OnboardingViewController: ASWebAuthenticationPresentationContextProviding {
|
||||||
|
|
Loading…
Reference in New Issue