Files
workavia-mail-front/ios/TwakeMailNSE/Keychain/KeychainController.swift
T
dab246 a1f86d502a TF-2384 Handle NSE to get plain notification in iOS
Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit 364f32a14b9888f73a3a103b8b5e1bc9b31e148e)
2024-01-08 14:25:26 +01:00

52 lines
1.6 KiB
Swift

import Foundation
import KeychainAccess
enum KeychainControllerService: String {
case sessions
var identifier: String {
InfoPlistReader.main.baseBundleIdentifier + "." + rawValue
}
}
class KeychainController: KeychainControllerDelegate {
private let keychain: Keychain
init(service: KeychainControllerService,
accessGroup: String) {
keychain = Keychain(service: service.identifier,
accessGroup: accessGroup)
}
func retrieveSharingSessionFromKeychain(accountId: String) -> KeychainSharingSession? {
do {
guard let sessionData = try keychain.getData(accountId) else {
return nil
}
return try JSONDecoder().decode(KeychainSharingSession.self, from: sessionData)
} catch {
return nil
}
}
func retrieveSharingSessions() -> [KeychainCredentials] {
keychain.allKeys().compactMap { accountId in
guard let sharingSession = retrieveSharingSessionFromKeychain(accountId: accountId) else {
return nil
}
return KeychainCredentials(accountId: accountId, sharingSession: sharingSession)
}
}
func updateEmailStateToKeychain(accountId: String, newState: String) {
do {
if let sharingSession = retrieveSharingSessionFromKeychain(accountId: accountId) {
let newSharingSession = sharingSession.updateEmailState(newState: newState)
try keychain.set(newSharingSession.toJson() ?? "", key: accountId)
}
} catch {}
}
}