diff --git a/src/Core/Abstractions/IAppIdService.cs b/src/Core/Abstractions/IAppIdService.cs new file mode 100644 index 000000000..6c3bd6d30 --- /dev/null +++ b/src/Core/Abstractions/IAppIdService.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; + +namespace Bit.Core.Abstractions +{ + public interface IAppIdService + { + Task GetAppIdAsync(); + Task GetAnonymousAppIdAsync(); + } +} diff --git a/src/Core/Services/AppIdService.cs b/src/Core/Services/AppIdService.cs new file mode 100644 index 000000000..d5210ee09 --- /dev/null +++ b/src/Core/Services/AppIdService.cs @@ -0,0 +1,38 @@ +using Bit.Core.Abstractions; +using System; +using System.Threading.Tasks; + +namespace Bit.Core.Services +{ + public class AppIdService + { + private readonly IStorageService _storageService; + + public AppIdService(IStorageService storageService) + { + _storageService = storageService; + } + + public Task GetAppIdAsync() + { + return MakeAndGetAppIdAsync("appId"); + } + + public Task GetAnonymousAppIdAsync() + { + return MakeAndGetAppIdAsync("anonymousAppId"); + } + + private async Task MakeAndGetAppIdAsync(string key) + { + var existingId = await _storageService.GetAsync(key); + if(existingId != null) + { + return existingId; + } + var guid = Guid.NewGuid().ToString(); + await _storageService.SaveAsync(key, guid); + return guid; + } + } +}