2023-03-05 19:35:25 +00:00
|
|
|
//
|
|
|
|
// UserAccountInfo.swift
|
|
|
|
// UserAccounts
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 3/5/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CryptoKit
|
|
|
|
|
2024-01-28 19:03:14 +00:00
|
|
|
public struct UserAccountInfo: Equatable, Hashable, Identifiable, Sendable {
|
2023-03-05 19:35:25 +00:00
|
|
|
public let id: String
|
|
|
|
public let instanceURL: URL
|
2024-04-12 02:19:29 +00:00
|
|
|
public internal(set) var clientID: String
|
|
|
|
public internal(set) var clientSecret: String
|
2024-04-09 23:05:31 +00:00
|
|
|
public let username: String!
|
|
|
|
public internal(set) var accessToken: String
|
|
|
|
public internal(set) var scopes: [String]?
|
2023-03-05 19:35:25 +00:00
|
|
|
|
2023-12-02 20:33:15 +00:00
|
|
|
// Sort of hack to be able to access these from the share extension.
|
|
|
|
public internal(set) var serverDefaultLanguage: String?
|
|
|
|
public internal(set) var serverDefaultVisibility: String?
|
|
|
|
public internal(set) var serverDefaultFederation: Bool?
|
|
|
|
|
2023-03-05 19:35:25 +00:00
|
|
|
fileprivate static let tempAccountID = "temp"
|
|
|
|
|
|
|
|
static func id(instanceURL: URL, username: String?) -> String {
|
|
|
|
// We hash the instance host and username to form the account ID
|
|
|
|
// so that account IDs will match across devices, allowing for data syncing and handoff.
|
|
|
|
var hasher = SHA256()
|
|
|
|
hasher.update(data: instanceURL.host!.data(using: .utf8)!)
|
|
|
|
if let username {
|
|
|
|
hasher.update(data: username.data(using: .utf8)!)
|
|
|
|
}
|
|
|
|
return Data(hasher.finalize()).base64EncodedString()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only to be used for temporary MastodonController needed to fetch own account info and create final UserAccountInfo with real username
|
|
|
|
public init(tempInstanceURL instanceURL: URL, clientID: String, clientSecret: String, accessToken: String) {
|
|
|
|
self.id = UserAccountInfo.tempAccountID
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = clientSecret
|
2024-04-09 23:05:31 +00:00
|
|
|
self.username = nil
|
2023-03-05 19:35:25 +00:00
|
|
|
self.accessToken = accessToken
|
2024-04-09 23:05:31 +00:00
|
|
|
self.scopes = nil
|
2023-03-05 19:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 23:05:31 +00:00
|
|
|
init(instanceURL: URL, clientID: String, clientSecret: String, username: String? = nil, accessToken: String, scopes: [String]) {
|
2023-03-05 19:35:25 +00:00
|
|
|
self.id = UserAccountInfo.id(instanceURL: instanceURL, username: username)
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = clientSecret
|
|
|
|
self.username = username
|
|
|
|
self.accessToken = accessToken
|
2024-04-09 23:05:31 +00:00
|
|
|
self.scopes = scopes
|
2023-03-05 19:35:25 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 20:33:15 +00:00
|
|
|
init?(userDefaultsDict dict: [String: Any]) {
|
|
|
|
guard let id = dict["id"] as? String,
|
|
|
|
let instanceURL = dict["instanceURL"] as? String,
|
2023-03-05 19:35:25 +00:00
|
|
|
let url = URL(string: instanceURL),
|
2023-12-02 20:33:15 +00:00
|
|
|
let clientID = dict["clientID"] as? String,
|
|
|
|
let secret = dict["clientSecret"] as? String,
|
|
|
|
let accessToken = dict["accessToken"] as? String else {
|
2023-03-05 19:35:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.id = id
|
|
|
|
self.instanceURL = url
|
|
|
|
self.clientID = clientID
|
|
|
|
self.clientSecret = secret
|
2023-12-02 20:33:15 +00:00
|
|
|
self.username = dict["username"] as? String
|
2023-03-05 19:35:25 +00:00
|
|
|
self.accessToken = accessToken
|
2024-04-09 23:05:31 +00:00
|
|
|
self.scopes = dict["scopes"] as? [String]
|
2023-12-02 20:33:15 +00:00
|
|
|
self.serverDefaultLanguage = dict["serverDefaultLanguage"] as? String
|
|
|
|
self.serverDefaultVisibility = dict["serverDefaultVisibility"] as? String
|
|
|
|
self.serverDefaultFederation = dict["serverDefaultFederation"] as? Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var userDefaultsDict: [String: Any] {
|
|
|
|
var dict: [String: Any] = [
|
|
|
|
"id": id,
|
|
|
|
"instanceURL": instanceURL.absoluteString,
|
|
|
|
"clientID": clientID,
|
|
|
|
"clientSecret": clientSecret,
|
|
|
|
"accessToken": accessToken,
|
|
|
|
]
|
|
|
|
if let username {
|
|
|
|
dict["username"] = username
|
|
|
|
}
|
2024-04-09 23:05:31 +00:00
|
|
|
if let scopes {
|
|
|
|
dict["scopes"] = scopes
|
|
|
|
}
|
2023-12-02 20:33:15 +00:00
|
|
|
if let serverDefaultLanguage {
|
|
|
|
dict["serverDefaultLanguage"] = serverDefaultLanguage
|
|
|
|
}
|
|
|
|
if let serverDefaultVisibility {
|
|
|
|
dict["serverDefaultVisibility"] = serverDefaultVisibility
|
|
|
|
}
|
|
|
|
if let serverDefaultFederation {
|
|
|
|
dict["serverDefaultFederation"] = serverDefaultFederation
|
|
|
|
}
|
|
|
|
return dict
|
2023-03-05 19:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A filename-safe string for this account
|
|
|
|
public var persistenceKey: String {
|
|
|
|
// slashes are not allowed in the persistent store coordinator name
|
|
|
|
id.replacingOccurrences(of: "/", with: "_")
|
|
|
|
}
|
|
|
|
}
|