diff --git a/src/Core/Services/SecureStorageService.cs b/src/Core/Services/SecureStorageService.cs new file mode 100644 index 000000000..dcede487e --- /dev/null +++ b/src/Core/Services/SecureStorageService.cs @@ -0,0 +1,53 @@ +using Bit.Core.Abstractions; +using System; +using System.Threading.Tasks; + +namespace Bit.Core.Services +{ + public class SecureStorageService : IStorageService + { + private string _keyFormat = "bwSecureStorage:{0}"; + + public async Task GetAsync(string key) + { + var objType = typeof(T); + if(objType == typeof(string)) + { + var formattedKey = string.Format(_keyFormat, key); + var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey); + return (T)(object)val; + } + else + { + throw new Exception("Unsupported object type for secure storage."); + } + } + + public async Task SaveAsync(string key, T obj) + { + if(obj == null) + { + await RemoveAsync(key); + return; + } + + var objType = typeof(T); + if(objType == typeof(string)) + { + var formattedKey = string.Format(_keyFormat, key); + await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, obj as string); + } + else + { + throw new Exception("Unsupported object type for secure storage."); + } + } + + public Task RemoveAsync(string key) + { + var formattedKey = string.Format(_keyFormat, key); + Xamarin.Essentials.SecureStorage.Remove(formattedKey); + return Task.FromResult(0); + } + } +}