mirror of
https://github.com/bitwarden/android.git
synced 2025-01-01 05:48:34 +03:00
56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
using Bit.Core.Abstractions;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Bit.App.Services
|
|
{
|
|
public class SecureStorageService : IStorageService
|
|
{
|
|
private readonly string _keyFormat = "bwSecureStorage:{0}";
|
|
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
|
{
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
};
|
|
|
|
public async Task<T> GetAsync<T>(string key)
|
|
{
|
|
var formattedKey = string.Format(_keyFormat, key);
|
|
var val = await Xamarin.Essentials.SecureStorage.GetAsync(formattedKey);
|
|
if(typeof(T) == typeof(string))
|
|
{
|
|
return (T)(object)val;
|
|
}
|
|
else
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(val, _jsonSettings);
|
|
}
|
|
}
|
|
|
|
public async Task SaveAsync<T>(string key, T obj)
|
|
{
|
|
if(obj == null)
|
|
{
|
|
await RemoveAsync(key);
|
|
return;
|
|
}
|
|
var formattedKey = string.Format(_keyFormat, key);
|
|
if(typeof(T) == typeof(string))
|
|
{
|
|
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey, obj as string);
|
|
}
|
|
else
|
|
{
|
|
await Xamarin.Essentials.SecureStorage.SetAsync(formattedKey,
|
|
JsonConvert.SerializeObject(obj, _jsonSettings));
|
|
}
|
|
}
|
|
|
|
public Task RemoveAsync(string key)
|
|
{
|
|
var formattedKey = string.Format(_keyFormat, key);
|
|
Xamarin.Essentials.SecureStorage.Remove(formattedKey);
|
|
return Task.FromResult(0);
|
|
}
|
|
}
|
|
}
|