Giter VIP home page Giter VIP logo

Comments (3)

tompson avatar tompson commented on May 28, 2024 2

found out the reason for this: when you try to add an entry with a different access mode it can not be stored because it is not unique

the problem is that access mode is not considered for uniqueness but considered for query, so the remove request did not remove the entry

from swiftkeychain.

damienrambout avatar damienrambout commented on May 28, 2024

+1

from swiftkeychain.

robertbarclay avatar robertbarclay commented on May 28, 2024

I ran into this problem too, except with the the scenario of using "onFirstUnlock", which by the way if you are storing login credentials is the safest and most secure. If you store as "always", it is stores unencrypted in the keychain, with the first unlock, it is accessible by the time you application opens after its been unlocked by the user, and is encrypted with the built in hardware encryption key. Since we need to use the access token while the application is in the background, you must use "Always" or "On first Unlock". I'd strongly recommend to the project admins to move the default to be "OnFirstUnlock" from "Unlock" since unlock is only available while the application is in the foreground, so if you application makes use of ANY extensions, or runs in the background for any amount of time, that token is not available, since it is in the background.

If you have a previously stored key, that key has the access control defined as "OnUnlock", so it is not found in the query, so you get a -25300, on the OSStatus response. Then when you go to insert it with the new value, there is an error on the insert getting the -25299, duplicate found, since there is in fact a keychain item there. When you respond on the errSecDuplicateItem (-25299), the delete fails with a -25300 since it can not find that item to delete. Then it gets re added, and fails for the same reason the initial failure occurred:

The solution i found was to change:

public func insertItemWithAttributes(attributes: [String: AnyObject]) throws {

        var statusCode = SecItemAdd(attributes, nil)
        if statusCode == errSecDuplicateItem {
            SecItemDelete(attributes)
            statusCode = SecItemAdd(attributes, nil)
        }

        if statusCode != errSecSuccess {

            throw errorForStatusCode(statusCode)
        }
    }

to

public func insertItemWithAttributes(attributes: [String: AnyObject]) throws {

        var statusCode = SecItemAdd(attributes, nil)
        if statusCode == errSecDuplicateItem {
            var removeAttributes = attributes
            removeAttributes.removeValueForKey(String(kSecAttrAccessible))
            SecItemDelete(removeAttributes)
            statusCode = SecItemAdd(attributes, nil)
        }

        if statusCode != errSecSuccess {

            throw errorForStatusCode(statusCode)
        }
    }

and to also change the removeItem in a similar way to ensure it is removed, on say logout:

public func removeItemWithAttributes(attributes: [String: AnyObject]) throws {
        var removeAttributes = attributes
        removeAttributes.removeValueForKey(String(kSecAttrAccessible))
        let statusCode = SecItemDelete(removeAttributes)

        if statusCode != errSecSuccess {

            throw errorForStatusCode(statusCode)
        }
    }

I've tested this in local code, but have not yet forked, and done a PR for it, but it appears to solve the issue.

from swiftkeychain.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.