Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
GH-704 Handle duplicate item in keychain (#705)
Browse files Browse the repository at this point in the history
* Update CONTRIBUTING.md (#692)

* #704 if we get a duplicate item try to remove and then re-add if possible.
  • Loading branch information
jamesmontemagno authored Feb 19, 2019
1 parent ead1eba commit a7fc2c7
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Xamarin.Essentials/SecureStorage/SecureStorage.ios.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;

using Foundation;
Expand Down Expand Up @@ -99,8 +100,31 @@ internal void SetValueForKey(string value, string key, string service)
using (var newRecord = CreateRecordForNewKeyValue(key, value, service))
{
var result = SecKeyChain.Add(newRecord);
if (result != SecStatusCode.Success)
throw new Exception($"Error adding record: {result}");

switch (result)
{
case SecStatusCode.DuplicateItem:
{
Debug.WriteLine("Duplicate item found. Attempting to remove and add again.");

// try to remove and add again
if (Remove(key, service))
{
result = SecKeyChain.Add(newRecord);
if (result != SecStatusCode.Success)
throw new Exception($"Error adding record: {result}");
}
else
{
Debug.WriteLine("Unable to remove key.");
}
}
break;
case SecStatusCode.Success:
return;
default:
throw new Exception($"Error adding record: {result}");
}
}
}

Expand Down

0 comments on commit a7fc2c7

Please sign in to comment.