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. --- .../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 ++++++++++++++++ 10 files changed, 88 insertions(+), 61 deletions(-) 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') 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