From 910f0083cd835723c4d1546833834061ef154644 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 28 Aug 2017 17:50:17 -0400 Subject: [PATCH] allow setting vault url for environment --- .../Services/IAppSettingsService.cs | 1 + src/App/Constants.cs | 1 + src/App/Pages/EnvironmentPage.cs | 23 ++++++++++++++++++- src/App/Pages/LoginTwoFactorPage.cs | 14 ++++++++++- src/App/Resources/AppResources.Designer.cs | 9 ++++++++ src/App/Resources/AppResources.resx | 3 +++ src/App/Services/AppSettingsService.cs | 18 +++++++++++++++ 7 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/App/Abstractions/Services/IAppSettingsService.cs b/src/App/Abstractions/Services/IAppSettingsService.cs index fce11e1cb..dc4c4dab0 100644 --- a/src/App/Abstractions/Services/IAppSettingsService.cs +++ b/src/App/Abstractions/Services/IAppSettingsService.cs @@ -11,6 +11,7 @@ namespace Bit.App.Abstractions bool AutofillPasswordField { get; set; } string SecurityStamp { get; set; } string BaseUrl { get; set; } + string VaultUrl { get; set; } string ApiUrl { get; set; } string IdentityUrl { get; set; } } diff --git a/src/App/Constants.cs b/src/App/Constants.cs index e745d1dfe..8dfeeb6f5 100644 --- a/src/App/Constants.cs +++ b/src/App/Constants.cs @@ -35,6 +35,7 @@ public const string LastSync = "other:lastSync"; public const string LastBuildKey = "LastBuild"; public const string BaseUrl = "other:baseUrl"; + public const string VaultUrl = "other:vaultUrl"; public const string ApiUrl = "other:apiUrl"; public const string IdentityUrl = "other:identityUrl"; diff --git a/src/App/Pages/EnvironmentPage.cs b/src/App/Pages/EnvironmentPage.cs index 049c739c3..35858c578 100644 --- a/src/App/Pages/EnvironmentPage.cs +++ b/src/App/Pages/EnvironmentPage.cs @@ -26,6 +26,7 @@ namespace Bit.App.Pages } public FormEntryCell BaseUrlCell { get; set; } + public FormEntryCell VaultUrlCell { get; set; } public FormEntryCell ApiUrlCell { get; set; } public FormEntryCell IdentityUrlCell { get; set; } public StackLayout StackLayout { get; set; } @@ -40,7 +41,9 @@ namespace Bit.App.Pages IdentityUrlCell.Entry.Text = _appSettings.IdentityUrl; ApiUrlCell = new FormEntryCell(AppResources.ApiUrl, nextElement: IdentityUrlCell.Entry, entryKeyboard: Keyboard.Url); ApiUrlCell.Entry.Text = _appSettings.ApiUrl; - BaseUrlCell = new FormEntryCell(AppResources.ServerUrl, nextElement: ApiUrlCell.Entry, entryKeyboard: Keyboard.Url); + VaultUrlCell = new FormEntryCell(AppResources.VaultUrl, nextElement: ApiUrlCell.Entry, entryKeyboard: Keyboard.Url); + VaultUrlCell.Entry.Text = _appSettings.VaultUrl; + BaseUrlCell = new FormEntryCell(AppResources.ServerUrl, nextElement: VaultUrlCell.Entry, entryKeyboard: Keyboard.Url); BaseUrlCell.Entry.Text = _appSettings.BaseUrl; var table = new FormTableView @@ -69,6 +72,7 @@ namespace Bit.App.Pages { new TableSection(AppResources.CustomEnvironment) { + VaultUrlCell, ApiUrlCell, IdentityUrlCell } @@ -120,6 +124,7 @@ namespace Bit.App.Pages BaseUrlCell.InitEvents(); IdentityUrlCell.InitEvents(); ApiUrlCell.InitEvents(); + VaultUrlCell.InitEvents(); StackLayout.LayoutChanged += Layout_LayoutChanged; BaseUrlCell.Entry.FocusWithDelay(); } @@ -129,6 +134,7 @@ namespace Bit.App.Pages BaseUrlCell.Dispose(); IdentityUrlCell.Dispose(); ApiUrlCell.Dispose(); + VaultUrlCell.Dispose(); StackLayout.LayoutChanged -= Layout_LayoutChanged; } @@ -156,6 +162,20 @@ namespace Bit.App.Pages BaseUrlCell.Entry.Text = null; } + if(!string.IsNullOrWhiteSpace(VaultUrlCell.Entry.Text)) + { + VaultUrlCell.Entry.Text = FixUrl(VaultUrlCell.Entry.Text); + if(!Uri.TryCreate(VaultUrlCell.Entry.Text, UriKind.Absolute, out result)) + { + _userDialogs.Alert(string.Format(AppResources.FormattedIncorrectly, AppResources.VaultUrl)); + return; + } + } + else + { + VaultUrlCell.Entry.Text = null; + } + if(!string.IsNullOrWhiteSpace(ApiUrlCell.Entry.Text)) { ApiUrlCell.Entry.Text = FixUrl(ApiUrlCell.Entry.Text); @@ -187,6 +207,7 @@ namespace Bit.App.Pages _appSettings.BaseUrl = BaseUrlCell.Entry.Text; _appSettings.IdentityUrl = IdentityUrlCell.Entry.Text; _appSettings.ApiUrl = ApiUrlCell.Entry.Text; + _appSettings.VaultUrl = VaultUrlCell.Entry.Text; _userDialogs.Toast(AppResources.EnvironmentSaved); _googleAnalyticsService.TrackAppEvent("SetEnvironmentUrls"); await Navigation.PopForDeviceAsync(); diff --git a/src/App/Pages/LoginTwoFactorPage.cs b/src/App/Pages/LoginTwoFactorPage.cs index 40c4c3899..90711d4b8 100644 --- a/src/App/Pages/LoginTwoFactorPage.cs +++ b/src/App/Pages/LoginTwoFactorPage.cs @@ -26,6 +26,7 @@ namespace Bit.App.Pages private IGoogleAnalyticsService _googleAnalyticsService; private ITwoFactorApiRepository _twoFactorApiRepository; private IPushNotification _pushNotification; + private IAppSettingsService _appSettingsService; private readonly string _email; private readonly string _masterPasswordHash; private readonly SymmetricCryptoKey _key; @@ -48,6 +49,7 @@ namespace Bit.App.Pages _authService = Resolver.Resolve(); _userDialogs = Resolver.Resolve(); _syncService = Resolver.Resolve(); + _appSettingsService = Resolver.Resolve(); _googleAnalyticsService = Resolver.Resolve(); _twoFactorApiRepository = Resolver.Resolve(); _pushNotification = Resolver.Resolve(); @@ -185,9 +187,19 @@ namespace Bit.App.Pages var host = WebUtility.UrlEncode(duoParams["Host"].ToString()); var req = WebUtility.UrlEncode(duoParams["Signature"].ToString()); + var vaultUrl = "https://vault.bitwarden.com"; + if(!string.IsNullOrWhiteSpace(_appSettingsService.BaseUrl)) + { + vaultUrl = _appSettingsService.BaseUrl; + } + else if(!string.IsNullOrWhiteSpace(_appSettingsService.VaultUrl)) + { + vaultUrl = _appSettingsService.VaultUrl; + } + var webView = new HybridWebView { - Uri = $"https://vault.bitwarden.com/duo-connector.html?host={host}&request={req}", + Uri = $"{vaultUrl}/duo-connector.html?host={host}&request={req}", HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, MinimumHeightRequest = 400 diff --git a/src/App/Resources/AppResources.Designer.cs b/src/App/Resources/AppResources.Designer.cs index 68fd5b1a1..6638d0627 100644 --- a/src/App/Resources/AppResources.Designer.cs +++ b/src/App/Resources/AppResources.Designer.cs @@ -2392,6 +2392,15 @@ namespace Bit.App.Resources { } } + /// + /// Looks up a localized string similar to Vault Server URL. + /// + public static string VaultUrl { + get { + return ResourceManager.GetString("VaultUrl", resourceCulture); + } + } + /// /// Looks up a localized string similar to Verification Code. /// diff --git a/src/App/Resources/AppResources.resx b/src/App/Resources/AppResources.resx index 760e4e30f..d2bff3499 100644 --- a/src/App/Resources/AppResources.resx +++ b/src/App/Resources/AppResources.resx @@ -1024,4 +1024,7 @@ Server URL + + Vault Server URL + \ No newline at end of file diff --git a/src/App/Services/AppSettingsService.cs b/src/App/Services/AppSettingsService.cs index bec836626..acf219a3c 100644 --- a/src/App/Services/AppSettingsService.cs +++ b/src/App/Services/AppSettingsService.cs @@ -104,6 +104,24 @@ namespace Bit.App.Services } } + public string VaultUrl + { + get + { + return _settings.GetValueOrDefault(Constants.VaultUrl); + } + set + { + if(value == null) + { + _settings.Remove(Constants.VaultUrl); + return; + } + + _settings.AddOrUpdateValue(Constants.VaultUrl, value); + } + } + public string ApiUrl { get