Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61840

How to workaround critical issue for keychain-swift library?

On the official github site for keychain-swift library there is a statement:

enter image description here

I have a simple class to decode and encode values from keychain:

class Keychain: Keychainable {
    private let keychain: KeychainSwift
    init(keychain: KeychainSwift) {
        self.keychain = keychain
    }

    func encode(value: String, forKey key: String) {
        keychain.set(value, forKey: key)
    }

    func decode(forKey key: String) -> String? {
        var returnValue: String?
        for _ in 1...3 {
            if let value = keychain.get(key) {
                returnValue = value
            } // ❌ my current non working workaround
        }
        return returnValue
    }

    func delete(forKey key: String) {
        keychain.delete(key)
    }
}

And I have another Devicer class where I use it:

class Devicer: Devicable {
    private let device: UIDevice
    private let keychain: Keychainable
    var deviceIdentifier: String {
        guard let identifier = keychain.decode(forKey: "did") else {
            let uuid = device.identifierForVendor!.uuidString
            keychain.encode(value: uuid, forKey: "did")
            return uuid
        }
        return identifier
    }
}

I need to keep ONE PERMANENT deviceId for all long live of the app on the device. But it changes from time to time. Every couple days decode returns nil, but it should not because there is a non nil value. Because it returns nil (it should not) then new deviceId is created and saved as deviceId.

How to create working workaround for it?

Upvotes: 0

Views: 57

Answers (0)

Related Questions