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