From 9447a8a09f87d6ea2cb62860021c595386668eec Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 20 Feb 2019 22:55:15 +0200 Subject: A lot of work !!! --- .../Tango.MachineService/App_Start/WebApiConfig.cs | 8 ++-- .../Controllers/MachineStudioController.cs | 26 +++++++----- .../Controllers/PPCController.cs | 42 ++++++++------------ .../Tango.MachineService/Filters/JwtTokenFilter.cs | 46 ++++++++++++++++++++++ .../Filters/MachineStudioLoginFilter.cs | 35 ---------------- .../Tango.MachineService/Filters/PPCLoginFilter.cs | 35 ---------------- .../Tango.MachineService/MachineServiceConfig.cs | 1 + .../Tango.MachineService.csproj | 12 +++--- .../Web/Tango.MachineService/Web.config | 9 +++-- .../Web/Tango.MachineService/packages.config | 3 +- 10 files changed, 98 insertions(+), 119 deletions(-) create mode 100644 Software/Visual_Studio/Web/Tango.MachineService/Filters/JwtTokenFilter.cs delete mode 100644 Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs delete mode 100644 Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs (limited to 'Software/Visual_Studio/Web/Tango.MachineService') diff --git a/Software/Visual_Studio/Web/Tango.MachineService/App_Start/WebApiConfig.cs b/Software/Visual_Studio/Web/Tango.MachineService/App_Start/WebApiConfig.cs index 0a63e9acb..73265328c 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/App_Start/WebApiConfig.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/App_Start/WebApiConfig.cs @@ -22,10 +22,10 @@ namespace Tango.MachineService defaults: new { id = RouteParameter.Optional }); config.Formatters.Insert(0, new ProtoBufFormatter()); - config.Formatters.Insert(1, new JsonNetFormatter(new JsonSerializerSettings() - { - PreserveReferencesHandling = PreserveReferencesHandling.All, - })); + //config.Formatters.Insert(1, new JsonNetFormatter(new JsonSerializerSettings() + //{ + // PreserveReferencesHandling = PreserveReferencesHandling.All, + //})); } } } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index 054c8d781..467b6ae28 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -24,12 +24,15 @@ using Tango.MachineService.Filters; namespace Tango.MachineService.Controllers { - public class MachineStudioController : JsonController + public class MachineStudioController : TangoController { private static List _pendingUploads; private ActiveDirectoryManager _ad_manager; - public static TokensManager TokensManager { get; set; } + public class TokenObject + { + public String UserGuid { get; set; } + } #region Constructors @@ -38,8 +41,6 @@ namespace Tango.MachineService.Controllers /// static MachineStudioController() { - TokensManager = new TokensManager(); - TokensManager.ExpirationTime = TimeSpan.FromDays(10); _pendingUploads = new List(); } @@ -61,11 +62,13 @@ namespace Tango.MachineService.Controllers /// The request. /// [HttpPost] - [MachineStudioLoginFilter] + [JwtTokenFilter] public CheckForUpdatesResponse CheckForUpdates(CheckForUpdatesRequest request) { LogManager.Log("Request received..."); + var userID = RequestToken.Object.UserGuid; + CheckForUpdatesResponse response = new CheckForUpdatesResponse(); using (ObservablesContext db = ObservablesContextHelper.CreateContext()) @@ -103,7 +106,7 @@ namespace Tango.MachineService.Controllers /// The request. /// [HttpPost] - [MachineStudioLoginFilter] + [JwtTokenFilter] public DownloadLatestVersionResponse DownloadLatestVersion(DownloadLatestVersionRequest request) { LogManager.Log("Request received..."); @@ -138,14 +141,14 @@ namespace Tango.MachineService.Controllers /// New version must be greater than latest version. /// Invalid user credentials. [HttpPost] - [MachineStudioLoginFilter] + [JwtTokenFilter] public UploadVersionResponse UploadVersion(UploadVersionRequest request) { UploadVersionResponse response = new UploadVersionResponse(); using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - String userID = TokensManager.GetTokenObject(request.AccessToken); + String userID = RequestToken.Object.UserGuid; var user = new UserBuilder(db).Set(userID).WithRolesAndPermissions().Build(); @@ -195,7 +198,7 @@ namespace Tango.MachineService.Controllers /// /// Invalid Token. [HttpPost] - [MachineStudioLoginFilter] + [JwtTokenFilter] public UploadCompletedResponse NotifyUploadCompleted(UploadCompletedRequest request) { MachineStudioPendingUpload upload = _pendingUploads.FirstOrDefault(x => x.Token == request.Token); @@ -347,7 +350,10 @@ namespace Tango.MachineService.Controllers Password = request.Password, }, - WebToken = TokensManager.GetOrCreate(user.Guid), + AccessToken = WebToken.CreateNew(MachineServiceConfig.JWT_TOKEN_SECRET, new TokenObject() + { + UserGuid = user.Guid, + }, DateTime.UtcNow.AddDays(1)).AccessToken, VersionChangeRequired = versionChangeRequired, RequiredVersion = requiredVersion, }; diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs index 70d64be51..912d2e5e9 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -26,30 +26,22 @@ using Tango.MachineService.Filters; namespace Tango.MachineService.Controllers { - public class PPCController : JsonController + public class PPCController : TangoController { private static List _pendingUploads; private ActiveDirectoryManager _ad_manager; - public class TokenObject : IEquatable + public class TokenObject { public LoginMode Mode { get; set; } public String UserGuid { get; set; } public String MachineGuid { get; set; } - - public bool Equals(TokenObject other) - { - return UserGuid == other.UserGuid || MachineGuid == MachineGuid; - } } - public static TokensManager TokensManager { get; set; } - #region Constructors static PPCController() { - TokensManager = new TokensManager(); _pendingUploads = new List(); } @@ -62,8 +54,8 @@ namespace Tango.MachineService.Controllers #region Setup & Update - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public MachineSetupResponse MachineSetup(MachineSetupRequest request) { MachineSetupResponse response = new MachineSetupResponse(); @@ -73,7 +65,7 @@ namespace Tango.MachineService.Controllers using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { db.Configuration.LazyLoadingEnabled = false; - String machine_guid = TokensManager.GetTokenObject(request.AccessToken).MachineGuid; + String machine_guid = RequestToken.Object.MachineGuid; var machine = db.Machines.SingleOrDefault(x => x.Guid == machine_guid); @@ -136,8 +128,8 @@ namespace Tango.MachineService.Controllers return response; } - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public DownloadUpdateResponse MachineUpdate(DownloadUpdateRequest request) { DownloadUpdateResponse response = new DownloadUpdateResponse(); @@ -145,7 +137,7 @@ namespace Tango.MachineService.Controllers using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { db.Configuration.LazyLoadingEnabled = false; - String machine_guid = TokensManager.GetTokenObject(request.AccessToken).MachineGuid; + String machine_guid = RequestToken.Object.MachineGuid; var machine = db.Machines.SingleOrDefault(x => x.Guid == machine_guid); @@ -195,15 +187,15 @@ namespace Tango.MachineService.Controllers return response; } - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public CheckForUpdateResponse CheckForUpdate(CheckForUpdateRequest request) { CheckForUpdateResponse response = new CheckForUpdateResponse(); using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - String machine_guid = TokensManager.GetTokenObject(request.AccessToken).MachineGuid; + String machine_guid = RequestToken.Object.MachineGuid; var machine = db.Machines.SingleOrDefault(x => x.Guid == machine_guid); @@ -227,8 +219,8 @@ namespace Tango.MachineService.Controllers return response; } - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public UpdateDBResponse UpdateDB(UpdateDBRequest request) { UpdateDBResponse response = new UpdateDBResponse(); @@ -236,7 +228,7 @@ namespace Tango.MachineService.Controllers using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - String machine_guid = TokensManager.GetTokenObject(request.AccessToken).MachineGuid; + String machine_guid = RequestToken.Object.MachineGuid; var machine = db.Machines.SingleOrDefault(x => x.Guid == machine_guid); @@ -301,8 +293,8 @@ namespace Tango.MachineService.Controllers return response; } - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public UploadVersionResponse UploadVersion(UploadVersionRequest request) { UploadVersionResponse response = new UploadVersionResponse(); @@ -315,7 +307,7 @@ namespace Tango.MachineService.Controllers db.UsersRoles.ToList(); db.RolesPermissions.ToList(); - var user_guid = TokensManager.GetTokenObject(request.AccessToken).UserGuid; + var user_guid = RequestToken.Object.UserGuid; var user = new UserBuilder(db).Set(user_guid).WithRolesAndPermissions().Build(); @@ -369,8 +361,8 @@ namespace Tango.MachineService.Controllers return response; } - [PPCLoginFilter] [HttpPost] + [JwtTokenFilter] public UploadCompletedResponse NotifyUploadCompleted(UploadCompletedRequest request) { PPCPendingUpload upload = _pendingUploads.FirstOrDefault(x => x.Token == request.Token); @@ -439,11 +431,11 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Domain user found but the database entry validation failed."); } - response.WebToken = TokensManager.GetOrCreate(new TokenObject() + response.AccessToken = WebToken.CreateNew(MachineServiceConfig.JWT_TOKEN_SECRET, new TokenObject() { Mode = LoginMode.User, UserGuid = user.Guid, - }); + }).AccessToken; } else if (request.Mode == LoginMode.Machine) { @@ -454,11 +446,11 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Invalid serial number."); } - response.WebToken = TokensManager.GetOrCreate(new TokenObject() + response.AccessToken = WebToken.CreateNew(MachineServiceConfig.JWT_TOKEN_SECRET, new TokenObject() { Mode = LoginMode.Machine, MachineGuid = machine.Guid, - }); + }).AccessToken; } } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Filters/JwtTokenFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService/Filters/JwtTokenFilter.cs new file mode 100644 index 000000000..4cf22cc43 --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService/Filters/JwtTokenFilter.cs @@ -0,0 +1,46 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Security.Authentication; +using System.Web; +using System.Web.Http; +using System.Web.Http.Controllers; +using System.Web.Http.Filters; +using Tango.Transport.Web; +using Tango.Web.Authentication; + +namespace Tango.MachineService.Filters +{ + public class JwtTokenFilter : ActionFilterAttribute + { + public override void OnActionExecuting(HttpActionContext actionContext) + { + var authorizationHeader = actionContext.Request.Headers.Authorization; + + if (authorizationHeader != null && authorizationHeader.Parameter != null) + { + try + { + WebToken.Validate(MachineServiceConfig.JWT_TOKEN_SECRET, authorizationHeader.Parameter); + } + catch (JWT.TokenExpiredException) + { + throw new TokenExpiredException("Token expired."); + } + catch (JWT.SignatureVerificationException) + { + throw new InvalidTokenException("Invalid token."); + } + } + else + { + throw new AuthenticationException("No token specified."); + } + + base.OnActionExecuting(actionContext); + } + } +} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs deleted file mode 100644 index dfa78f0a0..000000000 --- a/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Security.Authentication; -using System.Web; -using System.Web.Http; -using System.Web.Http.Controllers; -using System.Web.Http.Filters; -using Tango.Transport.Web; - -namespace Tango.MachineService.Filters -{ - public class MachineStudioLoginFilter : ActionFilterAttribute - { - public override void OnActionExecuting(HttpActionContext actionContext) - { - var json = actionContext.Request.Content.ReadAsStringAsync().Result; - WebRequestSecureMessage msg = JsonConvert.DeserializeObject(json); - - try - { - Controllers.MachineStudioController.TokensManager.Validate(msg.AccessToken); - } - catch (Exception ex) - { - throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex)); - } - - base.OnActionExecuting(actionContext); - } - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs deleted file mode 100644 index 322023b7d..000000000 --- a/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Security.Authentication; -using System.Web; -using System.Web.Http; -using System.Web.Http.Controllers; -using System.Web.Http.Filters; -using Tango.Transport.Web; - -namespace Tango.MachineService.Filters -{ - public class PPCLoginFilter : ActionFilterAttribute - { - public override void OnActionExecuting(HttpActionContext actionContext) - { - var json = actionContext.Request.Content.ReadAsStringAsync().Result; - WebRequestSecureMessage msg = JsonConvert.DeserializeObject(json); - - try - { - Controllers.PPCController.TokensManager.Validate(msg.AccessToken); - } - catch (Exception ex) - { - throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex)); - } - - base.OnActionExecuting(actionContext); - } - } -} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/MachineServiceConfig.cs b/Software/Visual_Studio/Web/Tango.MachineService/MachineServiceConfig.cs index 289fb08e2..a6aa71944 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/MachineServiceConfig.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/MachineServiceConfig.cs @@ -12,5 +12,6 @@ namespace Tango.MachineService public static String TANGO_VERSIONS_CONTAINER => ConfigurationManager.AppSettings[nameof(TANGO_VERSIONS_CONTAINER)].ToString(); public static String MACHINE_STUDIO_VERSIONS_CONTAINER => ConfigurationManager.AppSettings[nameof(MACHINE_STUDIO_VERSIONS_CONTAINER)].ToString(); public static bool ENFORCE_MACHINE_STUDIO_VERSION => bool.Parse(ConfigurationManager.AppSettings[nameof(ENFORCE_MACHINE_STUDIO_VERSION)].ToString()); + public static String JWT_TOKEN_SECRET => ConfigurationManager.AppSettings[nameof(JWT_TOKEN_SECRET)].ToString(); } } \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj index 038849c72..6dbbe49ab 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj @@ -56,6 +56,9 @@ ..\..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll + + ..\..\packages\JWT.5.0.0\lib\net46\JWT.dll + @@ -161,8 +164,8 @@ ..\..\packages\Microsoft.SqlServer.SqlManagementObjects.140.17283.0\lib\net40\Microsoft.SqlServer.WmiEnum.dll - - ..\..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll + + ..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll @@ -275,8 +278,7 @@ - - + @@ -379,7 +381,7 @@ False - + diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Web.config b/Software/Visual_Studio/Web/Tango.MachineService/Web.config index 72c56b38f..fb10e615e 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Web.config +++ b/Software/Visual_Studio/Web/Tango.MachineService/Web.config @@ -16,16 +16,17 @@ - + - - + + +