diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-18 18:16:24 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-02-18 18:16:24 +0200 |
| commit | 45829a9fb6f4b0d4443e22c972cf8543be533d4e (patch) | |
| tree | 8255065ff8e3e7071dc53486870b1a0047d57d5a /Software | |
| parent | 78181d457b113c25e9ea55b378d6b6bd8e201338 (diff) | |
| download | Tango-45829a9fb6f4b0d4443e22c972cf8543be533d4e.tar.gz Tango-45829a9fb6f4b0d4443e22c972cf8543be533d4e.zip | |
Working on Tokens Manager.
Diffstat (limited to 'Software')
14 files changed, 164 insertions, 46 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Publish/MachineStudioPublisher.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Publish/MachineStudioPublisher.cs index 19f186525..b9a50ec73 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Publish/MachineStudioPublisher.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Publish/MachineStudioPublisher.cs @@ -18,7 +18,7 @@ namespace Tango.MachineStudio.Common.Publish { public class MachineStudioPublisher : ExtendedObject { - private IMachineStudioService _client; + private IMachineStudioWebService _client; /// <summary> /// Occurs on publish progress. @@ -40,7 +40,7 @@ namespace Tango.MachineStudio.Common.Publish /// </summary> public MachineStudioPublisher() { - _client = new MachineStudioService(); + _client = new MachineStudioWebService(); Options = new PublishOptions(); } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index 40d7da460..d5e62f4e1 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -134,10 +134,10 @@ <Compile Include="Web\DownloadLatestVersionRequest.cs" /> <Compile Include="Web\CheckForUpdatesResponse.cs" /> <Compile Include="Web\CheckForUpdatesRequest.cs" /> - <Compile Include="Web\IMachineStudioService.cs" /> + <Compile Include="Web\IMachineStudioWebService.cs" /> <Compile Include="Web\LatestVersionRequest.cs" /> <Compile Include="Web\LatestVersionResponse.cs" /> - <Compile Include="Web\MachineStudioService.cs" /> + <Compile Include="Web\MachineStudioWebService.cs" /> <Compile Include="Web\UploadCompletedRequest.cs" /> <Compile Include="Web\UploadCompletedResponse.cs" /> <Compile Include="Web\UploadVersionRequest.cs" /> diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioService.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioWebService.cs index 748df4644..5a89f688f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioService.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioWebService.cs @@ -9,7 +9,7 @@ using Tango.Web; namespace Tango.MachineStudio.Common.Web { - public interface IMachineStudioService + public interface IMachineStudioWebService { DeploymentSlot Environment { get; set; } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioService.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebService.cs index 8b43146e1..d0aa8a5bf 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioService.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebService.cs @@ -10,13 +10,13 @@ using Tango.Web; namespace Tango.MachineStudio.Common.Web { - public class MachineStudioService : IMachineStudioService + public class MachineStudioWebService : IMachineStudioWebService { private WebTransportClient _client; public DeploymentSlot Environment { get; set; } - public MachineStudioService() + public MachineStudioWebService() { Environment = SettingsManager.Default.GetOrCreate<MachineStudioSettings>().DeploymentSlot; _client = new WebTransportClient(); diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs index 1f9aee937..d81f0d561 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -57,7 +57,7 @@ namespace Tango.MachineStudio.UI.Authentication { var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); - IMachineStudioService service = new MachineStudioService(); + IMachineStudioWebService service = new MachineStudioWebService(); var response = service.Login(new LoginRequest() { diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs index 64043e556..56ebb1e00 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs @@ -357,7 +357,7 @@ namespace Tango.MachineStudio.UI.ViewModels { if (_authenticationProvider.CurrentUser != null) { - var client = new MachineStudioService(); + var client = new MachineStudioWebService(); CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() { diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs index 8d4553e32..57043df0e 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs @@ -169,7 +169,7 @@ namespace Tango.MachineStudio.UI.ViewModels Status = UpdateStatus.CheckingForUpdate; - var client = new MachineStudioService(); + var client = new MachineStudioWebService(); DownloadLatestVersionResponse response = await client.DownloadLatestVersion(new DownloadLatestVersionRequest() { @@ -216,7 +216,7 @@ namespace Tango.MachineStudio.UI.ViewModels { Thread.Sleep(2000); - var client = new MachineStudioService(); + var client = new MachineStudioWebService(); CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() { diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs index b5140e336..256804f67 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -40,7 +40,6 @@ namespace Tango.PPC.Common.MachineSetup private IRemoteAssistanceProvider _remoteAssistance; private IUnifiedWriteFilterManager _uwf; private IOperationSystemManager _windows_manager; - private String _accessToken; #region Events @@ -84,19 +83,17 @@ namespace Tango.PPC.Common.MachineSetup #region Private Methods - private Task Login(String serialNumber) + private Task<String> Login(String serialNumber) { - return Task.Factory.StartNew(() => + return Task.Factory.StartNew<String>(() => { using (var client = new PPCWebService()) { - var response = client.Login(new LoginRequest() + return client.Login(new LoginRequest() { Mode = LoginMode.Machine, SerialNumber = serialNumber, - }).Result; - - _accessToken = response.AccessToken; + }).Result.AccessToken; } }); } @@ -128,10 +125,10 @@ namespace Tango.PPC.Common.MachineSetup LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - await Login(serialNumber); + var accessToken = await Login(serialNumber); MachineSetupRequest request = new MachineSetupRequest(); - request.AccessToken = _accessToken; + request.AccessToken = accessToken; MachineSetupResponse setup_response = null; diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs index c19208724..8661168b0 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -29,7 +29,6 @@ namespace Tango.PPC.Common.MachineUpdate { private IPPCApplicationManager _app_manager; private IMachineProvider _machineProvider; - private String _accessToken; #region Events @@ -72,19 +71,17 @@ namespace Tango.PPC.Common.MachineUpdate #region Private Methods - private Task Login(String serialNumber) + private Task<String> Login(String serialNumber) { - return Task.Factory.StartNew(() => + return Task.Factory.StartNew<String>(() => { using (var client = new PPCWebService()) { - var response = client.Login(new LoginRequest() + return client.Login(new LoginRequest() { Mode = LoginMode.Machine, SerialNumber = serialNumber, - }).Result; - - _accessToken = response.AccessToken; + }).Result.AccessToken; } }); } @@ -131,10 +128,10 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - await Login(serialNumber); + String accessToken = await Login(serialNumber); DownloadUpdateRequest request = new DownloadUpdateRequest(); - request.AccessToken = _accessToken; + request.AccessToken = accessToken; DownloadUpdateResponse update_response = null; @@ -301,12 +298,12 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - Login(serialNumber).Wait(); + String accessToken = Login(serialNumber).Result; LogManager.Log($"Checking if updates available..."); CheckForUpdateRequest request = new CheckForUpdateRequest(); - request.AccessToken = _accessToken; + request.AccessToken = accessToken; request.Version = _app_manager.Version.ToString(); CheckForUpdateResponse update_response = null; @@ -417,10 +414,10 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - Login(serialNumber).Wait(); + String accessToken = Login(serialNumber).Result; UpdateDBRequest request = new UpdateDBRequest(); - request.AccessToken = _accessToken; + request.AccessToken = accessToken; UpdateDBResponse update_response = null; diff --git a/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs b/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs new file mode 100644 index 000000000..ca1653e3e --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs @@ -0,0 +1,64 @@ +using System; +using System.Threading; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tango.Web.Authentication; + +namespace Tango.UnitTesting +{ + [TestClass] + [TestCategory("Machine Service")] + public class MachineService_TST + { + private class TokenObject : IEquatable<TokenObject> + { + public String Value { get; set; } + + public bool Equals(TokenObject other) + { + return Value == other.Value; + } + } + + [TestMethod] + public void Test_Tokens_Manager() + { + TokensManager<TokenObject> tokensManager = new TokensManager<TokenObject>(); + + TokenObject t1 = new TokenObject(); + t1.Value = "Roy"; + + TokenObject t2 = new TokenObject(); + t2.Value = "Sagi"; + + String token1 = tokensManager.GetOrCreate(t1); + String token2 = tokensManager.GetOrCreate(t2); + + Assert.AreEqual(tokensManager.GetTokenObject(token1), t1); + Assert.AreEqual(tokensManager.GetTokenObject(token2), t2); + + TokenObject t3 = new TokenObject(); + t3.Value = "Roy"; + + String token3 = tokensManager.GetOrCreate(t3); + Assert.AreEqual(token3, token1); + Assert.AreEqual(tokensManager.GetTokenObject(token1), tokensManager.GetTokenObject(token3)); + + TokenObject tOld = new TokenObject() + { + Value = "Expired Token" + }; + + tokensManager.ExpirationTime = TimeSpan.FromSeconds(2); + + Thread.Sleep(2000); + + String recent_token = tokensManager.GetOrCreate(tOld); + + Thread.Sleep(1000); + + Assert.IsTrue(tokensManager.Exists(recent_token)); + + Assert.IsFalse(tokensManager.Exists(token1)); + } + } +} diff --git a/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj b/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj index fa4652899..9ebc45dfc 100644 --- a/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj +++ b/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj @@ -107,6 +107,7 @@ <Compile Include="Helper.cs" /> <Compile Include="Protobuf_TST.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="MachineService_TST.cs" /> </ItemGroup> <ItemGroup> <None Include="App.config" /> @@ -193,6 +194,10 @@ <Project>{74e700b0-1156-4126-be40-ee450d3c3026}</Project> <Name>Tango.Transport</Name> </ProjectReference> + <ProjectReference Include="..\Tango.Web\Tango.Web.csproj"> + <Project>{5001990f-977b-48ff-b217-0236a5022ad8}</Project> + <Name>Tango.Web</Name> + </ProjectReference> <ProjectReference Include="..\Utilities\Tango.DBObservablesGenerator.CLI\Tango.DBObservablesGenerator.CLI.csproj"> <Project>{ebb7cb9f-6af2-456b-a5dd-1b136b605d90}</Project> <Name>Tango.DBObservablesGenerator.CLI</Name> @@ -212,7 +217,7 @@ <Import Project="..\packages\System.Data.SQLite.Core.1.0.108.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.108.0\build\net46\System.Data.SQLite.Core.targets')" /> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> + <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> </VisualStudio> </ProjectExtensions> </Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs index 701a87a3c..8e0eb4288 100644 --- a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs +++ b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs @@ -7,35 +7,85 @@ using System.Threading.Tasks; namespace Tango.Web.Authentication { - public class TokensManager<T> + public class TokensManager<T> where T : IEquatable<T> { - private Dictionary<String, T> _tokens; + private Dictionary<String, TokenWrapper> _tokens; + + private class TokenWrapper : IEquatable<TokenWrapper> + { + public DateTime Date { get; set; } + + public T Value { get; set; } + + public bool Equals(TokenWrapper other) + { + return Value.Equals(other.Value); + } + } public TokensManager() { - _tokens = new Dictionary<String, T>(); + _tokens = new Dictionary<string, TokenWrapper>(); + ExpirationTime = TimeSpan.FromHours(1); } - public String CreateNew(T userID) + public TimeSpan ExpirationTime { get; set; } + + public String GetOrCreate(T tokenObject) { - String token = Guid.NewGuid().ToString(); - _tokens.Add(token, userID); - return token; + var existing_token = _tokens.FirstOrDefault(x => x.Value.Value.Equals(tokenObject)); + + if (existing_token.Key != null) + { + return existing_token.Key; + } + else + { + String token = Guid.NewGuid().ToString(); + _tokens.Add(token, new TokenWrapper() + { + Value = tokenObject, + Date = DateTime.Now, + }); + return token; + } } public bool Exists(String token) { + RemoveOrRenewExpired(token); + return _tokens.ContainsKey(token); } public T GetTokenObject(String token) { + RemoveOrRenewExpired(token); + if (!_tokens.ContainsKey(token)) { throw new AuthenticationException("Invalid token."); } - return _tokens[token]; + return _tokens[token].Value; + } + + private void RemoveOrRenewExpired(String token) + { + DateTime now = DateTime.Now; + + if (_tokens.ContainsKey(token)) + { + _tokens[token].Date = now; + } + + foreach (var t in _tokens.ToList()) + { + if (now > t.Value.Date.Add(ExpirationTime)) + { + _tokens.Remove(t.Key); + } + } } } } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index 142980fdd..98ea8d827 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -347,7 +347,7 @@ namespace Tango.MachineService.Controllers Password = request.Password, }, - AccessToken = TokensManager.CreateNew(user.Guid), + AccessToken = TokensManager.GetOrCreate(user.Guid), 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 af861923c..064a44fdb 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -31,11 +31,16 @@ namespace Tango.MachineService.Controllers private static List<PPCPendingUpload> _pendingUploads; private ActiveDirectoryManager _ad_manager; - public class TokenObject + public class TokenObject : IEquatable<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<TokenObject> TokensManager { get; set; } @@ -434,7 +439,7 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Domain user found but the database entry validation failed."); } - response.AccessToken = TokensManager.CreateNew(new TokenObject() + response.AccessToken = TokensManager.GetOrCreate(new TokenObject() { Mode = LoginMode.User, UserGuid = user.Guid, @@ -449,7 +454,7 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Invalid serial number."); } - response.AccessToken = TokensManager.CreateNew(new TokenObject() + response.AccessToken = TokensManager.GetOrCreate(new TokenObject() { Mode = LoginMode.Machine, UserGuid = machine.Guid, |
