-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40f33c7
commit 243b703
Showing
6 changed files
with
170 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Foundation | ||
|
||
@propertyWrapper | ||
public struct KeychainStorage<T: Codable> { | ||
let key: String | ||
let itemClass: ItemClass | ||
let keychain: KeychainManager | ||
|
||
private var currentValue: T? | ||
|
||
public var wrappedValue: T? { | ||
get { | ||
return getItem() | ||
} | ||
|
||
set { | ||
if let newValue { | ||
getItem() != nil | ||
? updateItem(newValue) | ||
: saveItem(newValue) | ||
} else { | ||
deleteItem() | ||
} | ||
} | ||
} | ||
|
||
public init(key: String, itemClass: ItemClass, keychain: KeychainManager = .standard) { | ||
self.key = key | ||
self.itemClass = itemClass | ||
self.keychain = keychain | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
private extension KeychainStorage { | ||
|
||
func getItem() -> T? { | ||
do { | ||
return try keychain.retrieveItem(ofClass: itemClass, key: key) | ||
} catch { | ||
handleError(error) | ||
} | ||
return nil | ||
} | ||
|
||
func saveItem(_ item: T) { | ||
do { | ||
try keychain.saveItem(item, itemClass: itemClass, key: key) | ||
} catch { | ||
handleError(error) | ||
} | ||
} | ||
|
||
func updateItem(_ item: T) { | ||
do { | ||
try keychain.updateItem(with: item, ofClass: itemClass, key: key) | ||
} catch { | ||
handleError(error) | ||
} | ||
} | ||
|
||
func deleteItem() { | ||
do { | ||
try keychain.deleteItem(ofClass: itemClass, key: key) | ||
} catch { | ||
handleError(error) | ||
} | ||
} | ||
|
||
func handleError(_ error: Error) { | ||
print(error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Bruno Lorenzo on 5/10/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias KeychainDictionary = [String : Any] | ||
public typealias ItemAttributes = [CFString : Any] | ||
|
||
extension KeychainDictionary { | ||
mutating func addAttributes(_ attributes: ItemAttributes) { | ||
for(key, value) in attributes { | ||
self[key as String] = value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import KeychainUtility | ||
|
||
final class PropertyWrapperTests: XCTestCase { | ||
|
||
@KeychainStorage(key: "API-Token", itemClass: .generic) | ||
var token: String? | ||
|
||
override func setUp() { | ||
super.setUp() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
token = nil | ||
} | ||
|
||
func testSaveItem() { | ||
token = "c0c61d55558b0c8dac82a16c04981eea7c99e37d714367e575028221028b0d4cff122d6a7556fc0ab1c66d1d4b05b378" | ||
let keychainToken: String? = try? KeychainManager.standard.retrieveItem(ofClass: .generic, key: "API-Token") | ||
XCTAssertEqual(token, keychainToken) | ||
} | ||
|
||
func testUpdateItem() { | ||
token = "c0c61d55558b0c8dac82a16c04981eea7c99e37d714367e575028221028b0d4cff122d6a7556fc0ab1c66d1d4b05b378" | ||
var keychainToken: String? = try? KeychainManager.standard.retrieveItem(ofClass: .generic, key: "API-Token") | ||
XCTAssertEqual(token, keychainToken) | ||
|
||
token = "b7bb1d55558b0c8dac82a16c04981eea7c99e37d714367e575028221028b0d4cff122d6a7556fc0ab1c66d1d4b05b378" | ||
XCTAssertNotEqual(token, keychainToken) | ||
|
||
keychainToken = try? KeychainManager.standard.retrieveItem(ofClass: .generic, key: "API-Token") | ||
XCTAssertEqual(token, keychainToken) | ||
} | ||
|
||
func testRemoveItem() { | ||
token = "c0c61d55558b0c8dac82a16c04981eea7c99e37d714367e575028221028b0d4cff122d6a7556fc0ab1c66d1d4b05b378" | ||
var keychainToken: String? = try? KeychainManager.standard.retrieveItem(ofClass: .generic, key: "API-Token") | ||
XCTAssertEqual(token, keychainToken) | ||
|
||
token = nil | ||
keychainToken = try? KeychainManager.standard.retrieveItem(ofClass: .generic, key: "API-Token") | ||
XCTAssertNil(token) | ||
} | ||
} |
Credo che in un commit hai rimosso tutte le variabili necessarie al funzionamenrto, come
shared
,attributes
... Al momento l esempio su GitHub non è funzionante, io lo ho sistemato da me. Probabilmente con l inserimento del@propertyWrapper
si è perso tutto.I think in a commit you removed all the variables needed for it to work, like
shared
,attributes
... At the moment the example on GitHub is not working, I fixed it myself. Probably with the insertion of the@propertyWrapper
everything was lost.