Files
workavia-mail-front/ios/TwakeMailNSE/Keychain/KeychainController.swift
T
dab246 b1b2bb90a5 TF-2384 Handle refresh token when token expired in NSE
Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit c8a39bb42792770c001ac90d6301d53126225d95)
2024-01-08 14:25:26 +01:00

61 lines
2.0 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 {}
}
func updateTokenOidc(accountId: String, newTokenOidc: TokenOidc) {
do {
if let sharingSession = retrieveSharingSessionFromKeychain(accountId: accountId) {
let newSharingSession = sharingSession.updateTokenOidc(newTokenOidc: newTokenOidc)
try keychain.set(newSharingSession.toJson() ?? "", key: accountId)
}
} catch {}
}
}