#40: Add UserSessionStoreProtocol.

Only log out of the specific account.
Add tests for the keychain controller.
Expand test coverage.
PR comments
This commit is contained in:
Doug
2022-06-15 10:10:22 +01:00
committed by Doug
parent abd6a9c7c0
commit 8653f1dd6b
17 changed files with 261 additions and 78 deletions
@@ -0,0 +1,61 @@
//
// KeychainController.swift
// ElementX
//
// Created by Stefan Ceriu on 14.02.2022.
// Copyright © 2022 Element. All rights reserved.
//
import Foundation
import KeychainAccess
class KeychainController: KeychainControllerProtocol {
private let keychain: Keychain
init(identifier: String) {
keychain = Keychain(service: identifier)
}
func setAccessToken(_ accessToken: String, forUsername username: String) {
do {
try keychain.set(accessToken, key: username)
} catch {
MXLog.error("Failed storing user access token with error: \(error)")
}
}
func accessTokenForUsername(_ username: String) -> String? {
do {
return try keychain.get(username)
} catch {
MXLog.error("Failed retrieving user access token")
return nil
}
}
func accessTokens() -> [(username: String, accessToken: String)] {
keychain.allKeys().compactMap { username in
guard let accessToken = accessTokenForUsername(username) else {
return nil
}
return (username, accessToken)
}
}
func removeAccessTokenForUsername(_ username: String) {
do {
try keychain.remove(username)
} catch {
MXLog.error("Failed removing access token with error: \(error)")
}
}
func removeAllAccessTokens() {
do {
try keychain.removeAll()
} catch {
MXLog.error("Failed removing all tokens")
}
}
}