yattee/Model/Accounts/AccountsBridge.swift

57 lines
1.9 KiB
Swift
Raw Permalink Normal View History

2021-10-27 21:11:38 +00:00
import Defaults
import Foundation
struct AccountsBridge: Defaults.Bridge {
typealias Value = Account
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
2022-09-28 14:27:01 +00:00
guard let value else {
2021-10-27 21:11:38 +00:00
return nil
}
// Parse the urlString to check for embedded username and password
var sanitizedUrlString = value.urlString
if var urlComponents = URLComponents(string: value.urlString) {
if let user = urlComponents.user, let password = urlComponents.password {
// Sanitize the embedded username and password
let sanitizedUser = user.addingPercentEncoding(withAllowedCharacters: .urlUserAllowed) ?? user
let sanitizedPassword = password.addingPercentEncoding(withAllowedCharacters: .urlPasswordAllowed) ?? password
// Update the URL components with sanitized credentials
urlComponents.user = sanitizedUser
urlComponents.password = sanitizedPassword
// Reconstruct the sanitized URL
sanitizedUrlString = urlComponents.string ?? value.urlString
}
}
2021-10-27 21:11:38 +00:00
return [
"id": value.id,
"instanceID": value.instanceID ?? "",
2023-04-22 13:08:33 +00:00
"name": value.name,
"apiURL": sanitizedUrlString,
"username": value.username,
"password": value.password ?? ""
2021-10-27 21:11:38 +00:00
]
}
func deserialize(_ object: Serializable?) -> Value? {
guard
2022-09-28 14:27:01 +00:00
let object,
2021-10-27 21:11:38 +00:00
let id = object["id"],
let instanceID = object["instanceID"],
let url = object["apiURL"],
let username = object["username"]
2021-10-27 21:11:38 +00:00
else {
return nil
}
let name = object["name"] ?? ""
let password = object["password"]
2021-10-27 21:11:38 +00:00
2022-12-09 00:15:19 +00:00
return Account(id: id, instanceID: instanceID, name: name, urlString: url, username: username, password: password)
2021-10-27 21:11:38 +00:00
}
}