mirror of
https://github.com/bitwarden/mobile.git
synced 2024-12-24 16:38:22 +01:00
secure storage service
This commit is contained in:
parent
364f25e22a
commit
fca1dbd6ec
53
src/Core/Services/SecureStorageService.cs
Normal file
53
src/Core/Services/SecureStorageService.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user