aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs
diff options
context:
space:
mode:
authorShlomo Hecht <shlomo@twine-s.com>2020-02-20 11:39:04 +0200
committerShlomo Hecht <shlomo@twine-s.com>2020-02-20 11:39:04 +0200
commit57a828b4d11ab8274053ee035c8de8014ddd4ca1 (patch)
treec88e63b5e9019fe67cc3be451e46fbe57efc4a35 /Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs
parent2d55102532afaccc447c8a28ded8ccb93437683b (diff)
parentd6e2772dd98e6880de14ea12be0ef53bae24f763 (diff)
downloadTango-57a828b4d11ab8274053ee035c8de8014ddd4ca1.tar.gz
Tango-57a828b4d11ab8274053ee035c8de8014ddd4ca1.zip
merge
Diffstat (limited to 'Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs')
-rw-r--r--Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs59
1 files changed, 59 insertions, 0 deletions
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..ae5aa4543
--- /dev/null
+++ b/Software/Visual_Studio/Web/Tango.MachineService.Gateway/Controllers/AzureUtilsController.cs
@@ -0,0 +1,59 @@
+using Microsoft.IdentityModel.Clients.ActiveDirectory;
+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.Web.Controllers;
+using Tango.Web.Security;
+
+namespace Tango.MachineService.Gateway.Controllers
+{
+ public class AzureUtilsController : TangoController<AzureUtilsController.TokenObject>
+ {
+ public class TokenObject
+ {
+ public String Email { get; set; }
+ }
+
+ [HttpPost]
+ public async Task<LoginResponse> Login(LoginRequest request)
+ {
+ var azure = await AzureUtils.AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync();
+ AzureUtils.ActiveDirectory.ActiveDirectoryManager adManager = new AzureUtils.ActiveDirectory.ActiveDirectoryManager(azure);
+
+ try
+ {
+ await adManager.Authenticate(request.Email, request.Password);
+ }
+ catch (Exception ex)
+ {
+ throw new AuthenticationException("The specified email or password is incorrect.", ex);
+ }
+
+ 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<TokenObject>.CreateNew(GatewayConfig.JWT_TOKEN_SECRET, new TokenObject()
+ {
+ Email = request.Email,
+ }, DateTime.UtcNow.AddDays(1)).AccessToken,
+ };
+ }
+
+ [JwtTokenFilter]
+ public AzureUtilsCredentials GetCredentials()
+ {
+ return AzureUtils.AzureUtilsAuthenticationFactory.GetGlobalCredentials();
+ }
+ }
+} \ No newline at end of file