Revision: 21842
          http://sourceforge.net/p/bibdesk/svn/21842
Author:   hofman
Date:     2018-02-07 22:44:38 +0000 (Wed, 07 Feb 2018)
Log Message:
-----------
inline functions to do some repeating tasks

Modified Paths:
--------------
    trunk/bibdesk/BDSKPasswordController.m

Modified: trunk/bibdesk/BDSKPasswordController.m
===================================================================
--- trunk/bibdesk/BDSKPasswordController.m      2018-02-07 20:14:22 UTC (rev 
21841)
+++ trunk/bibdesk/BDSKPasswordController.m      2018-02-07 22:44:38 UTC (rev 
21842)
@@ -44,6 +44,23 @@
 
 @implementation BDSKPasswordController
 
+static inline void logError(NSString *action, OSStatus err) {
+    if (err != errSecItemNotFound && err != errSecUserCanceled)
+        NSLog(@"Error %d occurred %@ password: %@", (int)err, action, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+}
+
+static inline SecKeychainAttributeList makeAttributeList(const char 
*serviceCString, const char *accountCString, const char *nameCString) {
+    // default is to use current user's username
+    if (accountCString == NULL)
+        accountCString = [NSUserName() UTF8String];
+    SecKeychainAttribute attrs[] = {
+        {kSecServiceItemAttr, (SInt32)strlen(serviceCString), (void 
*)serviceCString},
+        {kSecAccountItemAttr, (SInt32)strlen(accountCString), (void 
*)accountCString},
+        {kSecLabelItemAttr, (SInt32)strlen(nameCString), (void *)nameCString}};
+    SecKeychainAttributeList attributes = {3, attrs};
+    return attributes;
+}
+
 + (NSData *)passwordForKeychainService:(NSString *)service account:(NSString 
*)account name:(NSString *)name {
     // use the service name to get password from keychain and hash it with 
sha1 for comparison purposes
     OSStatus err;
@@ -63,14 +80,7 @@
         err = SecKeychainFindGenericPassword(NULL, strlen(nameCString), 
nameCString, 0, NULL, &passwordLength, &password, &itemRef);
         if (err == noErr) {
             // item in old format exists, update to new format
-            // default is to use current user's username
-            if (account == nil)
-                accountCString = [NSUserName() UTF8String];
-            SecKeychainAttribute attrs[] = {
-                {kSecServiceItemAttr, (SInt32)strlen(serviceCString), (void 
*)serviceCString},
-                {kSecAccountItemAttr, (SInt32)strlen(accountCString), (void 
*)accountCString},
-                {kSecLabelItemAttr, (SInt32)strlen(nameCString), (void 
*)nameCString}};
-            SecKeychainAttributeList attributes = {3, attrs};
+            SecKeychainAttributeList attributes = 
makeAttributeList(serviceCString, accountCString, nameCString);
             SecKeychainItemModifyAttributesAndData(itemRef, &attributes, 0, 
NULL);
             CFRelease(itemRef);
         }
@@ -78,8 +88,8 @@
     if (err == noErr) {
         pwData = [NSData dataWithBytes:password length:passwordLength];
         SecKeychainItemFreeContent(NULL, password);
-    } else if (err != errSecItemNotFound && err != errSecUserCanceled){
-        NSLog(@"Error %d occurred getting password: %@", (int)err, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+    } else {
+        logError(@"getting", err);
     }
     return pwData;
 }
@@ -104,8 +114,7 @@
         // password was on keychain, so flush the buffer and then modify the 
keychain if necessary
         if (passwordLength != strlen(passwordData) || strncmp(passwordData, 
oldPasswordData, passwordLength) != 0) {
             err = SecKeychainItemModifyAttributesAndData(itemRef, NULL, 
strlen(passwordData), passwordData);
-            if (err != noErr && err != errSecUserCanceled)
-                NSLog(@"Error %d occurred modifying password: %@", (int)err, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+            logError(@"modifying", err);
         }
         SecKeychainItemFreeContent(NULL, (void *)oldPasswordData);
         CFRelease(itemRef);
@@ -117,23 +126,18 @@
             // see if an item in the old format exists
             err = SecKeychainFindGenericPassword(NULL, strlen(nameCString), 
nameCString, 0, NULL, NULL, NULL, &itemRef);
             
-            // default is to use current user's username
-            if (account == nil)
-                accountCString = [NSUserName() UTF8String];
-            SecKeychainAttribute attrs[] = {
-                {kSecServiceItemAttr, (SInt32)strlen(serviceCString), (void 
*)serviceCString},
-                {kSecAccountItemAttr, (SInt32)strlen(accountCString), (void 
*)accountCString},
-                {kSecLabelItemAttr, (SInt32)strlen(nameCString), (void 
*)nameCString}};
-            SecKeychainAttributeList attributes = {3, attrs};
+            SecKeychainAttributeList attributes = 
makeAttributeList(serviceCString, accountCString, nameCString);
             if (err == noErr) {
                 // password in old format was on keychain, modify the keychain 
to the new format
                 err = SecKeychainItemModifyAttributesAndData(itemRef, 
&attributes, strlen(passwordData), passwordData);
+                logError(@"modifying", err);
                 CFRelease(itemRef);
             } else if (err == errSecItemNotFound) {
                 // password not on keychain, so add it
                 err = 
SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributes, 
strlen(passwordData), passwordData, NULL, NULL, NULL);
-            } else if (err != errSecUserCanceled) {
-                NSLog(@"Error %d occurred getting password: %@", (int)err, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+                logError(@"adding", err);
+            } else {
+                logError(@"getting", err);
             }
         } else {
             // simple password not on keychain, so add it
@@ -141,11 +145,10 @@
             if (account == nil)
                 accountCString = [NSUserName() UTF8String];
             err = SecKeychainAddGenericPassword(NULL, strlen(serviceCString), 
serviceCString, strlen(accountCString), accountCString, strlen(passwordData), 
passwordData, NULL);
-            if (err != noErr && err != errSecUserCanceled)
-                NSLog(@"Error %d occurred adding password: %@", (int)err, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+            logError(@"adding", err);
         }
-    } else if (err != errSecUserCanceled) {
-        NSLog(@"Error %d occurred getting password: %@", (int)err, 
[(id)SecCopyErrorMessageString(err, NULL) autorelease]);
+    } else {
+        logError(@"getting", err);
     }
     return (err == noErr);
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to