diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-02-19 01:50:58 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2019-02-19 01:50:58 +0200 |
| commit | 9736b8c8ede6a0d121dea8381f0abb561fad5631 (patch) | |
| tree | 1c38f3f5be82b923ffae40fae00a25487179ab54 | |
| parent | be3343cc0268179c7a6f628fa5e68f323a3335e5 (diff) | |
| download | Tango-9736b8c8ede6a0d121dea8381f0abb561fad5631.tar.gz Tango-9736b8c8ede6a0d121dea8381f0abb561fad5631.zip | |
Implemented auto generated web clients for PPC and machine studio.
Improved interactions with web clients across solutions.
54 files changed, 994 insertions, 417 deletions
diff --git a/Software/Visual_Studio/Build/Shortcuts/Machine Emulator.lnk b/Software/Visual_Studio/Build/Shortcuts/Machine Emulator.lnk Binary files differindex 43aace6b5..d95642721 100644 --- a/Software/Visual_Studio/Build/Shortcuts/Machine Emulator.lnk +++ b/Software/Visual_Studio/Build/Shortcuts/Machine Emulator.lnk diff --git a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk Binary files differindex 4d317d343..1550b6e18 100644 --- a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk +++ b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk diff --git a/Software/Visual_Studio/Build/Shortcuts/Proto Compiler GUI.lnk b/Software/Visual_Studio/Build/Shortcuts/Proto Compiler GUI.lnk Binary files differindex 1be399f50..72ad667d8 100644 --- a/Software/Visual_Studio/Build/Shortcuts/Proto Compiler GUI.lnk +++ b/Software/Visual_Studio/Build/Shortcuts/Proto Compiler GUI.lnk diff --git a/Software/Visual_Studio/Build/Shortcuts/Stubs Execution GUI.lnk b/Software/Visual_Studio/Build/Shortcuts/Stubs Execution GUI.lnk Binary files differindex db7f7c338..5258be2c6 100644 --- a/Software/Visual_Studio/Build/Shortcuts/Stubs Execution GUI.lnk +++ b/Software/Visual_Studio/Build/Shortcuts/Stubs Execution GUI.lnk diff --git a/Software/Visual_Studio/Build/Shortcuts/Transport Router.lnk b/Software/Visual_Studio/Build/Shortcuts/Transport Router.lnk Binary files differindex 6f0433af2..73b23b4ce 100644 --- a/Software/Visual_Studio/Build/Shortcuts/Transport Router.lnk +++ b/Software/Visual_Studio/Build/Shortcuts/Transport Router.lnk diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/IAuthenticationProvider.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/IAuthenticationProvider.cs index 7cc2b5b51..4958cb0f4 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/IAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/IAuthenticationProvider.cs @@ -13,11 +13,6 @@ namespace Tango.MachineStudio.Common.Authentication public interface IAuthenticationProvider { /// <summary> - /// Gets the access token that was retrieved at the last login. - /// </summary> - String AccessToken { get; } - - /// <summary> /// Occurs when the current logged-in user has changed. /// </summary> event EventHandler<User> CurrentUserChanged; 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 b9a50ec73..dfbe0adc6 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 IMachineStudioWebService _client; + private MachineStudioWebClient _client; /// <summary> /// Occurs on publish progress. @@ -40,7 +40,7 @@ namespace Tango.MachineStudio.Common.Publish /// </summary> public MachineStudioPublisher() { - _client = new MachineStudioWebService(); + _client = new MachineStudioWebClient(); Options = new PublishOptions(); } @@ -98,16 +98,16 @@ namespace Tango.MachineStudio.Common.Publish /// Login to machine service and returns an access token. /// </summary> /// <returns></returns> - private Task<String> Login() + private Task Login() { - return Task.Factory.StartNew<String>(() => + return Task.Factory.StartNew(() => { return _client.Login(new LoginRequest() { Email = Options.Email, Password = Options.Password, Version = GetLocalVersion(), - }).Result.AccessToken; + }).Result; }); } @@ -121,7 +121,6 @@ namespace Tango.MachineStudio.Common.Publish String appPath = GetMachineStudioExecutablePath(); String folder = Options.GetApplicationPath(); - String accessToken = String.Empty; if (!File.Exists(appPath)) { @@ -135,7 +134,7 @@ namespace Tango.MachineStudio.Common.Publish try { OnPublishProgress(0, 100, $"Logging in to machine service at {Options.Environment.ToAddress()}..."); - accessToken = Login().Result; + Login().Wait(); OnPublishProgress(0, 100, $"Fetching remote version from {Options.Environment.ToAddress()}..."); @@ -154,7 +153,6 @@ namespace Tango.MachineStudio.Common.Publish var response = _client.UploadVersion(new UploadVersionRequest() { - AccessToken = accessToken, Version = local_version, Comments = Options.Comments, }).Result; @@ -200,7 +198,6 @@ namespace Tango.MachineStudio.Common.Publish _client.NotifyUploadCompleted(new UploadCompletedRequest() { - AccessToken = accessToken, Token = response.Token, }).Wait(); 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 d5e62f4e1..48c78e865 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\IMachineStudioWebService.cs" /> <Compile Include="Web\LatestVersionRequest.cs" /> <Compile Include="Web\LatestVersionResponse.cs" /> - <Compile Include="Web\MachineStudioWebService.cs" /> + <Compile Include="Web\MachineStudioWebClient.cs" /> + <Compile Include="Web\MachineStudioWebClientBase.cs" /> <Compile Include="Web\UploadCompletedRequest.cs" /> <Compile Include="Web\UploadCompletedResponse.cs" /> <Compile Include="Web\UploadVersionRequest.cs" /> @@ -326,7 +326,7 @@ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.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/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioWebService.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioWebService.cs deleted file mode 100644 index 5a89f688f..000000000 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/IMachineStudioWebService.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.ServiceModel; -using System.Text; -using System.Threading.Tasks; -using Tango.Web; - -namespace Tango.MachineStudio.Common.Web -{ - public interface IMachineStudioWebService - { - DeploymentSlot Environment { get; set; } - - Task<CheckForUpdatesResponse> CheckForUpdates(CheckForUpdatesRequest request); - - Task<DownloadLatestVersionResponse> DownloadLatestVersion(DownloadLatestVersionRequest request); - - Task<UploadVersionResponse> UploadVersion(UploadVersionRequest request); - - Task<UploadCompletedResponse> NotifyUploadCompleted(UploadCompletedRequest request); - - Task<LatestVersionResponse> GetLatestVersion(LatestVersionRequest request); - - Task<LoginResponse> Login(LoginRequest request); - } -} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/LoginResponse.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/LoginResponse.cs index 643e3930d..78c7bc560 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/LoginResponse.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/LoginResponse.cs @@ -5,13 +5,13 @@ using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.Transport.Web; +using Tango.Web.Authentication; namespace Tango.MachineStudio.Common.Web { - public class LoginResponse : WebResponseMessage + public class LoginResponse : WebTokenResponse { public DataSource DataSource { get; set; } - public String AccessToken { get; set; } public bool VersionChangeRequired { get; set; } public String RequiredVersion { get; set; } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClient.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClient.cs new file mode 100644 index 000000000..02276d641 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClient.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Settings; +using Tango.Web; +using Tango.Web.Authentication; + +namespace Tango.MachineStudio.Common.Web +{ + public class MachineStudioWebClient : MachineStudioWebClientBase + { + public MachineStudioWebClient(DeploymentSlot environment) : base(environment) + { + } + + public MachineStudioWebClient(DeploymentSlot environment, WebToken token) : base(environment, token) + { + + } + + public MachineStudioWebClient(WebToken token) : this(SettingsManager.Default.GetOrCreate<MachineStudioSettings>().DeploymentSlot, token) + { + + } + + public MachineStudioWebClient() : this(null) + { + + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClientBase.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClientBase.cs new file mode 100644 index 000000000..dc5a14856 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebClientBase.cs @@ -0,0 +1,79 @@ + +using System.Threading.Tasks; +using Tango.Web; +using Tango.Web.Authentication; + +namespace Tango.MachineStudio.Common.Web +{ + /// <summary> + /// Represents a machine service MachineStudio web client. + /// </summary> + /// <seealso cref="Tango.Web.TangoWebClient" /> + public abstract class MachineStudioWebClientBase : TangoWebClient<Tango.MachineStudio.Common.Web.LoginRequest, Tango.MachineStudio.Common.Web.LoginResponse> + { + /// <summary> + /// Initializes a new instance of the <see cref="MachineStudioWebClientBase"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + /// <param name="token">Existing token.</param> + public MachineStudioWebClientBase(DeploymentSlot environment, WebToken token) : base(environment, "MachineStudio", token) + { + + } + + /// <summary> + /// Initializes a new instance of the <see cref="MachineStudioWebClientBase"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + public MachineStudioWebClientBase(DeploymentSlot environment) : this(environment, null) + { + + } + + /// <summary> + /// Executes the CheckForUpdates action and returns Tango.MachineStudio.Common.Web.CheckForUpdatesResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.MachineStudio.Common.Web.CheckForUpdatesResponse> CheckForUpdates(Tango.MachineStudio.Common.Web.CheckForUpdatesRequest request) + { + return Post<Tango.MachineStudio.Common.Web.CheckForUpdatesRequest, Tango.MachineStudio.Common.Web.CheckForUpdatesResponse>("CheckForUpdates", request); + } + + /// <summary> + /// Executes the DownloadLatestVersion action and returns Tango.MachineStudio.Common.Web.DownloadLatestVersionResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.MachineStudio.Common.Web.DownloadLatestVersionResponse> DownloadLatestVersion(Tango.MachineStudio.Common.Web.DownloadLatestVersionRequest request) + { + return Post<Tango.MachineStudio.Common.Web.DownloadLatestVersionRequest, Tango.MachineStudio.Common.Web.DownloadLatestVersionResponse>("DownloadLatestVersion", request); + } + + /// <summary> + /// Executes the UploadVersion action and returns Tango.MachineStudio.Common.Web.UploadVersionResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.MachineStudio.Common.Web.UploadVersionResponse> UploadVersion(Tango.MachineStudio.Common.Web.UploadVersionRequest request) + { + return Post<Tango.MachineStudio.Common.Web.UploadVersionRequest, Tango.MachineStudio.Common.Web.UploadVersionResponse>("UploadVersion", request); + } + + /// <summary> + /// Executes the NotifyUploadCompleted action and returns Tango.MachineStudio.Common.Web.UploadCompletedResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.MachineStudio.Common.Web.UploadCompletedResponse> NotifyUploadCompleted(Tango.MachineStudio.Common.Web.UploadCompletedRequest request) + { + return Post<Tango.MachineStudio.Common.Web.UploadCompletedRequest, Tango.MachineStudio.Common.Web.UploadCompletedResponse>("NotifyUploadCompleted", request); + } + + /// <summary> + /// Executes the GetLatestVersion action and returns Tango.MachineStudio.Common.Web.LatestVersionResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.MachineStudio.Common.Web.LatestVersionResponse> GetLatestVersion(Tango.MachineStudio.Common.Web.LatestVersionRequest request) + { + return Post<Tango.MachineStudio.Common.Web.LatestVersionRequest, Tango.MachineStudio.Common.Web.LatestVersionResponse>("GetLatestVersion", request); + } + + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebService.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebService.cs deleted file mode 100644 index d0aa8a5bf..000000000 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Web/MachineStudioWebService.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.MachineStudio.Common.Authentication; -using Tango.Settings; -using Tango.Transport.Web; -using Tango.Web; - -namespace Tango.MachineStudio.Common.Web -{ - public class MachineStudioWebService : IMachineStudioWebService - { - private WebTransportClient _client; - - public DeploymentSlot Environment { get; set; } - - public MachineStudioWebService() - { - Environment = SettingsManager.Default.GetOrCreate<MachineStudioSettings>().DeploymentSlot; - _client = new WebTransportClient(); - } - - public Task<CheckForUpdatesResponse> CheckForUpdates(CheckForUpdatesRequest request) - { - return _client.PostJson<CheckForUpdatesRequest, CheckForUpdatesResponse>(GetAddress() + "CheckForUpdates", request); - } - - public Task<UploadVersionResponse> UploadVersion(UploadVersionRequest request) - { - return _client.PostJson<UploadVersionRequest, UploadVersionResponse>(GetAddress() + "UploadVersion", request); - } - - public Task<UploadCompletedResponse> NotifyUploadCompleted(UploadCompletedRequest request) - { - return _client.PostJson<UploadCompletedRequest, UploadCompletedResponse>(GetAddress() + "NotifyUploadCompleted", request); - } - - public Task<LatestVersionResponse> GetLatestVersion(LatestVersionRequest request) - { - return _client.PostJson<LatestVersionRequest, LatestVersionResponse>(GetAddress() + "GetLatestVersion", request); - } - - public Task<DownloadLatestVersionResponse> DownloadLatestVersion(DownloadLatestVersionRequest request) - { - return _client.PostJson<DownloadLatestVersionRequest, DownloadLatestVersionResponse>(GetAddress() + "DownloadLatestVersion", request); - } - - public Task<LoginResponse> Login(LoginRequest request) - { - return _client.PostJson<LoginRequest, LoginResponse>(GetAddress() + "Login", request); - } - - private String GetAddress() - { - return Environment.ToAddress() + "/api/MachineStudio/"; - } - } -} 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 d81f0d561..e07952f29 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -26,6 +26,8 @@ namespace Tango.MachineStudio.UI.Authentication /// <seealso cref="Tango.MachineStudio.Common.Authentication.IAuthenticationProvider" /> public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider { + private MachineStudioWebClient _client; + private User _currentUser; /// <summary> /// Gets the current logged-in user. @@ -47,6 +49,15 @@ namespace Tango.MachineStudio.UI.Authentication public event EventHandler<User> CurrentUserChanged; /// <summary> + /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class. + /// </summary> + /// <param name="machineStudioWebClient">The machine studio web client.</param> + public DefaultAuthenticationProvider(MachineStudioWebClient machineStudioWebClient) + { + _client = machineStudioWebClient; + } + + /// <summary> /// Performs a user login by the specified email and password. /// </summary> /// <param name="email">The email.</param> @@ -57,9 +68,8 @@ namespace Tango.MachineStudio.UI.Authentication { var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); - IMachineStudioWebService service = new MachineStudioWebService(); - - var response = service.Login(new LoginRequest() + _client.Environment = settings.DeploymentSlot; + var response = _client.Login(new LoginRequest() { Email = email, @@ -68,8 +78,6 @@ namespace Tango.MachineStudio.UI.Authentication }).Result; - AccessToken = response.AccessToken; - if (settings.Environment == MachineStudioSettings.WorkingEnvironment.Remote) { ObservablesContext.OverrideSettingsDataSource(response.DataSource); @@ -114,10 +122,5 @@ namespace Tango.MachineStudio.UI.Authentication { CurrentUser = null; } - - /// <summary> - /// Gets the access token that was retrieved at the last login. - /// </summary> - public string AccessToken { get; private set; } } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs index b0a3a8c11..7fbb0008c 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs @@ -14,6 +14,7 @@ using Tango.MachineStudio.Common.Speech; using Tango.MachineStudio.Common.StudioApplication; using Tango.MachineStudio.Common.Threading; using Tango.MachineStudio.Common.Video; +using Tango.MachineStudio.Common.Web; using Tango.MachineStudio.UI.Authentication; using Tango.MachineStudio.UI.Console; using Tango.MachineStudio.UI.FirmwareUpgrade; @@ -68,8 +69,10 @@ namespace Tango.MachineStudio.UI TangoIOC.Default.Unregister<ITeamFoundationServiceClient>(); TangoIOC.Default.Unregister<IDispatcherProvider>(); TangoIOC.Default.Unregister<IFirmwareUpgrader>(); + TangoIOC.Default.Unregister<MachineStudioWebClient>(); + TangoIOC.Default.Register<MachineStudioWebClient, MachineStudioWebClient>(new MachineStudioWebClient()); TangoIOC.Default.Register<IDispatcherProvider, DefaultDispatcherProvider>(new DefaultDispatcherProvider(Application.Current.Dispatcher)); TangoIOC.Default.Register<INotificationProvider, DefaultNotificationProvider>(); TangoIOC.Default.Register<IAuthenticationProvider, DefaultAuthenticationProvider>(); diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs index 8b3747584..1165c0920 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs @@ -16,6 +16,7 @@ using Tango.MachineStudio.Common.Authentication; using Tango.MachineStudio.Common.EventLogging; using Tango.MachineStudio.Common.Navigation; using Tango.MachineStudio.Common.Notifications; +using Tango.MachineStudio.Common.Web; using Tango.MachineStudio.UI.Messages; using Tango.Settings; using Tango.SharedUI; @@ -35,6 +36,7 @@ namespace Tango.MachineStudio.UI.ViewModels private IEventLogger _eventLogger; private Rfc2898Cryptographer cryptographer; private MachineStudioSettings _settings; + private MachineStudioWebClient _machineStudioWebClient; private String _email; /// <summary> @@ -111,10 +113,11 @@ namespace Tango.MachineStudio.UI.ViewModels /// <param name="authenticationProvider">The authentication provider.</param> /// <param name="navigationManager">The navigation manager.</param> /// <param name="notificationProvider">The notification provider.</param> - public LoginViewVM(IAuthenticationProvider authenticationProvider, INavigationManager navigationManager, INotificationProvider notificationProvider, IEventLogger eventLogger) + public LoginViewVM(MachineStudioWebClient machineStudioWebClient, IAuthenticationProvider authenticationProvider, INavigationManager navigationManager, INotificationProvider notificationProvider, IEventLogger eventLogger) { EnableSlotSelection = true; + _machineStudioWebClient = machineStudioWebClient; _settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); _notificationProvider = notificationProvider; 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 56ebb1e00..986bf483f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs @@ -55,6 +55,7 @@ namespace Tango.MachineStudio.UI.ViewModels private Thread _updateCheckThread; private IEventLogger _eventLogger; private MachineStudioSettings _settings; + private MachineStudioWebClient _machineStudioWebClient; /// <summary> /// Gets or sets the current loaded module. @@ -281,8 +282,10 @@ namespace Tango.MachineStudio.UI.ViewModels IEventLogger eventLogger, IDiagnosticsFrameProvider frameProvider, ISpeechProvider speechProvider, - TeamFoundationServiceExtendedClient tfs) : base() + TeamFoundationServiceExtendedClient tfs, + MachineStudioWebClient machineStudioWebClient) : base() { + _machineStudioWebClient = machineStudioWebClient; TFSClient = tfs; _eventLogger = eventLogger; _navigation = navigationManager; @@ -357,11 +360,8 @@ namespace Tango.MachineStudio.UI.ViewModels { if (_authenticationProvider.CurrentUser != null) { - var client = new MachineStudioWebService(); - - CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() + CheckForUpdatesResponse response = _machineStudioWebClient.CheckForUpdates(new CheckForUpdatesRequest() { - AccessToken = _authenticationProvider.AccessToken, Version = _applicationManager.Version.ToString(), }).Result; 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 57043df0e..17edf1dfd 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/UpdateViewVM.cs @@ -25,7 +25,6 @@ using Tango.MachineStudio.UI.Messages; using Tango.Settings; using Tango.MachineStudio.Common; using Tango.Transport.Web; -using Tango.MachineStudio.Common.Web; namespace Tango.MachineStudio.UI.ViewModels { @@ -56,6 +55,7 @@ namespace Tango.MachineStudio.UI.ViewModels private CheckForUpdatesResponse _updateInfo; private TemporaryFolder _newPackageTempFolder; private TemporaryFolder _previousPackageTempFolder; + private MachineStudioWebClient _machineStudioWebClient; private bool _forcedUpdate; public bool ForcedUpdate @@ -140,8 +140,9 @@ namespace Tango.MachineStudio.UI.ViewModels public RelayCommand TryRollbackAgainCommand { get; set; } - public UpdateViewVM(INotificationProvider notification, IAuthenticationProvider authentication, INavigationManager navigation, IStudioApplicationManager application) + public UpdateViewVM(MachineStudioWebClient machineStudioWebClient, INotificationProvider notification, IAuthenticationProvider authentication, INavigationManager navigation, IStudioApplicationManager application) { + _machineStudioWebClient = machineStudioWebClient; _notification = notification; _navigation = navigation; _application = application; @@ -169,11 +170,9 @@ namespace Tango.MachineStudio.UI.ViewModels Status = UpdateStatus.CheckingForUpdate; - var client = new MachineStudioWebService(); - - DownloadLatestVersionResponse response = await client.DownloadLatestVersion(new DownloadLatestVersionRequest() + DownloadLatestVersionResponse response = await _machineStudioWebClient.DownloadLatestVersion(new DownloadLatestVersionRequest() { - AccessToken = _authentication.AccessToken, + }); _updateInfo = new CheckForUpdatesResponse(); @@ -210,17 +209,14 @@ namespace Tango.MachineStudio.UI.ViewModels var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); - Task.Factory.StartNew(() => + Task.Factory.StartNew((Action)(() => { try { Thread.Sleep(2000); - var client = new MachineStudioWebService(); - - CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest() + CheckForUpdatesResponse response = _machineStudioWebClient.CheckForUpdates(new CheckForUpdatesRequest() { - AccessToken = _authentication.AccessToken, Version = _application.Version.ToString(), }).Result; @@ -241,7 +237,7 @@ namespace Tango.MachineStudio.UI.ViewModels logManager.Log(ex, "Error while checking for version update!"); Status = UpdateStatus.Error; } - }); + })); } private void BackToApplication() diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs index 896c7b921..4808252d0 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/IMachineSetupManager.cs @@ -27,8 +27,7 @@ namespace Tango.PPC.Common.MachineSetup /// Performs a machine setup using the specified serial number and machine service address. /// </summary> /// <param name="serialNumber">The serial number.</param> - /// <param name="machineServiceAddress">The machine service address.</param> /// <returns></returns> - Task<MachineSetupResult> Setup(String serialNumber, String machineServiceAddress); + Task<MachineSetupResult> Setup(String serialNumber); } } 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 256804f67..369248c7b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineSetup/MachineSetupManager.cs @@ -40,6 +40,7 @@ namespace Tango.PPC.Common.MachineSetup private IRemoteAssistanceProvider _remoteAssistance; private IUnifiedWriteFilterManager _uwf; private IOperationSystemManager _windows_manager; + private PPCWebClient _client; #region Events @@ -72,8 +73,9 @@ namespace Tango.PPC.Common.MachineSetup /// Initializes a new instance of the <see cref="MachineSetupManager"/> class. /// </summary> /// <param name="remoteAssistance">The remote assistance.</param> - public MachineSetupManager(IRemoteAssistanceProvider remoteAssistance, IUnifiedWriteFilterManager unifiedWriterFilterManager, IOperationSystemManager operationSystemManager) + public MachineSetupManager(PPCWebClient ppcWebClient, IRemoteAssistanceProvider remoteAssistance, IUnifiedWriteFilterManager unifiedWriterFilterManager, IOperationSystemManager operationSystemManager) { + _client = ppcWebClient; _remoteAssistance = remoteAssistance; _uwf = unifiedWriterFilterManager; _windows_manager = operationSystemManager; @@ -83,18 +85,16 @@ namespace Tango.PPC.Common.MachineSetup #region Private Methods - private Task<String> Login(String serialNumber) + private Task Login(String serialNumber) { - return Task.Factory.StartNew<String>(() => + return Task.Factory.StartNew(() => { - using (var client = new PPCWebService()) + return _client.Login(new LoginRequest() { - return client.Login(new LoginRequest() - { - Mode = LoginMode.Machine, - SerialNumber = serialNumber, - }).Result.AccessToken; - } + Mode = LoginMode.Machine, + SerialNumber = serialNumber, + }).Result; + }); } @@ -108,7 +108,7 @@ namespace Tango.PPC.Common.MachineSetup /// <param name="serialNumber">The serial number.</param> /// <param name="machineServiceAddress">The machine service address.</param> /// <returns></returns> - public async Task<MachineSetupResult> Setup(string serialNumber, string machineServiceAddress) + public async Task<MachineSetupResult> Setup(string serialNumber) { TaskCompletionSource<MachineSetupResult> result = new TaskCompletionSource<MachineSetupResult>(); @@ -116,6 +116,8 @@ namespace Tango.PPC.Common.MachineSetup { LogManager.Log($"Starting machine setup for serial number {serialNumber}..."); + var machineServiceAddress = SettingsManager.Default.GetOrCreate<PPCSettings>().GetMachineServiceAddress(); + IMachineOperator op = null; var settings = SettingsManager.Default.GetOrCreate<PPCSettings>(); @@ -125,19 +127,15 @@ namespace Tango.PPC.Common.MachineSetup LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - var accessToken = await Login(serialNumber); + Login(serialNumber).Wait(); MachineSetupRequest request = new MachineSetupRequest(); - request.AccessToken = accessToken; MachineSetupResponse setup_response = null; try { - using (var client = new PPCWebService()) - { - setup_response = await client.MachineSetup(request); - } + setup_response = await _client.MachineSetup(request); } catch (Exception ex) { 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 8661168b0..8b9aede87 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/MachineUpdate/MachineUpdateManager.cs @@ -29,6 +29,7 @@ namespace Tango.PPC.Common.MachineUpdate { private IPPCApplicationManager _app_manager; private IMachineProvider _machineProvider; + private PPCWebClient _client; #region Events @@ -61,8 +62,9 @@ namespace Tango.PPC.Common.MachineUpdate /// Initializes a new instance of the <see cref="MachineUpdateManager"/> class. /// </summary> /// <param name="applicationManager">The application manager.</param> - public MachineUpdateManager(IPPCApplicationManager applicationManager, IMachineProvider machineProvider) + public MachineUpdateManager(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineProvider machineProvider) { + _client = ppcWebClient; _machineProvider = machineProvider; _app_manager = applicationManager; } @@ -71,18 +73,15 @@ namespace Tango.PPC.Common.MachineUpdate #region Private Methods - private Task<String> Login(String serialNumber) + private Task Login(String serialNumber) { - return Task.Factory.StartNew<String>(() => + return Task.Factory.StartNew(() => { - using (var client = new PPCWebService()) + return _client.Login(new LoginRequest() { - return client.Login(new LoginRequest() - { - Mode = LoginMode.Machine, - SerialNumber = serialNumber, - }).Result.AccessToken; - } + Mode = LoginMode.Machine, + SerialNumber = serialNumber, + }).Result; }); } @@ -128,17 +127,13 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - String accessToken = await Login(serialNumber); + await Login(serialNumber); DownloadUpdateRequest request = new DownloadUpdateRequest(); - request.AccessToken = accessToken; DownloadUpdateResponse update_response = null; - using (var client = new PPCWebService()) - { - update_response = await client.MachineUpdate(request); - } + update_response = await _client.MachineUpdate(request); LogManager.Log($"Machine update response received: {Environment.NewLine}{update_response.ToJsonString()}"); @@ -298,20 +293,16 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - String accessToken = Login(serialNumber).Result; + Login(serialNumber).Wait(); LogManager.Log($"Checking if updates available..."); CheckForUpdateRequest request = new CheckForUpdateRequest(); - request.AccessToken = accessToken; request.Version = _app_manager.Version.ToString(); CheckForUpdateResponse update_response = null; - using (var client = new PPCWebService()) - { - update_response = client.CheckForUpdate(request).Result; - } + update_response = _client.CheckForUpdate(request).Result; LogManager.Log($"Check for update response received: {Environment.NewLine}{update_response.ToJsonString()}"); @@ -414,17 +405,13 @@ namespace Tango.PPC.Common.MachineUpdate LogManager.Log($"Connecting to machine service on {machineServiceAddress}..."); - String accessToken = Login(serialNumber).Result; + Login(serialNumber).Wait(); UpdateDBRequest request = new UpdateDBRequest(); - request.AccessToken = accessToken; UpdateDBResponse update_response = null; - using (var client = new PPCWebService()) - { - update_response = client.UpdateDB(request).Result; - } + update_response = _client.UpdateDB(request).Result; LogManager.Log($"Update DB response received: {Environment.NewLine}{update_response.ToJsonString()}"); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Publish/PPCPublisher.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Publish/PPCPublisher.cs index 1c69f0934..dfb544177 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Publish/PPCPublisher.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Publish/PPCPublisher.cs @@ -17,7 +17,7 @@ namespace Tango.PPC.Common.Publish { public class PPCPublisher : ExtendedObject { - private IPPCWebService _client; + private PPCWebClient _client; /// <summary> /// Occurs on publish progress. @@ -39,7 +39,7 @@ namespace Tango.PPC.Common.Publish /// </summary> public PPCPublisher() { - _client = new PPCWebService(); + _client = new PPCWebClient(); Options = new PublishOptions(); } @@ -100,16 +100,16 @@ namespace Tango.PPC.Common.Publish /// Login to machine service and returns an access token. /// </summary> /// <returns></returns> - private Task<String> Login() + private Task Login() { - return Task.Factory.StartNew<String>(() => + return Task.Factory.StartNew(() => { return _client.Login(new LoginRequest() { Mode = LoginMode.User, Email = Options.Email, Password = Options.Password, - }).Result.AccessToken; + }).Result; }); } @@ -123,7 +123,6 @@ namespace Tango.PPC.Common.Publish String appPath = GetPPCExecutablePath(); String folder = Options.GetApplicationPath(); - String accessToken = String.Empty; if (!File.Exists(appPath)) { @@ -137,7 +136,7 @@ namespace Tango.PPC.Common.Publish try { OnPublishProgress(0, 100, $"Logging in to machine service at {Options.Environment.ToAddress()}..."); - accessToken = Login().Result; + Login().Wait(); OnPublishProgress(0, 100, $"Fetching remote version from {Options.Environment.ToAddress()}..."); @@ -159,7 +158,6 @@ namespace Tango.PPC.Common.Publish Comments = Options.Comments, Version = local_version, MachineVersionGuid = Options.MachineVersionGuid, - AccessToken = accessToken, }).Result; CreateTupPackage(tempFile).Wait(); @@ -183,7 +181,6 @@ namespace Tango.PPC.Common.Publish _client.NotifyUploadCompleted(new UploadCompletedRequest() { - AccessToken = accessToken, Token = response.Token, }); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index 3b18a6217..e47437f4d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -161,6 +161,8 @@ <Compile Include="MachineUpdate\MachineUpdateProgress.cs" /> <Compile Include="MachineUpdate\MachineUpdateManager.cs" /> <Compile Include="MachineUpdate\MachineUpdateResult.cs" /> + <Compile Include="Web\PPCWebClient.cs" /> + <Compile Include="Web\PPCWebClientBase.cs" /> <Compile Include="Web\UpdateDBRequest.cs" /> <Compile Include="Web\UpdateDBResponse.cs" /> <Compile Include="MachineUpdate\UpdatePackageFile.cs" /> @@ -194,14 +196,12 @@ <Compile Include="Threading\IDispatcherProvider.cs" /> <Compile Include="Web\MachineVersionsRequest.cs" /> <Compile Include="Web\MachineVersionsResponse.cs" /> - <Compile Include="Web\IPPCWebService.cs" /> <Compile Include="Web\LatestVersionRequest.cs" /> <Compile Include="Web\LatestVersionResponse.cs" /> <Compile Include="Web\UploadCompletedResponse.cs" /> <Compile Include="Web\UploadCompletedRequest.cs" /> <Compile Include="Web\UploadVersionRequest.cs" /> <Compile Include="Web\UploadVersionResponse.cs" /> - <Compile Include="Web\PPCWebService.cs" /> <Compile Include="UWF\DefaultUnifiedWriteFilterManager.cs" /> <Compile Include="UWF\IUnifiedWriteFilterManager.cs" /> <Compile Include="WatchDog\WatchDogClient.cs" /> @@ -366,7 +366,7 @@ </Target> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> + <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> </VisualStudio> </ProjectExtensions> </Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/IPPCWebService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/IPPCWebService.cs deleted file mode 100644 index 6347836a9..000000000 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/IPPCWebService.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.ServiceModel; -using System.Text; -using System.Threading.Tasks; -using Tango.PPC.Common.Web; -using Tango.Web; - -namespace Tango.PPC.Common.Web -{ - /// <summary> - /// Represents an PPC update service for uploading PPC software packages. - /// </summary> - public interface IPPCWebService : IDisposable - { - /// <summary> - /// Gets or sets the environment. - /// </summary> - DeploymentSlot Environment { get; set; } - - /// <summary> - /// Uploads the version. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<UploadVersionResponse> UploadVersion(UploadVersionRequest request); - - /// <summary> - /// Notifies the upload completed. - /// </summary> - /// <param name="request">The request.</param> - Task<UploadCompletedResponse> NotifyUploadCompleted(UploadCompletedRequest request); - - /// <summary> - /// Gets the latest version. - /// </summary> - /// <param name="machineVersionGuid">The machine version unique identifier.</param> - /// <returns></returns> - Task<LatestVersionResponse> GetLatestVersion(LatestVersionRequest request); - - /// <summary> - /// Gets the machine versions. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<MachineVersionsResponse> GetMachineVersions(); - - /// <summary> - /// Checks for available update. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<CheckForUpdateResponse> CheckForUpdate(CheckForUpdateRequest request); - - /// <summary> - /// Gets the version download info for the specified version. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<DownloadUpdateResponse> MachineUpdate(DownloadUpdateRequest request); - - /// <summary> - /// Gets the machine setup information. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<MachineSetupResponse> MachineSetup(MachineSetupRequest request); - - /// <summary> - /// Gets the machine database update information. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<UpdateDBResponse> UpdateDB(UpdateDBRequest request); - - /// <summary> - /// Logins to the PPC service. - /// </summary> - /// <param name="request">The request.</param> - /// <returns></returns> - Task<LoginResponse> Login(LoginRequest request); - } -} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/LoginResponse.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/LoginResponse.cs index bf2a1e88e..5d97c6470 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/LoginResponse.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/LoginResponse.cs @@ -4,11 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Transport.Web; +using Tango.Web.Authentication; namespace Tango.PPC.Common.Web { - public class LoginResponse : WebResponseMessage + public class LoginResponse : WebTokenResponse { - public String AccessToken { get; set; } + } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClient.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClient.cs new file mode 100644 index 000000000..5900dd697 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClient.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Settings; +using Tango.Web; +using Tango.Web.Authentication; + +namespace Tango.PPC.Common.Web +{ + public class PPCWebClient : PPCWebClientBase + { + public PPCWebClient(DeploymentSlot environment) : base(environment) + { + } + + public PPCWebClient(DeploymentSlot environment, WebToken token) : base(environment, token) + { + } + + public PPCWebClient(WebToken token) : this(SettingsManager.Default.GetOrCreate<PPCSettings>().DeploymentSlot, token) + { + } + + public PPCWebClient() : this(null) + { + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs new file mode 100644 index 000000000..e5c1beb4a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebClientBase.cs @@ -0,0 +1,106 @@ + +using System.Threading.Tasks; +using Tango.Web; +using Tango.Web.Authentication; + +namespace Tango.PPC.Common.Web +{ + /// <summary> + /// Represents a machine service PPC web client. + /// </summary> + /// <seealso cref="Tango.Web.TangoWebClient" /> + public abstract class PPCWebClientBase : TangoWebClient<Tango.PPC.Common.Web.LoginRequest, Tango.PPC.Common.Web.LoginResponse> + { + /// <summary> + /// Initializes a new instance of the <see cref="PPCWebClientBase"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + /// <param name="token">Existing token.</param> + public PPCWebClientBase(DeploymentSlot environment, WebToken token) : base(environment, "PPC", token) + { + + } + + /// <summary> + /// Initializes a new instance of the <see cref="PPCWebClientBase"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + public PPCWebClientBase(DeploymentSlot environment) : this(environment, null) + { + + } + + /// <summary> + /// Executes the MachineSetup action and returns Tango.PPC.Common.Web.MachineSetupResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.MachineSetupResponse> MachineSetup(Tango.PPC.Common.Web.MachineSetupRequest request) + { + return Post<Tango.PPC.Common.Web.MachineSetupRequest, Tango.PPC.Common.Web.MachineSetupResponse>("MachineSetup", request); + } + + /// <summary> + /// Executes the MachineUpdate action and returns Tango.PPC.Common.Web.DownloadUpdateResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.DownloadUpdateResponse> MachineUpdate(Tango.PPC.Common.Web.DownloadUpdateRequest request) + { + return Post<Tango.PPC.Common.Web.DownloadUpdateRequest, Tango.PPC.Common.Web.DownloadUpdateResponse>("MachineUpdate", request); + } + + /// <summary> + /// Executes the CheckForUpdate action and returns Tango.PPC.Common.Web.CheckForUpdateResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.CheckForUpdateResponse> CheckForUpdate(Tango.PPC.Common.Web.CheckForUpdateRequest request) + { + return Post<Tango.PPC.Common.Web.CheckForUpdateRequest, Tango.PPC.Common.Web.CheckForUpdateResponse>("CheckForUpdate", request); + } + + /// <summary> + /// Executes the UpdateDB action and returns Tango.PPC.Common.Web.UpdateDBResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.UpdateDBResponse> UpdateDB(Tango.PPC.Common.Web.UpdateDBRequest request) + { + return Post<Tango.PPC.Common.Web.UpdateDBRequest, Tango.PPC.Common.Web.UpdateDBResponse>("UpdateDB", request); + } + + /// <summary> + /// Executes the GetLatestVersion action and returns Tango.PPC.Common.Web.LatestVersionResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.LatestVersionResponse> GetLatestVersion(Tango.PPC.Common.Web.LatestVersionRequest request) + { + return Post<Tango.PPC.Common.Web.LatestVersionRequest, Tango.PPC.Common.Web.LatestVersionResponse>("GetLatestVersion", request); + } + + /// <summary> + /// Executes the UploadVersion action and returns Tango.PPC.Common.Web.UploadVersionResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.UploadVersionResponse> UploadVersion(Tango.PPC.Common.Web.UploadVersionRequest request) + { + return Post<Tango.PPC.Common.Web.UploadVersionRequest, Tango.PPC.Common.Web.UploadVersionResponse>("UploadVersion", request); + } + + /// <summary> + /// Executes the NotifyUploadCompleted action and returns Tango.PPC.Common.Web.UploadCompletedResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.UploadCompletedResponse> NotifyUploadCompleted(Tango.PPC.Common.Web.UploadCompletedRequest request) + { + return Post<Tango.PPC.Common.Web.UploadCompletedRequest, Tango.PPC.Common.Web.UploadCompletedResponse>("NotifyUploadCompleted", request); + } + + /// <summary> + /// Executes the GetMachineVersions action and returns Tango.PPC.Common.Web.MachineVersionsResponse. + /// </summary> + /// <returns></returns> + public Task<Tango.PPC.Common.Web.MachineVersionsResponse> GetMachineVersions(Tango.PPC.Common.Web.MachineVersionsRequest request) + { + return Post<Tango.PPC.Common.Web.MachineVersionsRequest, Tango.PPC.Common.Web.MachineVersionsResponse>("GetMachineVersions", request); + } + + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebService.cs deleted file mode 100644 index 02821e8d4..000000000 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Web/PPCWebService.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Settings; -using Tango.Transport.Web; -using Tango.Web; - -namespace Tango.PPC.Common.Web -{ - public class PPCWebService : IPPCWebService - { - private WebTransportClient _client; - - public DeploymentSlot Environment { get; set; } - - public PPCWebService() - { - _client = new WebTransportClient(); - Environment = SettingsManager.Default.GetOrCreate<PPCSettings>().DeploymentSlot; - } - - private String GetAddress() - { - return Environment.ToAddress() + "/api/PPC/"; - } - - public Task<UploadVersionResponse> UploadVersion(UploadVersionRequest request) - { - return _client.PostJson<UploadVersionRequest, UploadVersionResponse>(GetAddress() + "UploadVersion", request); - } - - public Task<UploadCompletedResponse> NotifyUploadCompleted(UploadCompletedRequest request) - { - return _client.PostJson<UploadCompletedRequest, UploadCompletedResponse>(GetAddress() + "NotifyUploadCompleted", request); - } - - public Task<LatestVersionResponse> GetLatestVersion(LatestVersionRequest request) - { - return _client.PostJson<LatestVersionRequest, LatestVersionResponse>(GetAddress() + "GetLatestVersion", request); - } - - public Task<MachineVersionsResponse> GetMachineVersions() - { - return _client.PostJson<MachineVersionsRequest, MachineVersionsResponse>(GetAddress() + "GetMachineVersions", new MachineVersionsRequest()); - } - - public Task<CheckForUpdateResponse> CheckForUpdate(CheckForUpdateRequest request) - { - return _client.PostJson<CheckForUpdateRequest, CheckForUpdateResponse>(GetAddress() + "CheckForUpdate", request); - } - - public Task<DownloadUpdateResponse> MachineUpdate(DownloadUpdateRequest request) - { - return _client.PostJson<DownloadUpdateRequest, DownloadUpdateResponse>(GetAddress() + "MachineUpdate", request); - } - - public Task<MachineSetupResponse> MachineSetup(MachineSetupRequest request) - { - return _client.PostJson<MachineSetupRequest, MachineSetupResponse>(GetAddress() + "MachineSetup", request); - } - - public Task<UpdateDBResponse> UpdateDB(UpdateDBRequest request) - { - return _client.PostJson<UpdateDBRequest, UpdateDBResponse>(GetAddress() + "UpdateDB", request); - } - - public Task<LoginResponse> Login(LoginRequest request) - { - return _client.PostJson<LoginRequest, LoginResponse>(GetAddress() + "Login", request); - } - - public void Dispose() - { - _client.Dispose(); - } - } -} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Publisher.UI/MainWindowVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.Publisher.UI/MainWindowVM.cs index 6c7e1d005..1d77b152f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Publisher.UI/MainWindowVM.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Publisher.UI/MainWindowVM.cs @@ -35,7 +35,7 @@ namespace Tango.PPC.Publisher.UI { public class MainWindowVM : ViewModel { - private IPPCWebService _client; + private PPCWebClient _client; private PPCPublisher _publisher; private PublishOptions _options; @@ -102,7 +102,7 @@ namespace Tango.PPC.Publisher.UI public MainWindowVM() { - _client = new PPCWebService(); + _client = new PPCWebClient(); var settings = SettingsManager.Default.GetOrCreate<PublisherSettings>(); Options = settings.Options; @@ -152,7 +152,7 @@ namespace Tango.PPC.Publisher.UI private async Task UpdateAvailableMachineVersions() { IsFree = false; - _machineVersions = (await _client.GetMachineVersions()).MachineVersions; + _machineVersions = (await _client.GetMachineVersions(new MachineVersionsRequest())).MachineVersions; _selectedMachineVersion = MachineVersions.OrderBy(x => x.Version).LastOrDefault(); RaisePropertyChanged(nameof(MachineVersions)); RaisePropertyChanged(nameof(SelectedMachineVersion)); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs index 4eb5475f0..018c9b223 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs @@ -22,6 +22,7 @@ using Tango.PPC.Common.RemoteAssistance; using Tango.PPC.Common.Storage; using Tango.PPC.Common.Threading; using Tango.PPC.Common.UWF; +using Tango.PPC.Common.Web; using Tango.PPC.UI.Authentication; using Tango.PPC.UI.Connectivity; using Tango.PPC.UI.Modules; @@ -69,7 +70,9 @@ namespace Tango.PPC.UI TangoIOC.Default.Unregister<IStorageProvider>(); TangoIOC.Default.Unregister<IUnifiedWriteFilterManager>(); TangoIOC.Default.Unregister<IOperationSystemManager>(); + TangoIOC.Default.Unregister<PPCWebClient>(); + TangoIOC.Default.Register<PPCWebClient, PPCWebClient>(new PPCWebClient()); TangoIOC.Default.Register<IDispatcherProvider, DefaultDispatcherProvider>(new DefaultDispatcherProvider(Application.Current.Dispatcher)); TangoIOC.Default.Register<INotificationProvider, DefaultNotificationProvider>(); TangoIOC.Default.Register<IAuthenticationProvider, DefaultAuthenticationProvider>(); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs index 514a517ff..1bd1e1eea 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs @@ -18,6 +18,7 @@ using Tango.PPC.Common.Connection; using Tango.PPC.Common.MachineSetup; using Tango.PPC.Common.Navigation; using Tango.PPC.Common.OS; +using Tango.PPC.Common.Web; using Tango.PPC.UI.ViewsContracts; using Tango.Settings; using Tango.SharedUI.Helpers; @@ -60,6 +61,7 @@ namespace Tango.PPC.UI.ViewModels private MachineSetupResult _setup_result; private IOperationSystemManager _operationSystemManager; private IPPCApplicationManager _appManager; + private PPCWebClient _ppcWebClient; #region Properties @@ -192,8 +194,9 @@ namespace Tango.PPC.UI.ViewModels /// </summary> /// <param name="applicationManager">The application manager.</param> /// <param name="machineSetupManager">The machine setup manager.</param> - public MachineSetupViewVM(IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager, IOperationSystemManager operationSystemManager) + public MachineSetupViewVM(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager, IOperationSystemManager operationSystemManager) { + _ppcWebClient = ppcWebClient; _appManager = applicationManager; MachineSetupManager = machineSetupManager; @@ -210,7 +213,7 @@ namespace Tango.PPC.UI.ViewModels InstallCommand = new RelayCommand(Install); RestartCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.WelcomeView); }); TimeZoneSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.EnvironmentView); }); - EnvironmentSelectedCommand = new RelayCommand(() => + EnvironmentSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.SetupWelcomeView); Settings.DeploymentSlot = DeploymentSlot; @@ -348,8 +351,9 @@ namespace Tango.PPC.UI.ViewModels try { + _ppcWebClient.Environment = DeploymentSlot; await _operationSystemManager.ChangeTimeZone(SelectedTimeZone); - _setup_result = await MachineSetupManager.Setup(SerialNumber, HostAddress); + _setup_result = await MachineSetupManager.Setup(SerialNumber); State = MachineSetupStates.Completed; LogManager.Log("Machine setup completed."); await NavigateTo(MachineSetupView.SetupCompletedView); diff --git a/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj b/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj index 436bfae33..98c063a86 100644 --- a/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj +++ b/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj @@ -87,6 +87,7 @@ <Compile Include="ObservablesAdapterFile.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Property.cs" /> + <Compile Include="TangoWebClientCodeFile.cs" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="Templates\CodeFile.cshtml" /> @@ -118,11 +119,12 @@ <EmbeddedResource Include="Templates\EntityCodeFileJavaExtension.cshtml" /> <EmbeddedResource Include="Templates\ObservablesStaticCollectionsFile.cshtml" /> <EmbeddedResource Include="Templates\EntityInheritedCodeFile.cshtml" /> + <EmbeddedResource Include="Templates\TangoWebClientCodeFile.cshtml" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> + <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> </VisualStudio> </ProjectExtensions> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. diff --git a/Software/Visual_Studio/Tango.CodeGeneration/TangoWebClientCodeFile.cs b/Software/Visual_Studio/Tango.CodeGeneration/TangoWebClientCodeFile.cs new file mode 100644 index 000000000..81d80a393 --- /dev/null +++ b/Software/Visual_Studio/Tango.CodeGeneration/TangoWebClientCodeFile.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.CodeGeneration +{ + public class TangoWebClientCodeFile : Class + { + public class ControllerAction + { + public String Name { get; set; } + public String Request { get; set; } + public String Response { get; set; } + } + + public String Namespace { get; set; } + public String ControllerName { get; set; } + public String LoginRequest { get; set; } + public String LoginResponse { get; set; } + + public List<ControllerAction> Actions { get; set; } + + public TangoWebClientCodeFile() + { + Actions = new List<ControllerAction>(); + } + } +} diff --git a/Software/Visual_Studio/Tango.CodeGeneration/Templates/TangoWebClientCodeFile.cshtml b/Software/Visual_Studio/Tango.CodeGeneration/Templates/TangoWebClientCodeFile.cshtml new file mode 100644 index 000000000..100171849 --- /dev/null +++ b/Software/Visual_Studio/Tango.CodeGeneration/Templates/TangoWebClientCodeFile.cshtml @@ -0,0 +1,51 @@ +@{ + Tango.CodeGeneration.TangoWebClientCodeFile model = Model as Tango.CodeGeneration.TangoWebClientCodeFile; +} + +using System.Threading.Tasks; +using Tango.Web; +using Tango.Web.Authentication; + +namespace @(model.Namespace) +{ + /// <summary> + /// Represents a machine service @(model.ControllerName) web client. + /// </summary> + /// <seealso cref="Tango.Web.TangoWebClient" /> + public abstract class @(model.Name) : TangoWebClient<@(model.LoginRequest), @(model.LoginResponse)> + { + /// <summary> + /// Initializes a new instance of the <see cref="@(model.Name)"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + /// <param name="token">Existing token.</param> + public @(model.Name)(DeploymentSlot environment, WebToken token) : base(environment, "@(model.ControllerName)", token) + { + + } + + /// <summary> + /// Initializes a new instance of the <see cref="@(model.Name)"/> class. + /// </summary> + /// <param name="environment">The environment.</param> + public @(model.Name)(DeploymentSlot environment) : this(environment, null) + { + + } + + @foreach (var action in model.Actions) + { + <div> + /// <summary> + /// Executes the @(action.Name) action and returns @(action.Response). + /// </summary> + /// <returns></returns> + public Task<@(action.Response)> @(action.Name)(@(action.Request) request) + { + return Post<@(action.Request), @(action.Response)>("@(action.Name)", request); + } + + </div> + } + } +}
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Core/Helpers/PathHelper.cs b/Software/Visual_Studio/Tango.Core/Helpers/PathHelper.cs index ab0a189ed..ee2e3e132 100644 --- a/Software/Visual_Studio/Tango.Core/Helpers/PathHelper.cs +++ b/Software/Visual_Studio/Tango.Core/Helpers/PathHelper.cs @@ -31,6 +31,22 @@ namespace Tango.Core.Helpers } /// <summary> + /// Gets the solution folder. + /// </summary> + /// <returns></returns> + public static String GetSolutionFolder() + { + DirectoryInfo path = new DirectoryInfo(GetStartupPath()); + + while (path.Name != "Visual_Studio") + { + path = path.Parent; + } + + return path.FullName; + } + + /// <summary> /// Tries to delete the specified file. /// </summary> /// <param name="path">The file path.</param> diff --git a/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj b/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj index 9591c8bdb..bea51be51 100644 --- a/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj +++ b/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj @@ -112,6 +112,7 @@ <Compile Include="Web\IWebRequestMessage.cs" /> <Compile Include="Web\IWebResponseMessage.cs" /> <Compile Include="Web\IWebTransportMessage.cs" /> + <Compile Include="Web\SessionExpiredException.cs" /> <Compile Include="Web\StorageBlobDownloader.cs" /> <Compile Include="Web\StorageBlobProgressEventArgs.cs" /> <Compile Include="Web\StorageBlobUploader.cs" /> @@ -146,7 +147,7 @@ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <ProjectExtensions> <VisualStudio> - <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" /> + <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" /> </VisualStudio> </ProjectExtensions> </Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Transport/Web/SessionExpiredException.cs b/Software/Visual_Studio/Tango.Transport/Web/SessionExpiredException.cs new file mode 100644 index 000000000..7ee553b4a --- /dev/null +++ b/Software/Visual_Studio/Tango.Transport/Web/SessionExpiredException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Transport.Web +{ + public class SessionExpiredException : Exception + { + public SessionExpiredException(String message) : base(message) + { + + } + } +} diff --git a/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs b/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs index 227ecefa3..188203945 100644 --- a/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs +++ b/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs @@ -69,7 +69,14 @@ namespace Tango.Transport.Web } catch (HttpRequestException ex) { - throw new HttpRequestException(ex.Message + " " + JObject.Parse(data).GetValue("Message")); + String message = JObject.Parse(data).GetValue("Message").ToString(); + + if (message == "Session Expired.") + { + throw new SessionExpiredException(ex.Message); + } + + throw new HttpRequestException(ex.Message + " " + message); } return JsonConvert.DeserializeObject<Response>(data); diff --git a/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs b/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs index ca1653e3e..ababd1793 100644 --- a/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs +++ b/Software/Visual_Studio/Tango.UnitTesting/MachineService_TST.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tango.Transport.Web; using Tango.Web.Authentication; namespace Tango.UnitTesting @@ -30,8 +31,8 @@ namespace Tango.UnitTesting TokenObject t2 = new TokenObject(); t2.Value = "Sagi"; - String token1 = tokensManager.GetOrCreate(t1); - String token2 = tokensManager.GetOrCreate(t2); + String token1 = tokensManager.GetOrCreate(t1).AccessToken; + String token2 = tokensManager.GetOrCreate(t2).AccessToken; Assert.AreEqual(tokensManager.GetTokenObject(token1), t1); Assert.AreEqual(tokensManager.GetTokenObject(token2), t2); @@ -39,7 +40,7 @@ namespace Tango.UnitTesting TokenObject t3 = new TokenObject(); t3.Value = "Roy"; - String token3 = tokensManager.GetOrCreate(t3); + String token3 = tokensManager.GetOrCreate(t3).AccessToken; Assert.AreEqual(token3, token1); Assert.AreEqual(tokensManager.GetTokenObject(token1), tokensManager.GetTokenObject(token3)); @@ -52,13 +53,13 @@ namespace Tango.UnitTesting Thread.Sleep(2000); - String recent_token = tokensManager.GetOrCreate(tOld); + String recent_token = tokensManager.GetOrCreate(tOld).AccessToken; Thread.Sleep(1000); - Assert.IsTrue(tokensManager.Exists(recent_token)); + tokensManager.Validate(recent_token); - Assert.IsFalse(tokensManager.Exists(token1)); + Assert.ThrowsException<SessionExpiredException>(() => tokensManager.Validate(token1)); } } } diff --git a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs index 8e0eb4288..890d69d53 100644 --- a/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs +++ b/Software/Visual_Studio/Tango.Web/Authentication/TokensManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Security.Authentication; using System.Text; using System.Threading.Tasks; +using Tango.Transport.Web; namespace Tango.Web.Authentication { @@ -13,7 +14,7 @@ namespace Tango.Web.Authentication private class TokenWrapper : IEquatable<TokenWrapper> { - public DateTime Date { get; set; } + public WebToken WebToken { get; set; } public T Value { get; set; } @@ -31,61 +32,51 @@ namespace Tango.Web.Authentication public TimeSpan ExpirationTime { get; set; } - public String GetOrCreate(T tokenObject) + public WebToken GetOrCreate(T tokenObject) { var existing_token = _tokens.FirstOrDefault(x => x.Value.Value.Equals(tokenObject)); if (existing_token.Key != null) { - return existing_token.Key; + return existing_token.Value.WebToken; } else { String token = Guid.NewGuid().ToString(); - _tokens.Add(token, new TokenWrapper() + TokenWrapper wrapper = new TokenWrapper() { Value = tokenObject, - Date = DateTime.Now, - }); - return token; + WebToken = new WebToken() + { + AccessToken = token, + Expiration = DateTime.UtcNow.Add(ExpirationTime) + }, + }; + + _tokens.Add(token, wrapper); + return wrapper.WebToken; } } - public bool Exists(String token) + public void Validate(String token) { - RemoveOrRenewExpired(token); - - return _tokens.ContainsKey(token); + GetTokenObject(token); } public T GetTokenObject(String token) { - RemoveOrRenewExpired(token); - if (!_tokens.ContainsKey(token)) { throw new AuthenticationException("Invalid token."); } - return _tokens[token].Value; - } - - private void RemoveOrRenewExpired(String token) - { - DateTime now = DateTime.Now; - - if (_tokens.ContainsKey(token)) + if (DateTime.UtcNow > _tokens[token].WebToken.Expiration) { - _tokens[token].Date = now; + _tokens.Remove(token); + throw new SessionExpiredException("Session Expired."); } - foreach (var t in _tokens.ToList()) - { - if (now > t.Value.Date.Add(ExpirationTime)) - { - _tokens.Remove(t.Key); - } - } + return _tokens[token].Value; } } } diff --git a/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs b/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs new file mode 100644 index 000000000..71ec6eb0b --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/Authentication/WebToken.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Web.Authentication +{ + public class WebToken + { + public DateTime Expiration { get; set; } + public String AccessToken { get; set; } + } +} diff --git a/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs b/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs new file mode 100644 index 000000000..17ac6636f --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/Authentication/WebTokenResponse.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Transport.Web; + +namespace Tango.Web.Authentication +{ + public class WebTokenResponse : WebResponseMessage + { + public WebToken WebToken { get; set; } + } +} diff --git a/Software/Visual_Studio/Tango.Web/Tango.Web.csproj b/Software/Visual_Studio/Tango.Web/Tango.Web.csproj index 98352e131..56711df28 100644 --- a/Software/Visual_Studio/Tango.Web/Tango.Web.csproj +++ b/Software/Visual_Studio/Tango.Web/Tango.Web.csproj @@ -244,7 +244,9 @@ <Link>GlobalVersionInfo.cs</Link> </Compile> <Compile Include="ActiveDirectory\ActiveDirectoryManager.cs" /> + <Compile Include="Authentication\WebTokenResponse.cs" /> <Compile Include="Authentication\TokensManager.cs" /> + <Compile Include="Authentication\WebToken.cs" /> <Compile Include="DeploymentSlot.cs" /> <Compile Include="DeploymentSlotAddressAttribute.cs" /> <Compile Include="ExtensionMethods\DeploymentSlotExtensions.cs" /> @@ -253,6 +255,7 @@ <Compile Include="Storage\ExtensionMethods.cs" /> <Compile Include="Storage\StorageManager.cs" /> <Compile Include="TangoWebApplication.cs" /> + <Compile Include="TangoWebClient.cs" /> <Compile Include="WebConfig.cs" /> <Compile Include="Controllers\JsonController.cs" /> <Compile Include="Formatters\JsonNetFormatter.cs" /> @@ -289,6 +292,10 @@ <Project>{d8f1ad85-526a-4f50-b6dc-d437af63d8d8}</Project> <Name>Tango.Settings</Name> </ProjectReference> + <ProjectReference Include="..\Tango.Transport\Tango.Transport.csproj"> + <Project>{74e700b0-1156-4126-be40-ee450d3c3026}</Project> + <Name>Tango.Transport</Name> + </ProjectReference> </ItemGroup> <ItemGroup /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> diff --git a/Software/Visual_Studio/Tango.Web/TangoWebClient.cs b/Software/Visual_Studio/Tango.Web/TangoWebClient.cs new file mode 100644 index 000000000..bb943533a --- /dev/null +++ b/Software/Visual_Studio/Tango.Web/TangoWebClient.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using Tango.Transport.Web; +using Tango.Web.Authentication; + +namespace Tango.Web +{ + public abstract class TangoWebClient<TLoginRequest, TLoginResponse> : IDisposable where TLoginRequest : WebRequestMessage where TLoginResponse : WebTokenResponse + { + private WebTransportClient _client; + private bool _disposed; + private TLoginRequest _lastLoginRequest; + + private DeploymentSlot _environment; + public DeploymentSlot Environment + { + get { return _environment; } + set + { + if (_environment != value) + { + _lastLoginRequest = null; + IsAuthenticated = false; + } + + _environment = value; + } + } + + public String Controller { get; private set; } + public WebToken WebToken { get; private set; } + public bool IsAuthenticated { get; private set; } + + public TangoWebClient(DeploymentSlot environment, String controller) + { + Controller = controller; + Environment = environment; + _client = new WebTransportClient(); + } + + public TangoWebClient(DeploymentSlot environment, String controller, WebToken token) : this(environment, controller) + { + WebToken = token; + } + + public async Task<TLoginResponse> Login(TLoginRequest request) + { + var response = await _client.PostJson<TLoginRequest, TLoginResponse>(GetActionAddress("Login"), request); + WebToken = response.WebToken; + _lastLoginRequest = request; + IsAuthenticated = true; + return response; + } + + protected virtual async Task<TResponse> Post<TRequest, TResponse>(String action, TRequest request) where TRequest : class, IWebRequestMessage where TResponse : class, IWebResponseMessage + { + bool has_own_token = false; + + if (request is WebRequestSecureMessage) + { + has_own_token = (request as WebRequestSecureMessage).AccessToken != null; + + if (!has_own_token) + { + if (IsAuthenticated) + { + if (DateTime.UtcNow >= WebToken.Expiration) + { + await Login(_lastLoginRequest); + } + } + else + { + throw new AuthenticationException("This tango web client has not been authenticated with the service. Please use the login method to authenticate."); + } + + (request as WebRequestSecureMessage).AccessToken = WebToken.AccessToken; + } + } + + try + { + var response = await _client.PostJson<TRequest, TResponse>(GetActionAddress(action), request); + return response; + } + catch (SessionExpiredException) + { + if (!has_own_token) + { + try + { + await Login(_lastLoginRequest); + + if (request is WebRequestSecureMessage) + { + (request as WebRequestSecureMessage).AccessToken = WebToken.AccessToken; + } + + var response = await _client.PostJson<TRequest, TResponse>(GetActionAddress(action), request); + return response; + } + catch + { + throw; + } + } + else + { + throw; + } + } + catch + { + throw; + } + } + + private String GetActionAddress(String action) + { + return GetServiceAddress() + action; + } + + protected virtual String GetServiceAddress() + { + return Environment.ToAddress() + $"/api/{Controller}/"; + } + + public virtual void Dispose() + { + if (!_disposed) + { + _disposed = true; + _client.Dispose(); + } + } + } +} diff --git a/Software/Visual_Studio/Tango.sln b/Software/Visual_Studio/Tango.sln index f8eb5e42e..6e3e89f19 100644 --- a/Software/Visual_Studio/Tango.sln +++ b/Software/Visual_Studio/Tango.sln @@ -260,6 +260,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tango.MachineStudio.Publish EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tango.PPC.Publisher.UI", "PPC\Tango.PPC.Publisher.UI\Tango.PPC.Publisher.UI.csproj", "{FABA6A2B-C7EA-4C58-A125-150FA5B4C72F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tango.WebClientGenerator", "Utilities\Tango.WebClientGenerator\Tango.WebClientGenerator.csproj", "{E470673E-AEC8-4555-9136-D2F66B02DBFB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution AppVeyor|Any CPU = AppVeyor|Any CPU @@ -4637,6 +4639,46 @@ Global {FABA6A2B-C7EA-4C58-A125-150FA5B4C72F}.Release|x64.Build.0 = Release|Any CPU {FABA6A2B-C7EA-4C58-A125-150FA5B4C72F}.Release|x86.ActiveCfg = Release|Any CPU {FABA6A2B-C7EA-4C58-A125-150FA5B4C72F}.Release|x86.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|Any CPU.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|Any CPU.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|ARM.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|ARM.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|ARM64.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|ARM64.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|x64.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|x64.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|x86.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.AppVeyor|x86.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|ARM.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|ARM.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|ARM64.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|x64.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|x64.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|x86.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Debug|x86.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|Any CPU.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|Any CPU.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|ARM.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|ARM.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|ARM64.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|ARM64.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|x64.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|x64.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|x86.ActiveCfg = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.DefaultBuild|x86.Build.0 = Debug|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|Any CPU.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|ARM.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|ARM.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|ARM64.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|ARM64.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|x64.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|x64.Build.0 = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|x86.ActiveCfg = Release|Any CPU + {E470673E-AEC8-4555-9136-D2F66B02DBFB}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -4719,14 +4761,15 @@ Global {6F924F92-611B-41CF-A6E6-443E69D4CA83} = {57DF2A95-5DDD-4830-A4AF-B484B59C7C2B} {E711CD86-89C1-432C-9C60-BFF30BBBFB3A} = {57DF2A95-5DDD-4830-A4AF-B484B59C7C2B} {FABA6A2B-C7EA-4C58-A125-150FA5B4C72F} = {C81ED1A3-D18C-4D80-A8F5-061994A14A60} + {E470673E-AEC8-4555-9136-D2F66B02DBFB} = {5F6BBAA8-EAD0-4B18-97E5-55B4F56DD760} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - BuildVersion_UseGlobalSettings = False - BuildVersion_AssemblyInfoFilename = Properties\AssemblyInfo.cs - BuildVersion_StartDate = 2000/1/1 - BuildVersion_UpdateFileVersion = False - BuildVersion_UpdateAssemblyVersion = True - BuildVersion_BuildVersioningStyle = None.None.Increment.DeltaBaseYearDayOfYear SolutionGuid = {7986F7F4-A86A-4994-B1B6-0988D7F057B6} + BuildVersion_BuildVersioningStyle = None.None.Increment.DeltaBaseYearDayOfYear + BuildVersion_UpdateAssemblyVersion = True + BuildVersion_UpdateFileVersion = False + BuildVersion_StartDate = 2000/1/1 + BuildVersion_AssemblyInfoFilename = Properties\AssemblyInfo.cs + BuildVersion_UseGlobalSettings = False EndGlobalSection EndGlobal diff --git a/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/App.config b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/App.config new file mode 100644 index 000000000..d748e2305 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/App.config @@ -0,0 +1,78 @@ +<?xml version="1.0" encoding="utf-8"?> +<configuration> + <startup> + <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> + </startup> + <runtime> + <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> + <dependentAssembly> + <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-2.7.0.0" newVersion="2.7.0.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Reactive.Core" publicKeyToken="94bc3704cddfc263" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-3.0.3000.0" newVersion="3.0.3000.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-1.2.2.0" newVersion="1.2.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-1.4.2.0" newVersion="1.4.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Console" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" /> + </dependentAssembly> + <dependentAssembly> + <assemblyIdentity name="System.Diagnostics.StackTrace" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" /> + </dependentAssembly> + </assemblyBinding> + </runtime> +</configuration>
\ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Program.cs b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Program.cs new file mode 100644 index 000000000..e95f57344 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Program.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.CodeGeneration; +using Tango.Core.Helpers; +using Tango.Transport.Web; +using Tango.Web.Authentication; +using Tango.Web.Controllers; + +namespace Tango.WebClientGenerator +{ + class Program + { + static void Main(string[] args) + { + //Generate PPC client. + GenerateWebClient<PPC.Common.Web.LoginRequest, PPC.Common.Web.LoginResponse, MachineService.Controllers.PPCController>("Tango.PPC.Common.Web", "PPCWebClientBase", PathHelper.GetSolutionFolder() + @"\PPC\Tango.PPC.Common\Web"); + + //Generate Machine Studio client. + GenerateWebClient<MachineStudio.Common.Web.LoginRequest, MachineStudio.Common.Web.LoginResponse, MachineService.Controllers.MachineStudioController>("Tango.MachineStudio.Common.Web", "MachineStudioWebClientBase", PathHelper.GetSolutionFolder() + @"\MachineStudio\Tango.MachineStudio.Common\Web"); + } + + private static void GenerateWebClient<TLoginRequest, TLoginResponse, TController>(String nameSpace, String name, String path) where TLoginRequest : WebRequestMessage where TLoginResponse : WebTokenResponse where TController : JsonController + { + TangoWebClientCodeFile model = new TangoWebClientCodeFile(); + model.Namespace = nameSpace; + model.ControllerName = typeof(TController).Name.Replace("Controller", ""); + model.Name = name; + model.LoginRequest = typeof(TLoginRequest).FullName; + model.LoginResponse = typeof(TLoginResponse).FullName; + + foreach (var action in typeof(TController).GetMethods().Where(x => typeof(WebResponseMessage).IsAssignableFrom(x.ReturnType) && x.Name != "Login")) + { + model.Actions.Add(new TangoWebClientCodeFile.ControllerAction() + { + Name = action.Name, + Request = action.GetParameters()[0].ParameterType.FullName, + Response = action.ReturnType.FullName, + }); + } + + String code = model.GenerateCode(); + File.WriteAllText(Path.Combine(path, name + ".cs"), code); + } + } +} diff --git a/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Properties/AssemblyInfo.cs b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..e9745e48b --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tango.WebClientGenerator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tango.WebClientGenerator")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e470673e-aec8-4555-9136-d2f66b02dbfb")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Tango.WebClientGenerator.csproj b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Tango.WebClientGenerator.csproj new file mode 100644 index 000000000..da8738617 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/Tango.WebClientGenerator.csproj @@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{E470673E-AEC8-4555-9136-D2F66B02DBFB}</ProjectGuid> + <OutputType>Exe</OutputType> + <RootNamespace>Tango.WebClientGenerator</RootNamespace> + <AssemblyName>Tango.WebClientGenerator</AssemblyName> + <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL"> + <HintPath>..\..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Net.Http" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="App.config" /> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\MachineStudio\Tango.MachineStudio.Common\Tango.MachineStudio.Common.csproj"> + <Project>{cb0b0aa2-bb24-4bca-a720-45e397684e12}</Project> + <Name>Tango.MachineStudio.Common</Name> + </ProjectReference> + <ProjectReference Include="..\..\PPC\Tango.PPC.Common\Tango.PPC.Common.csproj"> + <Project>{0be74eee-22cb-4dba-b896-793b9e1a3ac0}</Project> + <Name>Tango.PPC.Common</Name> + </ProjectReference> + <ProjectReference Include="..\..\Tango.CodeGeneration\Tango.CodeGeneration.csproj"> + <Project>{caedae94-11ed-473c-888a-268a6d38cd20}</Project> + <Name>Tango.CodeGeneration</Name> + </ProjectReference> + <ProjectReference Include="..\..\Tango.Core\Tango.Core.csproj"> + <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> + <Name>Tango.Core</Name> + </ProjectReference> + <ProjectReference Include="..\..\Tango.Transport\Tango.Transport.csproj"> + <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="..\..\Web\Tango.MachineService\Tango.MachineService.csproj"> + <Project>{3f09b230-5aac-4651-ba7a-19f3bdfcf701}</Project> + <Name>Tango.MachineService</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> +</Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/packages.config b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/packages.config new file mode 100644 index 000000000..011bf97c7 --- /dev/null +++ b/Software/Visual_Studio/Utilities/Tango.WebClientGenerator/packages.config @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="CommandLineParser" version="1.9.71" targetFramework="net461" /> +</packages>
\ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index bafac31f0..82082ca2f 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -348,7 +348,7 @@ namespace Tango.MachineService.Controllers Password = request.Password, }, - AccessToken = TokensManager.GetOrCreate(user.Guid), + WebToken = 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 064a44fdb..5e41e0d8a 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -439,7 +439,7 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Domain user found but the database entry validation failed."); } - response.AccessToken = TokensManager.GetOrCreate(new TokenObject() + response.WebToken = TokensManager.GetOrCreate(new TokenObject() { Mode = LoginMode.User, UserGuid = user.Guid, @@ -454,7 +454,7 @@ namespace Tango.MachineService.Controllers throw new AuthenticationException("Invalid serial number."); } - response.AccessToken = TokensManager.GetOrCreate(new TokenObject() + response.WebToken = TokensManager.GetOrCreate(new TokenObject() { Mode = LoginMode.Machine, UserGuid = machine.Guid, diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs index 3c5798e82..dfa78f0a0 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Filters/MachineStudioLoginFilter.cs @@ -20,9 +20,13 @@ namespace Tango.MachineService.Filters var json = actionContext.Request.Content.ReadAsStringAsync().Result; WebRequestSecureMessage msg = JsonConvert.DeserializeObject<WebRequestSecureMessage>(json); - if (!Controllers.MachineStudioController.TokensManager.Exists(msg.AccessToken)) + try { - throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new AuthenticationException("Invalid Token."))); + Controllers.MachineStudioController.TokensManager.Validate(msg.AccessToken); + } + catch (Exception ex) + { + throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex)); } base.OnActionExecuting(actionContext); diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs b/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs index 45c436ef7..322023b7d 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Filters/PPCLoginFilter.cs @@ -20,9 +20,13 @@ namespace Tango.MachineService.Filters var json = actionContext.Request.Content.ReadAsStringAsync().Result; WebRequestSecureMessage msg = JsonConvert.DeserializeObject<WebRequestSecureMessage>(json); - if (!Controllers.PPCController.TokensManager.Exists(msg.AccessToken)) + try { - throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new AuthenticationException("Invalid Token."))); + Controllers.PPCController.TokensManager.Validate(msg.AccessToken); + } + catch (Exception ex) + { + throw new HttpResponseException(actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, ex)); } base.OnActionExecuting(actionContext); |
