diff --git a/src/Core/Abstractions/IEnvironmentService.cs b/src/Core/Abstractions/IEnvironmentService.cs new file mode 100644 index 000000000..5df4e0dd2 --- /dev/null +++ b/src/Core/Abstractions/IEnvironmentService.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using Bit.Core.Models.Data; + +namespace Bit.Core.Abstractions +{ + public interface IEnvironmentService + { + string ApiUrl { get; set; } + string BaseUrl { get; set; } + string IconsUrl { get; set; } + string IdentityUrl { get; set; } + string NotificationsUrl { get; set; } + string WebVaultUrl { get; set; } + + string GetWebVaultUrl(); + Task SetUrlsAsync(EnvironmentUrlData urls); + Task SetUrlsFromStorageAsync(); + } +} \ No newline at end of file diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs index 7b81571b2..42b0d6087 100644 --- a/src/Core/Constants.cs +++ b/src/Core/Constants.cs @@ -8,5 +8,6 @@ public static string PinProtectedKey = "pinProtectedKey"; public static string DefaultUriMatch = "defaultUriMatch"; public static string DisableAutoTotpCopyKey = "disableAutoTotpCopy"; + public static string EnvironmentUrlsKey = "environmentUrls"; } } diff --git a/src/Core/Models/Data/EnvironmentUrlData.cs b/src/Core/Models/Data/EnvironmentUrlData.cs new file mode 100644 index 000000000..ae510a264 --- /dev/null +++ b/src/Core/Models/Data/EnvironmentUrlData.cs @@ -0,0 +1,12 @@ +namespace Bit.Core.Models.Data +{ + public class EnvironmentUrlData + { + public string Base { get; set; } + public string Api { get; set; } + public string Identity { get; set; } + public string Icons { get; set; } + public string Notifications { get; set; } + public string WebVault { get; set; } + } +} diff --git a/src/Core/Services/EnvironmentService.cs b/src/Core/Services/EnvironmentService.cs new file mode 100644 index 000000000..68eb6b8d0 --- /dev/null +++ b/src/Core/Services/EnvironmentService.cs @@ -0,0 +1,110 @@ +using Bit.Core.Abstractions; +using Bit.Core.Models.Data; +using Bit.Core.Models.Domain; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Bit.Core.Services +{ + public class EnvironmentService : IEnvironmentService + { + private readonly IApiService _apiService; + private readonly IStorageService _storageService; + + public EnvironmentService( + IApiService apiService, + IStorageService storageService) + { + _apiService = apiService; + _storageService = storageService; + } + + public string BaseUrl { get; set; } + public string WebVaultUrl { get; set; } + public string ApiUrl { get; set; } + public string IdentityUrl { get; set; } + public string IconsUrl { get; set; } + public string NotificationsUrl { get; set; } + + public string GetWebVaultUrl() + { + if(!string.IsNullOrWhiteSpace(WebVaultUrl)) + { + return WebVaultUrl; + } + else if(string.IsNullOrWhiteSpace(BaseUrl)) + { + return BaseUrl; + } + return null; + } + + public async Task SetUrlsFromStorageAsync() + { + var urls = await _storageService.GetAsync(Constants.EnvironmentUrlsKey); + if(urls == null) + { + urls = new EnvironmentUrlData(); + } + var envUrls = new EnvironmentUrls(); + if(!string.IsNullOrWhiteSpace(urls.Base)) + { + BaseUrl = envUrls.Base = urls.Base; + _apiService.SetUrls(envUrls); + return; + } + WebVaultUrl = urls.WebVault; + ApiUrl = envUrls.Api = urls.Api; + IdentityUrl = envUrls.Identity = urls.Identity; + IconsUrl = urls.Icons; + NotificationsUrl = urls.Notifications; + _apiService.SetUrls(envUrls); + } + + public async Task SetUrlsAsync(EnvironmentUrlData urls) + { + urls.Base = FormatUrl(urls.Base); + urls.WebVault = FormatUrl(urls.WebVault); + urls.Api = FormatUrl(urls.Api); + urls.Identity = FormatUrl(urls.Identity); + urls.Icons = FormatUrl(urls.Icons); + urls.Notifications = FormatUrl(urls.Notifications); + await _storageService.SaveAsync(Constants.EnvironmentUrlsKey, urls); + BaseUrl = urls.Base; + WebVaultUrl = urls.WebVault; + ApiUrl = urls.Api; + IdentityUrl = urls.Identity; + IconsUrl = urls.Icons; + NotificationsUrl = urls.Notifications; + + var envUrls = new EnvironmentUrls(); + if(!string.IsNullOrWhiteSpace(BaseUrl)) + { + envUrls.Base = BaseUrl; + } + else + { + envUrls.Api = ApiUrl; + envUrls.Identity = IdentityUrl; + } + + _apiService.SetUrls(envUrls); + // TODO: init notifications service + return urls; + } + + private string FormatUrl(string url) + { + if(string.IsNullOrWhiteSpace(url)) + { + return null; + } + url = Regex.Replace(url, "\\/+$", string.Empty); + if(!url.StartsWith("http://") && !url.StartsWith("https://")) + { + url = string.Concat("https://", url); + } + return url.Trim(); + } + } +}