Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SSkeychain to store access tokens

Tags:

ios

sskeychain

I'm trying to figure out how to use the SSkeychain in order to store access tokens for the instagram api. I'm currently using NSUserDefault class but I dont think thats the best of ideas.

Does the SSkeychain class itself need to be allocated and initialized in order to be used as well?

like image 218
TheM00s3 Avatar asked Apr 11 '14 01:04

TheM00s3


People also ask

What are tokens in keychain?

Keychain Services are invented to “secrets” that the user explicitly cares about, i.e. passwords, private keys or even secure notes, i.e. clear credentials. But access tokens are temporary hashes generated after user entered password and have limited time.

How secure is Apple keychain?

Everything stored in iCloud Keychain is secure—it's protected by industry-standard encryption. Your iCloud Keychain can't be set up on another Mac or iOS or iPadOS device unless you approve it.

What is key chain in security?

Overview. Keychain items are encrypted using two different AES-256-GCM keys: a table key (metadata) and a per-row key (secret key). Keychain metadata (all attributes other than kSecValue) is encrypted with the metadata key to speed searches and the secret value (kSecValueData) is encrypted with the secret key.


1 Answers

SSKeychain just provides class methods, so you don't need to initialize an instance. It does require some setup, though. The readme is a great source of information on this.

Here's a code example to help:

// Specify how the keychain items can be access
// Do this in your -application:didFinishLaunchingWithOptions: callback
[SSKeychain setAccessibilityType:kSecAttrAccessibleWhenUnlocked];

// Set an access token for later use
[SSKeychain setPassword:instagramToken forService:@"InstagramService" account:@"com.yourapp.keychain"];

// Access that token when needed
[SSKeychain passwordForService:@"InstagramService" account:@"com.yourapp.keychain"];

// Delete the token when appropriate (on sign out, perhaps)
[SSKeychain deletePasswordForService:@"InstagramService" account:@"com.yourapp.keychain"];

I'd also recommend making those @"InstagramService" and @"com.yourapp.keychain" strings constants so it's easier to reference them.

Hope that helps!

like image 78
Stephen C Avatar answered Oct 06 '22 20:10

Stephen C