secure storage service

This commit is contained in:
Kyle Spearrin 2019-03-28 14:09:39 -04:00
parent 364f25e22a
commit fca1dbd6ec
1 changed files with 53 additions and 0 deletions

View File

@ -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<T> GetAsync<T>(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<T>(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);
}
}
}