From d17cd66d675ec9de79c8a12b57d75079dba62eee Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 12 Feb 2020 18:26:56 +0200 Subject: Gateway --- .../Controllers/AzureUtilsController.cs | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs (limited to 'Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs') diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs new file mode 100644 index 000000000..2daf3c7e6 --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs @@ -0,0 +1,62 @@ +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Authentication; +using System.Web; +using System.Web.Mvc; +using Tango.MachineService.Gateway.Filters; +using Tango.MachineService.Gateway.Messages; +using Tango.Web.Controllers; +using Tango.Web.Security; + +namespace Tango.MachineService.Gateway.Controllers +{ + public class AzureUtilsController : TangoController + { + public class TokenObject + { + public String Email { get; set; } + } + + [HttpPost] + public LoginResponse Login(LoginRequest request) + { + var azure = AzureUtils.AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync().Result; + AzureUtils.ActiveDirectory.ActiveDirectoryManager adManager = new AzureUtils.ActiveDirectory.ActiveDirectoryManager(azure); + + try + { + adManager.Authenticate(request.Email, request.Password).GetAwaiter().GetResult(); + } + catch (Exception ex) + { + throw new AuthenticationException("The specified email or password is incorrect.", ex); + } + + try + { + adManager.IsUserMemberOf(MachineServiceGatewayConfig.AZURE_UTILS_GROUP, request.Email); + } + catch + { + throw new AuthenticationException("The specified user is not authorized to access the resource."); + } + + return new LoginResponse() + { + AccessToken = WebToken.CreateNew(MachineServiceGatewayConfig.JWT_TOKEN_SECRET, new TokenObject() + { + Email = request.Email, + }, DateTime.UtcNow.AddDays(1)).AccessToken, + }; + } + + [HttpPost] + [JwtTokenFilter] + public void DoSomethingSecret() + { + + } + } +} \ No newline at end of file -- cgit v1.3.1 From 678b22afc27e53811f978103b7ea41609ff68606 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Thu, 13 Feb 2020 23:09:44 +0200 Subject: Implemented and deployed machine service gateway. Implemented AzureUtils => Gateway. --- .../Azure/Tango.AzureUtils.UI/App.config | 15 ++- .../Tango.AzureUtils.UI/AzureDashboardViewModel.cs | 10 +- .../Tango.AzureUtils.UI/AzureUtilsSettings.cs | 38 ++++++++ .../Tango.AzureUtils.UI/Tango.AzureUtils.UI.csproj | 106 +++++++++++---------- .../ViewModels/EnvironmentCreationViewVM.cs | 26 +---- .../ViewModels/EnvironmentRemovalViewVM.cs | 27 +----- .../Tango.AzureUtils.UI/ViewModels/MainViewVM.cs | 33 ++++++- .../Views/EnvironmentCreationView.xaml | 8 +- .../Views/EnvironmentRemovalView.xaml | 8 +- .../Azure/Tango.AzureUtils.UI/Views/MainView.xaml | 17 +++- .../Azure/Tango.AzureUtils.UI/packages.config | 50 +++++----- .../ActiveDirectory/ActiveDirectoryManager.cs | 4 +- .../AzureUtilsAuthenticationFactory.cs | 49 +++++++++- .../Azure/Tango.AzureUtils/Tango.AzureUtils.csproj | 102 ++++++++++---------- .../Tango.AzureUtils/Web/AzureUtilsWebClient.cs | 44 +++------ .../Azure/Tango.AzureUtils/Web/LoginRequest.cs | 13 +++ .../Azure/Tango.AzureUtils/Web/LoginResponse.cs | 12 +++ .../Azure/Tango.AzureUtils/app.config | 8 ++ .../Azure/Tango.AzureUtils/packages.config | 50 +++++----- .../Controllers/AzureUtilsController.cs | 23 ++--- .../Filters/JwtTokenFilter.cs | 2 +- .../Tango.MachineService.Gateway/GatewayConfig.cs | 19 ++++ .../Tango.MachineService.Gateway/Global.asax.cs | 8 ++ .../MachineServiceGatewayConfig.cs | 19 ---- .../Messages/LoginRequest.cs | 13 --- .../Messages/LoginResponse.cs | 12 --- .../machineservice-gateway - Web Deploy.pubxml | 28 ++++++ .../Tango.MachineService.Gateway.csproj | 5 +- .../Web/Tango.MachineService.Gateway/Web.config | 20 ++++ 29 files changed, 455 insertions(+), 314 deletions(-) create mode 100644 Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureUtilsSettings.cs create mode 100644 Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginRequest.cs create mode 100644 Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginResponse.cs create mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/GatewayConfig.cs delete mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/MachineServiceGatewayConfig.cs delete mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginRequest.cs delete mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginResponse.cs create mode 100644 Software/Visual_Studio/Web/Tango.MachineService.Gateway/Properties/PublishProfiles/machineservice-gateway - Web Deploy.pubxml (limited to 'Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs') diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/App.config b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/App.config index f97c59da6..a142ac2b3 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/App.config +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/App.config @@ -1,9 +1,14 @@  - +
+ + + + + @@ -73,6 +78,14 @@ + + + + + + + + diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureDashboardViewModel.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureDashboardViewModel.cs index 3c6a95ebf..eddc7d009 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureDashboardViewModel.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureDashboardViewModel.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using System.Windows; using Tango.AzureUtils.UI.Managers; using Tango.Core.DI; +using Tango.Settings; using Tango.SharedUI; namespace Tango.AzureUtils.UI @@ -17,9 +18,16 @@ namespace Tango.AzureUtils.UI [TangoInject] public IStatusManager StatusManager { get; set; } - public virtual void OnApplicationReady() + public AzureUtilsSettings Settings { get; set; } + + public AzureDashboardViewModel() { + Settings = SettingsManager.Default.GetOrCreate(); + } + public virtual void OnApplicationReady() + { + } public virtual void OnAuthenticated(IAzure azure, List apps) diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureUtilsSettings.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureUtilsSettings.cs new file mode 100644 index 000000000..b6ed1845d --- /dev/null +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/AzureUtilsSettings.cs @@ -0,0 +1,38 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core.Cryptography; +using Tango.Settings; + +namespace Tango.AzureUtils.UI +{ + public class AzureUtilsSettings : SettingsBase + { + private Rfc2898Cryptographer _crypt; + + public AzureUtilsSettings() + { + _crypt = new Rfc2898Cryptographer(); + } + + public String Email { get; set; } + + public String EncryptedPassword { get; set; } + + [JsonIgnore] + public String Password + { + get + { + return _crypt.Decrypt(EncryptedPassword); + } + set + { + EncryptedPassword = _crypt.Encrypt(value); + } + } + } +} diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Tango.AzureUtils.UI.csproj b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Tango.AzureUtils.UI.csproj index 8f0546bfa..985c54c00 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Tango.AzureUtils.UI.csproj +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Tango.AzureUtils.UI.csproj @@ -56,80 +56,80 @@ ..\..\packages\Microsoft.Azure.KeyVault.WebKey.3.0.1\lib\net452\Microsoft.Azure.KeyVault.WebKey.dll - - ..\..\packages\Microsoft.Azure.Management.AppService.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.AppService.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.AppService.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.AppService.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.BatchAI.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.BatchAI.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.BatchAI.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.BatchAI.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Cdn.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Cdn.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Cdn.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Cdn.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Compute.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Compute.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Compute.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Compute.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerInstance.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerInstance.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerInstance.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerInstance.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerRegistry.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerRegistry.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerRegistry.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerRegistry.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerService.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerService.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerService.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerService.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.CosmosDB.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.CosmosDB.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.CosmosDB.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.CosmosDB.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Dns.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Dns.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Dns.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Dns.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.EventHub.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.EventHub.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.EventHub.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.EventHub.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Graph.RBAC.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Graph.RBAC.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Graph.RBAC.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Graph.RBAC.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.KeyVault.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.KeyVault.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.KeyVault.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.KeyVault.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Locks.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Locks.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Locks.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Locks.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Monitor.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Monitor.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Monitor.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Monitor.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Msi.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Msi.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Msi.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Msi.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Network.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Network.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Network.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Network.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.PrivateDns.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.PrivateDns.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.PrivateDns.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.PrivateDns.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Redis.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Redis.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Redis.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Redis.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ResourceManager.Fluent.1.30.0\lib\net461\Microsoft.Azure.Management.ResourceManager.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ResourceManager.Fluent.1.31.0\lib\net461\Microsoft.Azure.Management.ResourceManager.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Search.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Search.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Search.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Search.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ServiceBus.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ServiceBus.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ServiceBus.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ServiceBus.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Sql.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Sql.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Sql.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Sql.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Storage.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Storage.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Storage.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Storage.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.TrafficManager.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.TrafficManager.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.TrafficManager.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.TrafficManager.Fluent.dll ..\..\packages\Microsoft.Azure.Storage.Blob.11.1.2\lib\net452\Microsoft.Azure.Storage.Blob.dll @@ -166,6 +166,7 @@ + @@ -194,6 +195,7 @@ Designer + WebAppPropertiesControl.xaml @@ -311,6 +313,10 @@ {a34ee0f0-649d-41c8-8489-b6f1cc6924ee} Tango.Core + + {d8f1ad85-526a-4f50-b6dc-d437af63d8d8} + Tango.Settings + {8491D07B-C1F6-4B62-A412-41B9FD2D6538} Tango.SharedUI diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentCreationViewVM.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentCreationViewVM.cs index 7c3abd169..a4c65fcdd 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentCreationViewVM.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentCreationViewVM.cs @@ -41,23 +41,6 @@ namespace Tango.AzureUtils.UI.ViewModels set { _slotName = value; RaisePropertyChangedAuto(); } } - private String _email; - [Required(ErrorMessage = "Active directory email is required.")] - [EmailAddress(ErrorMessage = "Please enter a valid email.")] - public String Email - { - get { return _email; } - set { _email = value; RaisePropertyChangedAuto(); } - } - - private String _password; - [Required(ErrorMessage = "Password is required.")] - public String Password - { - get { return _password; } - set { _password = value; RaisePropertyChangedAuto(); } - } - private CreateEnvironmentConfiguration _config; public CreateEnvironmentConfiguration Config { @@ -73,11 +56,6 @@ namespace Tango.AzureUtils.UI.ViewModels Config = new CreateEnvironmentConfiguration(); } - public override void OnApplicationReady() - { - Email = "roy@twine-s.com"; - } - public override void OnAuthenticated(IAzure azure, List apps) { _machineServiceApp = apps.SingleOrDefault(x => x.Name == "MachineService"); @@ -97,8 +75,8 @@ namespace Tango.AzureUtils.UI.ViewModels IsFree = false; - Config.Email = Email; - Config.Password = Password; + Config.Email = Settings.Email; + Config.Password = Settings.Password; await _environmentManager.CreateEnvironment(_machineServiceApp as IWebApp, SelectedDeploymentSlot, SlotName, Config); } diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentRemovalViewVM.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentRemovalViewVM.cs index e8d966158..0917b012d 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentRemovalViewVM.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentRemovalViewVM.cs @@ -39,23 +39,6 @@ namespace Tango.AzureUtils.UI.ViewModels set { _slotName = value; RaisePropertyChangedAuto(); } } - private String _email; - [Required(ErrorMessage = "Active directory email is required.")] - [EmailAddress(ErrorMessage = "Please enter a valid email.")] - public String Email - { - get { return _email; } - set { _email = value; RaisePropertyChangedAuto(); } - } - - private String _password; - [Required(ErrorMessage = "Password is required.")] - public String Password - { - get { return _password; } - set { _password = value; RaisePropertyChangedAuto(); } - } - private RemoveEnvironmentConfiguration _config; public RemoveEnvironmentConfiguration Config { @@ -71,12 +54,6 @@ namespace Tango.AzureUtils.UI.ViewModels Config = new RemoveEnvironmentConfiguration(); } - public override void OnApplicationReady() - { - Email = "roy@twine-s.com"; - Password = "1Creativity"; - } - public override void OnAuthenticated(IAzure azure, List apps) { _machineServiceApp = apps.SingleOrDefault(x => x.Name == "MachineService"); @@ -96,8 +73,8 @@ namespace Tango.AzureUtils.UI.ViewModels IsFree = false; - Config.Email = Email; - Config.Password = Password; + Config.Email = Settings.Email; + Config.Password = Settings.Password; await _environmentManager.RemoveEnvironment(SelectedDeploymentSlot, SlotName, Config); diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/MainViewVM.cs index 4faf1369f..69543cb5a 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/MainViewVM.cs @@ -2,6 +2,7 @@ using Microsoft.Azure.Management.Fluent; using System; using System.Collections.Generic; +using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,6 +10,7 @@ using System.Windows.Input; using Tango.AzureUtils.Deployment; using Tango.AzureUtils.UI.Managers; using Tango.Core.Commands; +using Tango.Core.Cryptography; using Tango.Core.DI; using Tango.SharedUI; @@ -34,6 +36,21 @@ namespace Tango.AzureUtils.UI.ViewModels set { _progress = value; RaisePropertyChangedAuto(); } } + private String _email; + public String Email + { + get { return _email; } + set { _email = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private String _password; + public String Password + { + get { return _password; } + set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + #endregion #region Commands @@ -44,7 +61,14 @@ namespace Tango.AzureUtils.UI.ViewModels public MainViewVM() { - InitCommand = new RelayCommand(Init, () => !IsInitialized); + InitCommand = new RelayCommand(Init, () => !IsInitialized && !String.IsNullOrWhiteSpace(Email) && !String.IsNullOrWhiteSpace(Password)); + + try + { + Email = Settings.Email; + Password = Settings.Password; + } + catch { } } public override void OnApplicationReady() @@ -65,7 +89,12 @@ namespace Tango.AzureUtils.UI.ViewModels IsFree = false; StatusManager.UpdateStatus(AzureUtilsStage.Initializing, "Authenticating...", true); - _azure = await AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync(); + _azure = await AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync(ConfigurationManager.AppSettings.Get("GatewayUrl"), Email, Password); + + Settings.Email = Email; + Settings.Password = Password; + Settings.Save(); + List allApps = new List(); diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentCreationView.xaml b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentCreationView.xaml index 068e96318..05f917e5b 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentCreationView.xaml +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentCreationView.xaml @@ -51,16 +51,10 @@ - + Environment Name (e.g DEV) - - Active Directory Administrator Email - - - Password - diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentRemovalView.xaml b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentRemovalView.xaml index 2175aaaf3..3eed6575e 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentRemovalView.xaml +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/EnvironmentRemovalView.xaml @@ -41,16 +41,10 @@ - + Environment Name (e.g DEV) - - Active Directory Administrator Email - - - Password - diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/MainView.xaml b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/MainView.xaml index 40d431be1..b79d306d1 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/MainView.xaml +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/Views/MainView.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:helpers="clr-namespace:Tango.SharedUI.Helpers;assembly=Tango.SharedUI" xmlns:vm="clr-namespace:Tango.AzureUtils.UI.ViewModels" xmlns:global="clr-namespace:Tango.AzureUtils.UI" xmlns:views="clr-namespace:Tango.AzureUtils.UI.Views" @@ -24,7 +25,21 @@ Azure Utils - + + + + + Email + + + + + Password + + + + + diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/packages.config b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/packages.config index 14a601db9..95f192e49 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/packages.config +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/packages.config @@ -6,31 +6,31 @@ - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs index 26ce44b90..fad95df28 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/ActiveDirectory/ActiveDirectoryManager.cs @@ -34,7 +34,7 @@ namespace Tango.AzureUtils.ActiveDirectory { if (_adClient == null) { - var credentials = AzureUtilsAuthenticationFactory.GetCredentials(); + var credentials = AzureUtilsAuthenticationFactory.GetGlobalCredentials(); _adClient = new ActiveDirectoryClient(new Uri($"https://graph.windows.net/{credentials.TenantID}"), async () => await Task.FromResult(_authResult.AccessToken)); } return _adClient; @@ -70,7 +70,7 @@ namespace Tango.AzureUtils.ActiveDirectory OnProgress(AzureUtilsStage.ActiveDirectory, $"Authenticating with active directory graph..."); if (_authResult == null) { - var credentials = AzureUtilsAuthenticationFactory.GetCredentials(); + var credentials = AzureUtilsAuthenticationFactory.GetGlobalCredentials(); var authContext = new AuthenticationContext($"https://login.microsoftonline.com/{credentials.TenantID}"); authContext.TokenCache.Clear(); UserCredential userCredential = new UserPasswordCredential(email, password); diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs index 0d8b2ccfc..2006b492f 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs @@ -3,8 +3,10 @@ using Microsoft.Azure.Management.ResourceManager.Fluent; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Tango.AzureUtils.Web; namespace Tango.AzureUtils { @@ -19,13 +21,18 @@ namespace Tango.AzureUtils SubscriptionID = "10c8aa60-3b15-4e0d-b412-6aeef90e5e91" }; - public static void SetCredentials(AzureUtilsCredentials credentials) + public static void SetGlobalCredentials(AzureUtilsCredentials credentials) { _credentials = credentials; } public static Task AuthenticateOrGetAsync() { + if (_credentials == null) + { + throw new NullReferenceException("Credentials were not set."); + } + if (_azure == null) { return Task.Factory.StartNew(() => @@ -46,7 +53,45 @@ namespace Tango.AzureUtils } } - public static AzureUtilsCredentials GetCredentials() + public static Task AuthenticateOrGetAsync(AzureUtilsCredentials credentials) + { + return Task.Factory.StartNew(() => + { + var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal( + credentials.ClientID, + credentials.ClientSecret, + credentials.TenantID, + AzureEnvironment.AzureGlobalCloud); + + _azure = Azure.Authenticate(creds).WithSubscription(credentials.SubscriptionID); + return _azure; + }); + } + + public static async Task AuthenticateOrGetAsync(String gatewayUrl, String email, String password) + { + using (var http = new HttpClient()) + { + AzureUtilsWebClient client = new AzureUtilsWebClient(gatewayUrl, http); + var response = await client.LoginAsync(new LoginRequest() + { + Email = email, + Password = password + }); + + http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", response.AccessToken); + var c = await client.GetCredentialsAsync(); + return await AuthenticateOrGetAsync(new AzureUtilsCredentials() + { + ClientID = c.ClientID, + ClientSecret = c.ClientSecret, + TenantID = c.TenantID, + SubscriptionID = c.SubscriptionID + }); + } + } + + public static AzureUtilsCredentials GetGlobalCredentials() { return _credentials; } diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/Tango.AzureUtils.csproj b/Software/Visual_Studio/Azure/Tango.AzureUtils/Tango.AzureUtils.csproj index 11d589570..4a8daf233 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/Tango.AzureUtils.csproj +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/Tango.AzureUtils.csproj @@ -64,83 +64,83 @@ ..\..\packages\Microsoft.Azure.KeyVault.WebKey.3.0.1\lib\net452\Microsoft.Azure.KeyVault.WebKey.dll - - ..\..\packages\Microsoft.Azure.Management.AppService.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.AppService.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.AppService.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.AppService.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.BatchAI.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.BatchAI.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.BatchAI.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.BatchAI.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Cdn.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Cdn.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Cdn.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Cdn.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Compute.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Compute.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Compute.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Compute.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerInstance.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerInstance.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerInstance.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerInstance.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerRegistry.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerRegistry.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerRegistry.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerRegistry.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ContainerService.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ContainerService.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ContainerService.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ContainerService.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.CosmosDB.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.CosmosDB.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.CosmosDB.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.CosmosDB.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Dns.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Dns.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Dns.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Dns.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.EventHub.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.EventHub.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.EventHub.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.EventHub.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Graph.RBAC.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Graph.RBAC.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Graph.RBAC.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Graph.RBAC.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.KeyVault.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.KeyVault.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.KeyVault.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.KeyVault.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Locks.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Locks.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Locks.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Locks.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Monitor.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Monitor.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Monitor.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Monitor.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Msi.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Msi.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Msi.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Msi.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Network.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Network.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Network.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Network.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.PrivateDns.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.PrivateDns.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.PrivateDns.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.PrivateDns.Fluent.dll ..\..\packages\Microsoft.Azure.Management.RecoveryServices.Backup.0.1.2\lib\net40\Microsoft.Azure.Management.RecoveryServices.Backup.dll - - ..\..\packages\Microsoft.Azure.Management.Redis.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Redis.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Redis.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Redis.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ResourceManager.Fluent.1.30.0\lib\net461\Microsoft.Azure.Management.ResourceManager.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ResourceManager.Fluent.1.31.0\lib\net461\Microsoft.Azure.Management.ResourceManager.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Search.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Search.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Search.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Search.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.ServiceBus.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.ServiceBus.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.ServiceBus.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.ServiceBus.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Sql.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Sql.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Sql.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Sql.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.Storage.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.Storage.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.Storage.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.Storage.Fluent.dll - - ..\..\packages\Microsoft.Azure.Management.TrafficManager.Fluent.1.30.0\lib\net452\Microsoft.Azure.Management.TrafficManager.Fluent.dll + + ..\..\packages\Microsoft.Azure.Management.TrafficManager.Fluent.1.31.0\lib\net452\Microsoft.Azure.Management.TrafficManager.Fluent.dll ..\..\packages\Microsoft.Azure.Storage.Blob.11.1.2\lib\net452\Microsoft.Azure.Storage.Blob.dll @@ -253,6 +253,8 @@ + + diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/AzureUtilsWebClient.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/AzureUtilsWebClient.cs index 531bfb248..d62854987 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/AzureUtilsWebClient.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/AzureUtilsWebClient.cs @@ -123,31 +123,31 @@ namespace Tango.AzureUtils.Web } /// A server side error occurred. - public System.Threading.Tasks.Task DoSomethingSecretAsync() + public System.Threading.Tasks.Task GetCredentialsAsync() { - return DoSomethingSecretAsync(System.Threading.CancellationToken.None); + return GetCredentialsAsync(System.Threading.CancellationToken.None); } /// A server side error occurred. - public void DoSomethingSecret() + public AzureUtilsCredentials GetCredentials() { - System.Threading.Tasks.Task.Run(async () => await DoSomethingSecretAsync(System.Threading.CancellationToken.None)).GetAwaiter().GetResult(); + return System.Threading.Tasks.Task.Run(async () => await GetCredentialsAsync(System.Threading.CancellationToken.None)).GetAwaiter().GetResult(); } /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// A server side error occurred. - public async System.Threading.Tasks.Task DoSomethingSecretAsync(System.Threading.CancellationToken cancellationToken) + public async System.Threading.Tasks.Task GetCredentialsAsync(System.Threading.CancellationToken cancellationToken) { var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/AzureUtils/DoSomethingSecret"); + urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/AzureUtils/GetCredentials"); var client_ = _httpClient; try { using (var request_ = new System.Net.Http.HttpRequestMessage()) { - request_.Content = new System.Net.Http.StringContent(string.Empty, System.Text.Encoding.UTF8, "application/json"); - request_.Method = new System.Net.Http.HttpMethod("POST"); + request_.Method = new System.Net.Http.HttpMethod("GET"); + request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); @@ -167,9 +167,10 @@ namespace Tango.AzureUtils.Web ProcessResponse(client_, response_); var status_ = ((int)response_.StatusCode).ToString(); - if (status_ == "204") + if (status_ == "200") { - return; + var objectResponse_ = await ReadObjectResponseAsync(response_, headers_).ConfigureAwait(false); + return objectResponse_.Object; } else if (status_ != "200" && status_ != "204") @@ -177,6 +178,8 @@ namespace Tango.AzureUtils.Web var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); throw new ApiException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", (int)response_.StatusCode, responseData_, headers_, null); } + + return default(AzureUtilsCredentials); } finally { @@ -359,27 +362,6 @@ namespace Tango.AzureUtils.Web } } - [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.5.0 (Newtonsoft.Json v11.0.0.0)")] - public partial class LoginResponse - { - [Newtonsoft.Json.JsonProperty("AccessToken", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] - public string AccessToken { get; set; } - - - } - - [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.5.0 (Newtonsoft.Json v11.0.0.0)")] - public partial class LoginRequest - { - [Newtonsoft.Json.JsonProperty("Email", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] - public string Email { get; set; } - - [Newtonsoft.Json.JsonProperty("Password", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] - public string Password { get; set; } - - - } - /// Contains information for a single HTTP operation. [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.5.0 (Newtonsoft.Json v11.0.0.0)")] public partial class HttpControllerContext diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginRequest.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginRequest.cs new file mode 100644 index 000000000..e06281a5b --- /dev/null +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginRequest.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Tango.AzureUtils.Web +{ + public class LoginRequest + { + public String Email { get; set; } + public String Password { get; set; } + } +} \ No newline at end of file diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginResponse.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginResponse.cs new file mode 100644 index 000000000..a3be17700 --- /dev/null +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/Web/LoginResponse.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Tango.AzureUtils.Web +{ + public class LoginResponse + { + public String AccessToken { get; set; } + } +} \ No newline at end of file diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/app.config b/Software/Visual_Studio/Azure/Tango.AzureUtils/app.config index 9c4bc82cf..104b683be 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/app.config +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/app.config @@ -34,6 +34,14 @@ + + + + + + + + diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/packages.config b/Software/Visual_Studio/Azure/Tango.AzureUtils/packages.config index 57597d222..09feb04cd 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/packages.config +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/packages.config @@ -10,32 +10,32 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs index 2daf3c7e6..ae5aa4543 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs @@ -3,10 +3,12 @@ using System; using System.Collections.Generic; using System.Linq; using System.Security.Authentication; +using System.Threading.Tasks; using System.Web; using System.Web.Mvc; +using Tango.AzureUtils; +using Tango.AzureUtils.Web; using Tango.MachineService.Gateway.Filters; -using Tango.MachineService.Gateway.Messages; using Tango.Web.Controllers; using Tango.Web.Security; @@ -20,43 +22,38 @@ namespace Tango.MachineService.Gateway.Controllers } [HttpPost] - public LoginResponse Login(LoginRequest request) + public async Task Login(LoginRequest request) { - var azure = AzureUtils.AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync().Result; + var azure = await AzureUtils.AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync(); AzureUtils.ActiveDirectory.ActiveDirectoryManager adManager = new AzureUtils.ActiveDirectory.ActiveDirectoryManager(azure); try { - adManager.Authenticate(request.Email, request.Password).GetAwaiter().GetResult(); + await adManager.Authenticate(request.Email, request.Password); } catch (Exception ex) { throw new AuthenticationException("The specified email or password is incorrect.", ex); } - try - { - adManager.IsUserMemberOf(MachineServiceGatewayConfig.AZURE_UTILS_GROUP, request.Email); - } - catch + if (!adManager.IsUserMemberOf(GatewayConfig.AZURE_UTILS_GROUP, request.Email)) { throw new AuthenticationException("The specified user is not authorized to access the resource."); } return new LoginResponse() { - AccessToken = WebToken.CreateNew(MachineServiceGatewayConfig.JWT_TOKEN_SECRET, new TokenObject() + AccessToken = WebToken.CreateNew(GatewayConfig.JWT_TOKEN_SECRET, new TokenObject() { Email = request.Email, }, DateTime.UtcNow.AddDays(1)).AccessToken, }; } - [HttpPost] [JwtTokenFilter] - public void DoSomethingSecret() + public AzureUtilsCredentials GetCredentials() { - + return AzureUtils.AzureUtilsAuthenticationFactory.GetGlobalCredentials(); } } } \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Filters/JwtTokenFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Filters/JwtTokenFilter.cs index 4f2b26e82..841fd6baa 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Filters/JwtTokenFilter.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Filters/JwtTokenFilter.cs @@ -35,7 +35,7 @@ namespace Tango.MachineService.Gateway.Filters { try { - WebToken.Validate(MachineServiceGatewayConfig.JWT_TOKEN_SECRET, authorizationHeader.Parameter); + WebToken.Validate(GatewayConfig.JWT_TOKEN_SECRET, authorizationHeader.Parameter); } catch (JWT.TokenExpiredException) { diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/GatewayConfig.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/GatewayConfig.cs new file mode 100644 index 000000000..e7ad241ed --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/GatewayConfig.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Web; +using Tango.Web; + +namespace Tango.MachineService.Gateway +{ + public class GatewayConfig + { + public static String JWT_TOKEN_SECRET => ConfigurationManager.AppSettings[nameof(JWT_TOKEN_SECRET)].ToString(); + public static String AZURE_UTILS_GROUP => ConfigurationManager.AppSettings[nameof(AZURE_UTILS_GROUP)].ToString(); + public static String TENANT_ID => ConfigurationManager.AppSettings[nameof(TENANT_ID)].ToString(); + public static String CLIENT_ID => ConfigurationManager.AppSettings[nameof(CLIENT_ID)].ToString(); + public static String CLIENT_SECRET => ConfigurationManager.AppSettings[nameof(CLIENT_SECRET)].ToString(); + public static String SUBSCRIPTION_ID => ConfigurationManager.AppSettings[nameof(SUBSCRIPTION_ID)].ToString(); + } +} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Global.asax.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Global.asax.cs index 20df6c0bb..52b370236 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Global.asax.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Global.asax.cs @@ -26,6 +26,14 @@ namespace Tango.MachineService.Gateway FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); + + AzureUtils.AzureUtilsAuthenticationFactory.SetGlobalCredentials(new AzureUtils.AzureUtilsCredentials() + { + ClientID = GatewayConfig.CLIENT_ID, + ClientSecret = GatewayConfig.CLIENT_SECRET, + TenantID = GatewayConfig.TENANT_ID, + SubscriptionID = GatewayConfig.SUBSCRIPTION_ID + }); } public class LogExceptionFilterAttribute : ExceptionFilterAttribute diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/MachineServiceGatewayConfig.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/MachineServiceGatewayConfig.cs deleted file mode 100644 index 3a91ef181..000000000 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/MachineServiceGatewayConfig.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Linq; -using System.Web; -using Tango.Web; - -namespace Tango.MachineService.Gateway -{ - public class MachineServiceGatewayConfig - { - public static String JWT_TOKEN_SECRET => ConfigurationManager.AppSettings[nameof(JWT_TOKEN_SECRET)].ToString(); - public static String AZURE_UTILS_GROUP => ConfigurationManager.AppSettings[nameof(AZURE_UTILS_GROUP)].ToString(); - public static String TENANT_ID => ConfigurationManager.AppSettings[nameof(TENANT_ID)].ToString(); - public static String CLIENT_ID => ConfigurationManager.AppSettings[nameof(CLIENT_ID)].ToString(); - public static String CLIENT_SECRET => ConfigurationManager.AppSettings[nameof(CLIENT_SECRET)].ToString(); - public static String SUBSCRIPTION_ID => ConfigurationManager.AppSettings[nameof(SUBSCRIPTION_ID)].ToString(); - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginRequest.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginRequest.cs deleted file mode 100644 index 40a0cd744..000000000 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginRequest.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace Tango.MachineService.Gateway.Messages -{ - public class LoginRequest - { - public String Email { get; set; } - public String Password { get; set; } - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginResponse.cs b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginResponse.cs deleted file mode 100644 index da569ee99..000000000 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Messages/LoginResponse.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -namespace Tango.MachineService.Gateway.Messages -{ - public class LoginResponse - { - public String AccessToken { get; set; } - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Properties/PublishProfiles/machineservice-gateway - Web Deploy.pubxml b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Properties/PublishProfiles/machineservice-gateway - Web Deploy.pubxml new file mode 100644 index 000000000..8af9f7fc8 --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Properties/PublishProfiles/machineservice-gateway - Web Deploy.pubxml @@ -0,0 +1,28 @@ + + + + + MSDeploy + /subscriptions/10c8aa60-3b15-4e0d-b412-6aeef90e5e91/resourceGroups/Tango/providers/Microsoft.Web/sites/machineservice-gateway + Tango + AzureWebSite + Release + Any CPU + http://machineservice-gateway.azurewebsites.net + True + False + machineservice-gateway.scm.azurewebsites.net:443 + machineservice-gateway + + True + False + WMSVC + True + $machineservice-gateway + <_SavePWD>True + <_DestinationType>AzureWebSite + + \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Tango.MachineService.Gateway.csproj b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Tango.MachineService.Gateway.csproj index c9232d7b9..c1f3b59bd 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Tango.MachineService.Gateway.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Tango.MachineService.Gateway.csproj @@ -248,9 +248,7 @@ Global.asax - - - + @@ -273,6 +271,7 @@ + diff --git a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Web.config b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Web.config index 64ebb21b3..f53e9c92e 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Web.config +++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Web.config @@ -9,6 +9,26 @@ + + + + + + + + + + + + + + + + + + + + -- cgit v1.3.1