mirror of
https://github.com/mastodon/mastodon-ios.git
synced 2024-12-12 08:36:02 +01:00
119 lines
4.3 KiB
Swift
119 lines
4.3 KiB
Swift
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
import CoreDataStack
|
|
import MastodonSDK
|
|
import KeychainAccess
|
|
import MastodonCommon
|
|
import os.log
|
|
|
|
public class AuthenticationServiceProvider: ObservableObject {
|
|
private let logger = Logger(subsystem: "AuthenticationServiceProvider", category: "Authentication")
|
|
|
|
public static let shared = AuthenticationServiceProvider()
|
|
private static let keychain = Keychain(service: "org.joinmastodon.app.authentications", accessGroup: AppName.groupID)
|
|
private let userDefaults: UserDefaults = .shared
|
|
|
|
private init() {}
|
|
|
|
@Published public var authentications: [MastodonAuthentication] = [] {
|
|
didSet {
|
|
persist() // todo: Is this too heavy and too often here???
|
|
}
|
|
}
|
|
|
|
func update(instance: Instance, where domain: String) {
|
|
authentications = authentications.map { authentication in
|
|
guard authentication.domain == domain else { return authentication }
|
|
return authentication.updating(instance: instance)
|
|
}
|
|
}
|
|
|
|
func delete(authentication: MastodonAuthentication) {
|
|
authentications.removeAll(where: { $0 == authentication })
|
|
}
|
|
|
|
func activateAuthentication(in domain: String, for userID: String) {
|
|
authentications = authentications.map { authentication in
|
|
guard authentication.domain == domain, authentication.userID == userID else {
|
|
return authentication
|
|
}
|
|
return authentication.updating(activatedAt: Date())
|
|
}
|
|
}
|
|
|
|
func getAuthentication(in domain: String, for userID: String) -> MastodonAuthentication? {
|
|
authentications.first(where: { $0.domain == domain && $0.userID == userID })
|
|
}
|
|
}
|
|
|
|
// MARK: - Public
|
|
public extension AuthenticationServiceProvider {
|
|
func getAuthentication(matching userAccessToken: String) -> MastodonAuthentication? {
|
|
authentications.first(where: { $0.userAccessToken == userAccessToken })
|
|
}
|
|
|
|
func authenticationSortedByActivation() -> [MastodonAuthentication] { // fixme: why do we need this?
|
|
return authentications.sorted(by: { $0.activedAt > $1.activedAt })
|
|
}
|
|
|
|
func restore() {
|
|
authentications = Self.keychain.allKeys().compactMap {
|
|
guard
|
|
let encoded = Self.keychain[$0],
|
|
let data = Data(base64Encoded: encoded)
|
|
else { return nil }
|
|
return try? JSONDecoder().decode(MastodonAuthentication.self, from: data)
|
|
}
|
|
}
|
|
|
|
func migrateLegacyAuthenticationsIfRequired(in context: NSManagedObjectContext) {
|
|
guard !userDefaults.didMigrateAuthentications else { return }
|
|
|
|
defer { userDefaults.didMigrateAuthentications = true }
|
|
|
|
do {
|
|
let request = NSFetchRequest<MastodonAuthenticationLegacy>(entityName: "MastodonAuthentication")
|
|
let legacyAuthentications = try context.fetch(request)
|
|
|
|
self.authentications = legacyAuthentications.map { auth in
|
|
MastodonAuthentication(
|
|
identifier: auth.identifier,
|
|
domain: auth.domain,
|
|
username: auth.username,
|
|
appAccessToken: auth.appAccessToken,
|
|
userAccessToken: auth.userAccessToken,
|
|
clientID: auth.clientID,
|
|
clientSecret: auth.clientSecret,
|
|
createdAt: auth.createdAt,
|
|
updatedAt: auth.updatedAt,
|
|
activedAt: auth.activedAt,
|
|
userID: auth.userID
|
|
)
|
|
}
|
|
} catch {
|
|
logger.log(level: .error, "Could not migrate legacy authentications")
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
private extension AuthenticationServiceProvider {
|
|
func persist() {
|
|
for authentication in authentications {
|
|
Self.keychain[authentication.persistenceIdentifier] = try? JSONEncoder().encode(authentication).base64EncodedString()
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension UserDefaults {
|
|
@objc dynamic var didMigrateAuthentications: Bool {
|
|
get {
|
|
register(defaults: [#function: false])
|
|
return bool(forKey: #function)
|
|
}
|
|
set { self[#function] = newValue }
|
|
}
|
|
}
|