1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Accounts/AccountsBridge.swift

57 lines
1.9 KiB
Swift
Raw Normal View History

2021-10-28 02:41:38 +05:30
import Defaults
import Foundation
struct AccountsBridge: Defaults.Bridge {
typealias Value = Account
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
2022-09-28 19:57:01 +05:30
guard let value else {
2021-10-28 02:41:38 +05:30
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-28 02:41:38 +05:30
return [
"id": value.id,
"instanceID": value.instanceID ?? "",
2023-04-22 18:38:33 +05:30
"name": value.name,
"apiURL": sanitizedUrlString,
"username": value.username,
"password": value.password ?? ""
2021-10-28 02:41:38 +05:30
]
}
func deserialize(_ object: Serializable?) -> Value? {
guard
2022-09-28 19:57:01 +05:30
let object,
2021-10-28 02:41:38 +05:30
let id = object["id"],
let instanceID = object["instanceID"],
let url = object["apiURL"],
let username = object["username"]
2021-10-28 02:41:38 +05:30
else {
return nil
}
let name = object["name"] ?? ""
let password = object["password"]
2021-10-28 02:41:38 +05:30
2022-12-09 05:45:19 +05:30
return Account(id: id, instanceID: instanceID, name: name, urlString: url, username: username, password: password)
2021-10-28 02:41:38 +05:30
}
}