Commits

Tuukka Norri  committed 1b40132

Fixed a bug which caused updating the keychain to fail

  • Participants
  • Parent commits ebc4674

Comments (0)

Files changed (1)

File Sources/BXDatabaseContext.m

 	if (! (password && [mDatabaseInterface usedPassword]))
     	password = "";
     
-	SecKeychainItemRef item = NULL;
-
-	if (NULL == mKeychainPasswordItem)
+	if (mKeychainPasswordItem)
+		status = SecKeychainItemModifyAttributesAndData (mKeychainPasswordItem, NULL, strlen (password), password);
+	else
 	{
+		SecKeychainItemRef item = NULL;
 		status = SecKeychainAddInternetPassword (NULL, //Default keychain
 												 strlen (serverName), serverName,
 												 0, NULL,
 												 0, kSecAuthenticationTypeDefault,
 												 strlen (password), password, 
 												 &item);
-		[self setKeychainPasswordItem: item];
+		
+		if (noErr == status)
+			[self setKeychainPasswordItem: item];
+		else if (errSecDuplicateItem == status)
+		{
+			status = SecKeychainFindInternetPassword (NULL, 
+													  strlen (serverName), serverName, 
+													  0, NULL, 
+													  strlen (username), username, 
+													  strlen (path), path, 
+													  port, 
+													  0, kSecAuthenticationTypeDefault, 
+													  0, NULL,
+													  &item);
+			if (noErr == status)
+				status = SecKeychainItemModifyAttributesAndData (item, NULL, strlen (password), password);
+		}
 	}
 	
-	if (errSecDuplicateItem == status || (NULL == item && NULL != mKeychainPasswordItem))
-	{
-		status = SecKeychainItemModifyAttributesAndData (mKeychainPasswordItem, NULL, strlen (password), password);
-	}
-	
-	if (noErr == status)
-	{
-		[self setKeychainPasswordItem: NULL];
-	}
+	[self setKeychainPasswordItem: NULL];
 }
 
 /** \name Faulting database objects */