+/*************************************************************************/
+#pragma mark -
+#pragma mark STATIC KEYCHAIN SUPPORT METHODS
+/*************************************************************************/
+
++ (SecKeychainItemRef)keychainItemForUser:(NSString *)user
+{
+ SecKeychainSearchRef search;
+ SecKeychainItemRef item;
+ OSStatus status;
+ SecKeychainAttribute attributes[3];
+ SecKeychainAttributeList list;
+
+ if ((user == nil) || ([user length] == 0)) {
+ return nil;
+ }
+
+ ITDebugLog(@"Audioscrobbler: Searching for keychain item for %@.", user);
+ attributes[0].tag = kSecAccountItemAttr;
+ attributes[0].data = (char *)[user UTF8String];
+ attributes[0].length = [user length];
+ attributes[1].tag = kSecDescriptionItemAttr;
+ attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
+ attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
+ attributes[2].tag = kSecLabelItemAttr;
+ attributes[2].data = AUDIOSCROBBLER_KEYCHAIN_SERVICE;
+ attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE);
+ list.count = 3;
+ list.attr = attributes;
+
+ status = SecKeychainSearchCreateFromAttributes(NULL, kSecGenericPasswordItemClass, &list, &search);
+
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error searching for existing keychain item: %i", status);
+ }
+
+ status = SecKeychainSearchCopyNext(search, &item);
+
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error searching for existing keychain item: %i", status);
+ item = nil;
+ }
+
+ CFRelease(search);
+ return item;
+}
+
++ (BOOL)keychainItemExistsForUser:(NSString *)user
+{
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ BOOL exists = (item != nil);
+ if (item) {
+ CFRelease(item);
+ }
+ return exists;
+}
+
++ (BOOL)createKeychainItemForUser:(NSString *)user andPassword:(NSString *)password
+{
+ SecKeychainItemRef item;
+ OSStatus status;
+ SecKeychainAttribute attributes[3];
+ SecKeychainAttributeList list;
+
+ ITDebugLog(@"Audioscrobbler: Creating new keychain item for %@.", user);
+ attributes[0].tag = kSecAccountItemAttr;
+ attributes[0].data = (char *)[user UTF8String];
+ attributes[0].length = [user length];
+ attributes[1].tag = kSecDescriptionItemAttr;
+ attributes[1].data = AUDIOSCROBBLER_KEYCHAIN_KIND;
+ attributes[1].length = strlen(AUDIOSCROBBLER_KEYCHAIN_KIND);
+ attributes[2].tag = kSecLabelItemAttr;
+ attributes[2].data = AUDIOSCROBBLER_KEYCHAIN_SERVICE;
+ attributes[2].length = strlen(AUDIOSCROBBLER_KEYCHAIN_SERVICE);
+ list.count = 3;
+ list.attr = attributes;
+
+ status = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &list, [password length], [password UTF8String], NULL, NULL, &item);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error creating keychain item: %i", status);
+ }
+ return (status == noErr);
+}
+
++ (BOOL)deleteKeychainItemForUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ if (item != nil) {
+ status = SecKeychainItemDelete(item);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ CFRelease(item);
+ }
+ return (status == noErr);
+}
+
++ (BOOL)setKeychainItemPassword:(NSString *)password forUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ if (item != nil) {
+ status = SecKeychainItemModifyContent(item, NULL, [password length], [password cString]);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ CFRelease(item);
+ }
+ return (status == noErr);
+}
+
++ (NSString *)getKeychainItemPasswordForUser:(NSString *)user
+{
+ OSStatus status = errSecNotAvailable;
+ SecKeychainItemRef item = [PreferencesController keychainItemForUser:user];
+ NSString *pass = nil;
+ if (item != nil) {
+ UInt32 length;
+ char *buffer;
+ status = SecKeychainItemCopyContent(item, NULL, NULL, &length, (void **)&buffer);
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error getting keychain item password: %i", status);
+ } else {
+ if ([NSString respondsToSelector:@selector(stringWithCString:encoding:)]) {
+ pass = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];
+ } else {
+ pass = [NSString stringWithCString:buffer];
+ }
+ }
+ if (status != noErr) {
+ ITDebugLog(@"Audioscrobbler: Error deleting keychain item: %i", status);
+ }
+ CFRelease(item);
+ }
+ return pass;
+}