environment service

This commit is contained in:
Kyle Spearrin 2019-04-18 12:19:17 -04:00
parent b94485be75
commit 859788ca46
4 changed files with 142 additions and 0 deletions

View File

@ -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<EnvironmentUrlData> SetUrlsAsync(EnvironmentUrlData urls);
Task SetUrlsFromStorageAsync();
}
}

View File

@ -8,5 +8,6 @@
public static string PinProtectedKey = "pinProtectedKey";
public static string DefaultUriMatch = "defaultUriMatch";
public static string DisableAutoTotpCopyKey = "disableAutoTotpCopy";
public static string EnvironmentUrlsKey = "environmentUrls";
}
}

View File

@ -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; }
}
}

View File

@ -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<EnvironmentUrlData>(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<EnvironmentUrlData> 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();
}
}
}