@@ -57,8 +57,7 @@ const std::string errorStatusToString(OSStatus status) {
57
57
KEYTAR_OP_RESULT AddPassword (const std::string& service,
58
58
const std::string& account,
59
59
const std::string& password,
60
- std::string* error,
61
- bool returnNonfatalOnDuplicate) {
60
+ std::string* error) {
62
61
OSStatus status = SecKeychainAddGenericPassword (NULL ,
63
62
service.length (),
64
63
service.data (),
@@ -68,9 +67,7 @@ KEYTAR_OP_RESULT AddPassword(const std::string& service,
68
67
password.data (),
69
68
NULL );
70
69
71
- if (status == errSecDuplicateItem && returnNonfatalOnDuplicate) {
72
- return FAIL_NONFATAL;
73
- } else if (status != errSecSuccess) {
70
+ if (status != errSecSuccess) {
74
71
*error = errorStatusToString (status);
75
72
return FAIL_ERROR;
76
73
}
@@ -82,16 +79,30 @@ KEYTAR_OP_RESULT SetPassword(const std::string& service,
82
79
const std::string& account,
83
80
const std::string& password,
84
81
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);
95
106
return FAIL_ERROR;
96
107
}
97
108
0 commit comments