mirror of
https://github.com/bitwarden/mobile
synced 2026-01-18 16:33:15 +00:00
move some xamarin specific services to app proj
This commit is contained in:
56
src/App/Services/SecureStorageService.cs
Normal file
56
src/App/Services/SecureStorageService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user