From fca1dbd6ec1b6c377da21a394125f9f0c31fd2c5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 28 Mar 2019 14:09:39 -0400 Subject: [PATCH] secure storage service --- src/Core/Services/SecureStorageService.cs | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Core/Services/SecureStorageService.cs 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); + } + } +}