matthewpalmer / Locksmith

A powerful, protocol-oriented library for working with the keychain in Swift.
MIT License
2.92k stars 268 forks source link

Is Locksmith ignoring my accessible setting? #126

Closed morgz closed 8 years ago

morgz commented 8 years ago

Hi,

Firstly, thanks for the library. Apart from one issue it's proving very neat for my project.

The issue I have is with trying to set accessible on my class. It seems to be ignored, and when this code is called:

public extension SecureStorable {
    var accessible: LocksmithAccessibleOption? { return nil }
    var accessGroup: String? { return nil }
    var secureStorableBaseStoragePropertyDictionary: [String: AnyObject] {
        let dictionary = [
            String(kSecAttrAccessGroup): accessGroup,
            String(kSecAttrAccessible): accessible?.rawValue
        ]

        return Dictionary(withoutOptionalValues: dictionary)
    }

If I print accessible it's value is Locksmith.LocksmithAccessibleOption.WhenUnlocked

But in my class I have set Accessible to be .Always:

   class Accounts: ReadableSecureStorable, CreateableSecureStorable, GenericPasswordSecureStorable, DeleteableSecureStorable {

   let accessible: LocksmithAccessibleOption = .Always
    var emails = [String]()
    // Required by GenericPasswordSecureStorable
    let service = keychainInfo.service
    var account: String { return "Accounts" }
   ...

The strange thing is in the debugger I can see that it has .Always as the value... but when I print accessible it is coming through as .WhenUnlocked.

Image of value in debugger

matthewpalmer commented 8 years ago

From memory there was a pretty annoying thing in Swift where if you override a property in a protocol it needs to have the exact same type (so a non-optional won't replace an optional). Without digging into it more the first thing I'd try is changing let accessible: LocksmithAccessibleOption = .Always to var accessible: LocksmithAccessibleOption? = { return .Always } and see if that helps.

morgz commented 8 years ago

Thanks Mathew. This worked. Your suggestion was just missing the final parenthesis.

var accessible: LocksmithAccessibleOption? = {return .Always}()

lohithkorp commented 4 years ago

@morgz - How/where do you use this accessible var while you are performing .updateData or .loadDataForUserAccount?