bitwarden-android/src/App/Services/AppIdService.cs

42 lines
1.2 KiB
C#
Raw Normal View History

2016-06-22 05:29:29 +03:00
using System;
using Bit.App.Abstractions;
namespace Bit.App.Services
{
public class AppIdService : IAppIdService
{
private const string AppIdKey = "appId";
private const string AnonymousAppIdKey = "anonymousAppId";
private readonly ISecureStorageService _secureStorageService;
private Guid? _appId;
private Guid? _anonymousAppId;
2016-06-22 05:29:29 +03:00
public AppIdService(ISecureStorageService secureStorageService)
2016-06-22 05:29:29 +03:00
{
_secureStorageService = secureStorageService;
2016-06-22 05:29:29 +03:00
}
public string AppId => GetAppId(AppIdKey, ref _appId);
public string AnonymousAppId => GetAppId(AnonymousAppIdKey, ref _anonymousAppId);
private string GetAppId(string key, ref Guid? appId)
2016-06-22 05:29:29 +03:00
{
if(appId.HasValue)
2016-06-22 05:29:29 +03:00
{
return appId.Value.ToString();
}
2016-06-22 05:29:29 +03:00
var appIdBytes = _secureStorageService.Retrieve(key);
if(appIdBytes != null)
{
appId = new Guid(appIdBytes);
return appId.Value.ToString();
2016-06-22 05:29:29 +03:00
}
appId = Guid.NewGuid();
_secureStorageService.Store(key, appId.Value.ToByteArray());
return appId.Value.ToString();
2016-06-22 05:29:29 +03:00
}
}
}