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";
|
2016-08-07 02:03:48 +03:00
|
|
|
|
private const string AnonymousAppIdKey = "anonymousAppId";
|
2016-07-28 01:46:55 +03:00
|
|
|
|
private readonly ISecureStorageService _secureStorageService;
|
|
|
|
|
private Guid? _appId;
|
2016-08-07 02:03:48 +03:00
|
|
|
|
private Guid? _anonymousAppId;
|
2016-06-22 05:29:29 +03:00
|
|
|
|
|
2016-07-28 01:46:55 +03:00
|
|
|
|
public AppIdService(ISecureStorageService secureStorageService)
|
2016-06-22 05:29:29 +03:00
|
|
|
|
{
|
2016-07-28 01:46:55 +03:00
|
|
|
|
_secureStorageService = secureStorageService;
|
2016-06-22 05:29:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 02:03:48 +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
|
|
|
|
{
|
2016-08-07 02:03:48 +03:00
|
|
|
|
if(appId.HasValue)
|
2016-06-22 05:29:29 +03:00
|
|
|
|
{
|
2016-08-07 02:03:48 +03:00
|
|
|
|
return appId.Value.ToString();
|
|
|
|
|
}
|
2016-06-22 05:29:29 +03:00
|
|
|
|
|
2016-08-07 02:03:48 +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
|
|
|
|
}
|
2016-08-07 02:03:48 +03:00
|
|
|
|
|
|
|
|
|
appId = Guid.NewGuid();
|
|
|
|
|
_secureStorageService.Store(key, appId.Value.ToByteArray());
|
|
|
|
|
return appId.Value.ToString();
|
2016-06-22 05:29:29 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|