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
|
|
|
|
}
|
|
|
|
|
2024-08-28 14:21:49 +00:00
|
|
|
// 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,
|
2022-07-01 21:28:32 +00:00
|
|
|
"instanceID": value.instanceID ?? "",
|
2023-04-22 13:08:33 +00:00
|
|
|
"name": value.name,
|
2024-08-28 14:21:49 +00:00
|
|
|
"apiURL": sanitizedUrlString,
|
2021-11-14 23:06:01 +00:00
|
|
|
"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"],
|
2021-11-14 23:06:01 +00:00
|
|
|
let username = object["username"]
|
2021-10-27 21:11:38 +00:00
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = object["name"] ?? ""
|
2021-11-14 23:06:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|