Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit ef46bb2

Browse files
author
Rachel Macfarlane
authored
On mac, use SecKeyChainItemModifyAttributesAndData instead of delete and add (#260)
1 parent 3c232b5 commit ef46bb2

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/keytar_mac.cc

+26-15
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ const std::string errorStatusToString(OSStatus status) {
5757
KEYTAR_OP_RESULT AddPassword(const std::string& service,
5858
const std::string& account,
5959
const std::string& password,
60-
std::string* error,
61-
bool returnNonfatalOnDuplicate) {
60+
std::string* error) {
6261
OSStatus status = SecKeychainAddGenericPassword(NULL,
6362
service.length(),
6463
service.data(),
@@ -68,9 +67,7 @@ KEYTAR_OP_RESULT AddPassword(const std::string& service,
6867
password.data(),
6968
NULL);
7069

71-
if (status == errSecDuplicateItem && returnNonfatalOnDuplicate) {
72-
return FAIL_NONFATAL;
73-
} else if (status != errSecSuccess) {
70+
if (status != errSecSuccess) {
7471
*error = errorStatusToString(status);
7572
return FAIL_ERROR;
7673
}
@@ -82,16 +79,30 @@ KEYTAR_OP_RESULT SetPassword(const std::string& service,
8279
const std::string& account,
8380
const std::string& password,
8481
std::string* error) {
85-
KEYTAR_OP_RESULT result = AddPassword(service, account, password,
86-
error, true);
87-
if (result == FAIL_NONFATAL) {
88-
// This password already exists, delete it and try again.
89-
KEYTAR_OP_RESULT delResult = DeletePassword(service, account, error);
90-
if (delResult == FAIL_ERROR)
91-
return FAIL_ERROR;
92-
else
93-
return AddPassword(service, account, password, error, false);
94-
} else if (result == FAIL_ERROR) {
82+
SecKeychainItemRef item;
83+
OSStatus result = SecKeychainFindGenericPassword(NULL,
84+
service.length(),
85+
service.data(),
86+
account.length(),
87+
account.data(),
88+
NULL,
89+
NULL,
90+
&item);
91+
92+
if (result == errSecItemNotFound) {
93+
return AddPassword(service, account, password, error);
94+
} else if (result != errSecSuccess) {
95+
*error = errorStatusToString(result);
96+
return FAIL_ERROR;
97+
}
98+
99+
result = SecKeychainItemModifyAttributesAndData(item,
100+
NULL,
101+
password.length(),
102+
password.data());
103+
CFRelease(item);
104+
if (result != errSecSuccess) {
105+
*error = errorStatusToString(result);
95106
return FAIL_ERROR;
96107
}
97108

0 commit comments

Comments
 (0)