Impressia/Vernissage/Services/AuthorizationService.swift

257 lines
11 KiB
Swift
Raw Normal View History

2023-01-03 14:09:22 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-03 14:09:22 +01:00
//
2023-01-03 14:09:22 +01:00
import Foundation
2023-02-19 10:32:38 +01:00
import PixelfedKit
import CoreData
2023-01-29 19:11:44 +01:00
import AuthenticationServices
2023-01-03 14:09:22 +01:00
2023-01-28 21:09:31 +01:00
/// Srvice responsible for login user into the Pixelfed account.
2023-01-03 14:09:22 +01:00
public class AuthorizationService {
public static let shared = AuthorizationService()
2023-01-05 11:55:20 +01:00
private init() { }
2023-01-28 21:09:31 +01:00
/// Access token verification.
2023-03-29 17:06:41 +02:00
public func verifyAccount(session: AuthorizationSession, accountModel: AccountModel, _ result: @escaping (AccountModel?) -> Void) async {
2023-01-03 14:09:22 +01:00
// When we dont have even one account stored in database then we have to ask user to enter server and sign in.
2023-03-29 17:06:41 +02:00
guard let accessToken = accountModel.accessToken else {
2023-01-03 14:09:22 +01:00
result(nil)
return
}
2023-01-03 14:09:22 +01:00
// When we have at least one account then we have to verify access token.
2023-03-29 17:06:41 +02:00
let client = PixelfedClient(baseURL: accountModel.serverUrl).getAuthenticated(token: accessToken)
2023-01-03 14:09:22 +01:00
do {
let account = try await client.verifyCredentials()
2023-03-29 17:06:41 +02:00
let signedInAccountModel = await self.update(accountId: accountModel.id,
basedOn: account,
accessToken: accessToken,
refreshToken: accountModel.refreshToken)
2023-01-29 19:11:44 +01:00
2023-03-29 17:06:41 +02:00
result(signedInAccountModel)
2023-01-03 14:09:22 +01:00
} catch {
do {
2023-03-29 17:06:41 +02:00
let signedInAccountModel = try await self.refreshCredentials(for: accountModel, presentationContextProvider: session)
result(signedInAccountModel)
2023-01-03 14:09:22 +01:00
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Issues during refreshing credentials.", showToastr: true)
2023-01-03 14:09:22 +01:00
}
}
}
2023-01-28 21:09:31 +01:00
/// Sign in to the Pixelfed server.
2023-03-29 17:06:41 +02:00
public func sign(in serverAddress: String, session: AuthorizationSession, _ result: @escaping (AccountModel) -> Void) async throws {
2023-01-29 19:11:44 +01:00
guard let baseUrl = URL(string: serverAddress) else {
throw AuthorisationError.badServerUrl
}
2023-02-19 10:43:37 +01:00
let client = PixelfedClient(baseURL: baseUrl)
2023-01-03 14:09:22 +01:00
// Verify address.
2023-01-29 19:11:44 +01:00
_ = try await client.readInstanceInformation()
2023-01-03 14:09:22 +01:00
2023-02-06 14:50:28 +01:00
// Create application (we will get clientId and clientSecret).
2023-01-03 14:09:22 +01:00
let oAuthApp = try await client.createApp(
2023-01-29 19:11:44 +01:00
named: AppConstants.oauthApplicationName,
redirectUri: AppConstants.oauthRedirectUri,
scopes: Scopes(AppConstants.oauthScopes),
2023-01-03 14:09:22 +01:00
website: baseUrl)
2023-01-03 14:09:22 +01:00
// Authorize a user (browser, we will get clientCode).
let oAuthSwiftCredential = try await client.authenticate(
app: oAuthApp,
2023-01-29 19:11:44 +01:00
scope: Scopes(AppConstants.oauthScopes),
callbackUrlScheme: AppConstants.oauthScheme,
presentationContextProvider: session)
2023-01-03 14:09:22 +01:00
// Get authenticated client.
let authenticatedClient = client.getAuthenticated(token: oAuthSwiftCredential.oauthToken)
2023-01-03 14:09:22 +01:00
// Get account information from server.
let account = try await authenticatedClient.verifyCredentials()
2023-02-06 14:50:28 +01:00
// Get/create account object in database.
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
let accountData = self.getAccountData(account: account, backgroundContext: backgroundContext)
2023-01-03 14:09:22 +01:00
accountData.id = account.id
accountData.username = account.username
accountData.acct = account.acct
2023-01-18 18:41:42 +01:00
accountData.displayName = account.displayNameWithoutEmojis
2023-02-24 15:52:57 +01:00
accountData.note = account.note?.htmlValue
2023-01-03 14:09:22 +01:00
accountData.url = account.url
accountData.avatar = account.avatar
accountData.header = account.header
accountData.locked = account.locked
accountData.createdAt = account.createdAt
accountData.followersCount = Int32(account.followersCount)
accountData.followingCount = Int32(account.followingCount)
accountData.statusesCount = Int32(account.statusesCount)
2023-01-29 19:11:44 +01:00
// Store data about Server and OAuth client.
2023-01-03 14:09:22 +01:00
accountData.serverUrl = baseUrl
accountData.clientId = oAuthApp.clientId
accountData.clientSecret = oAuthApp.clientSecret
2023-01-14 08:52:51 +01:00
accountData.clientVapidKey = oAuthApp.vapidKey ?? String.empty()
2023-01-29 19:11:44 +01:00
// Store data about oauth tokens.
2023-01-03 14:09:22 +01:00
accountData.accessToken = oAuthSwiftCredential.oauthToken
2023-01-29 19:11:44 +01:00
accountData.refreshToken = oAuthSwiftCredential.oauthRefreshToken
2023-01-03 14:09:22 +01:00
// Download avatar image.
if let avatarUrl = account.avatar {
do {
let avatarData = try await RemoteFileService.shared.fetchData(url: avatarUrl)
accountData.avatarData = avatarData
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Avatar has not been downloaded.")
2023-01-03 14:09:22 +01:00
}
}
2023-02-20 16:41:18 +01:00
// Set newly created account as current (only when we create a first account).
let defaultSettings = ApplicationSettingsHandler.shared.get(viewContext: backgroundContext)
2023-02-20 16:41:18 +01:00
if defaultSettings.currentAccount == nil {
defaultSettings.currentAccount = accountData.id
}
2023-02-20 16:41:18 +01:00
// Save account/settings data in database.
CoreDataHandler.shared.save(viewContext: backgroundContext)
2023-01-03 14:09:22 +01:00
// Return account data.
2023-03-29 17:06:41 +02:00
let accountModel = AccountModel(accountData: accountData)
result(accountModel)
2023-01-03 14:09:22 +01:00
}
2023-02-06 14:50:28 +01:00
public func refreshAccessTokens() async {
let accounts = AccountDataHandler.shared.getAccountsData()
2023-02-06 14:50:28 +01:00
await withTaskGroup(of: Void.self) { group in
for account in accounts {
group.addTask {
do {
2023-03-29 17:06:41 +02:00
_ = try await self.refreshAccessToken(accountData: account)
2023-02-12 09:13:04 +01:00
#if DEBUG
2023-02-21 22:41:20 +01:00
ToastrService.shared.showSuccess("New access tokens has been retrieved.", imageSystemName: "key.fill")
2023-02-12 09:13:04 +01:00
#endif
2023-02-06 14:50:28 +01:00
} catch {
2023-02-12 09:13:04 +01:00
#if DEBUG
2023-02-21 22:41:20 +01:00
ErrorService.shared.handle(error, message: "Refresh token failed: '\(account.acct)'.", showToastr: true)
2023-02-12 09:13:04 +01:00
#else
ErrorService.shared.handle(error, message: "Error during refreshing access token for account '\(account.acct)'.")
#endif
2023-02-06 14:50:28 +01:00
}
}
}
}
}
2023-03-29 17:06:41 +02:00
private func refreshAccessToken(accountData: AccountData) async throws -> AccountModel? {
2023-02-19 10:43:37 +01:00
let client = PixelfedClient(baseURL: accountData.serverUrl)
2023-02-06 14:50:28 +01:00
guard let refreshToken = accountData.refreshToken else {
2023-03-29 17:06:41 +02:00
return nil
2023-02-06 14:50:28 +01:00
}
2023-02-06 14:50:28 +01:00
let oAuthSwiftCredential = try await client.refreshToken(clientId: accountData.clientId,
clientSecret: accountData.clientSecret,
refreshToken: refreshToken)
2023-02-06 14:50:28 +01:00
// Get authenticated client.
let authenticatedClient = client.getAuthenticated(token: oAuthSwiftCredential.oauthToken)
2023-02-06 14:50:28 +01:00
// Get account information from server.
let account = try await authenticatedClient.verifyCredentials()
2023-03-29 17:06:41 +02:00
return await self.update(accountId: accountData.id,
basedOn: account,
accessToken: oAuthSwiftCredential.oauthToken,
refreshToken: oAuthSwiftCredential.oauthRefreshToken)
2023-02-06 14:50:28 +01:00
}
2023-03-29 17:06:41 +02:00
private func refreshCredentials(for accountModel: AccountModel,
2023-01-29 19:11:44 +01:00
presentationContextProvider: ASWebAuthenticationPresentationContextProviding
2023-03-29 17:06:41 +02:00
) async throws -> AccountModel? {
2023-01-29 19:11:44 +01:00
2023-03-29 17:06:41 +02:00
let client = PixelfedClient(baseURL: accountModel.serverUrl)
2023-01-03 14:09:22 +01:00
2023-02-09 18:17:01 +01:00
// Create application (we will get clientId and clientSecret).
2023-03-29 17:06:41 +02:00
let oAuthApp = Application(clientId: accountModel.clientId,
clientSecret: accountModel.clientSecret,
2023-02-09 18:17:01 +01:00
redirectUri: AppConstants.oauthRedirectUri)
2023-01-03 14:09:22 +01:00
// Authorize a user (browser, we will get clientCode).
2023-01-29 19:11:44 +01:00
let oAuthSwiftCredential = try await client.authenticate(app: oAuthApp,
scope: Scopes(AppConstants.oauthScopes),
callbackUrlScheme: AppConstants.oauthScheme,
presentationContextProvider: presentationContextProvider)
2023-01-03 14:09:22 +01:00
// Get authenticated client.
let authenticatedClient = client.getAuthenticated(token: oAuthSwiftCredential.oauthToken)
2023-01-03 14:09:22 +01:00
// Get account information from server.
let account = try await authenticatedClient.verifyCredentials()
2023-03-29 17:06:41 +02:00
return await self.update(accountId: accountModel.id,
basedOn: account,
accessToken: oAuthSwiftCredential.oauthToken,
refreshToken: oAuthSwiftCredential.oauthRefreshToken)
2023-01-03 14:09:22 +01:00
}
2023-02-28 06:28:00 +01:00
private func update(accountId: String,
2023-01-29 19:11:44 +01:00
basedOn account: Account,
accessToken: String,
refreshToken: String?
2023-03-29 17:06:41 +02:00
) async -> AccountModel? {
let backgroundContext = CoreDataHandler.shared.newBackgroundContext()
guard let dbAccount = AccountDataHandler.shared.getAccountData(accountId: accountId, viewContext: backgroundContext) else {
2023-03-29 17:06:41 +02:00
return nil
2023-02-28 06:28:00 +01:00
}
2023-01-28 21:09:31 +01:00
dbAccount.username = account.username
dbAccount.acct = account.acct
2023-02-17 17:10:09 +01:00
dbAccount.displayName = account.displayNameWithoutEmojis
2023-02-24 15:52:57 +01:00
dbAccount.note = account.note?.htmlValue
2023-01-28 21:09:31 +01:00
dbAccount.url = account.url
dbAccount.avatar = account.avatar
dbAccount.header = account.header
dbAccount.locked = account.locked
dbAccount.createdAt = account.createdAt
dbAccount.followersCount = Int32(account.followersCount)
dbAccount.followingCount = Int32(account.followingCount)
dbAccount.statusesCount = Int32(account.statusesCount)
2023-01-29 19:11:44 +01:00
// Store data about new oauth tokens.
dbAccount.accessToken = accessToken
dbAccount.refreshToken = refreshToken
2023-01-03 14:09:22 +01:00
// Download avatar image.
if let avatarUrl = account.avatar {
do {
let avatarData = try await RemoteFileService.shared.fetchData(url: avatarUrl)
2023-01-28 21:09:31 +01:00
dbAccount.avatarData = avatarData
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Avatar has not been downloaded.")
2023-01-03 14:09:22 +01:00
}
}
2023-01-03 14:09:22 +01:00
// Save account data in database and in application state.
CoreDataHandler.shared.save(viewContext: backgroundContext)
2023-03-29 17:06:41 +02:00
return AccountModel(accountData: dbAccount)
2023-01-03 14:09:22 +01:00
}
private func getAccountData(account: Account, backgroundContext: NSManagedObjectContext) -> AccountData {
if let accountFromDb = AccountDataHandler.shared.getAccountData(accountId: account.id, viewContext: backgroundContext) {
2023-02-06 14:50:28 +01:00
return accountFromDb
}
return AccountDataHandler.shared.createAccountDataEntity(viewContext: backgroundContext)
2023-02-06 14:50:28 +01:00
}
2023-01-03 14:09:22 +01:00
}