Comments (3)
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.
+1
from swiftkeychain.
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)
- Issue with Release build configuration for 64bit HOT 1
- error errSecItemNotFound 25300 should not be thrown when removing items
- Error Domain=swift.keychain.error Code=-25300 "(null)" on fetch of not existing accountName
- Migrate to Swift 3 HOT 2
- Unique keychain for each user? HOT 1
- Can`t get any data from keychain: segmentation fault HOT 5
- Cocoapods support? HOT 2
- update pod version
- keychain error on iPhone only HOT 2
- Swift 2.0 support? HOT 1
- Active development HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftkeychain.