granoff / Lockbox

Objective-C utility class for storing data securely in the key chain.
MIT License
847 stars 87 forks source link

Lockbox unarchiveObjectForKey: returns nil sometimes #54

Open maxencecornu opened 8 years ago

maxencecornu commented 8 years ago

Hello,

I use LockBox to store a refresh token and some urls, but sometimes when I call [Lockbox unarchiveObjectForKey:myKey] it returns nil. I don't know why :/

Here is how I save the data : (the infos came from a WS call)

[Lockbox archiveObject:oauthDico[@"baseUrl"] forKey:kOAuthBaseUrl]; [Lockbox archiveObject:oauthDico[@"getTokenEndpoint"] forKey:kOAuthTokenEndPoint]; [Lockbox archiveObject:oauthDico[@"refreshTokenEndpoint"] forKey:kOAuthRefreshTokenEndPoint];`

and this is how I retrieve it :

NSString* requestUrl = [NSString stringWithFormat:@"%@%@", [Lockbox unarchiveObjectForKey:kOAuthBaseUrl], [Lockbox unarchiveObjectForKey:kOAuthTokenEndPoint]];

and as I told, sometimes, I didn't figured out when excatly, the unarchive method returns nil, which is very problematic for a login url :p

Thanks for your help Max

granoff commented 8 years ago

That is odd indeed.

At the risk of stating the obvious, have you tried examining the contents of the dictionary oauthDico before you store values from it? If, for example, any of those values were nil, Lockbox would assume that you want to remove the associated key from the Keychain. (That is, storing a nil value removes the associated key from the Keychain.) Put another way, Lockbox would not complain about "storing" a nil value. When you then tried to retrieve the value for a key that is not in the Keychain, Lockbox returns nil.

I would start by being a bit more verbose with your code, at least to try to see where this problem is coming from. Verify (perhaps using NSAssert statements while developing the code) that values you expect to be non-nil are in fact not nil, for example.

...unless you've already done all that...

Are you certain there isn't a timing issue with fetching the values, storing the values, and then retrieving the values? Is it possible that you are retrieving the values before they have actually been saved to the Keychain? Without seeing your whole app, I am just throwing out ideas here. :)

maxencecornu commented 8 years ago

Hello,

Thanks for your answer. I will check the non-nil value with asserts and control the timing. If I have the problem again, I'll let you know. Please leave this issue open for a month if you don't mind, to be sure on my side that all is ok.

Max