diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs | 55 |
1 files changed, 52 insertions, 3 deletions
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 7fe1ae36a..26938b203 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -18,6 +18,7 @@ using Tango.Core.Helpers; using Tango.MachineStudio.Common.Web; using Tango.BL.Builders; using System.Data.Entity.Core; +using System.Windows.Threading; namespace Tango.MachineStudio.UI.Authentication { @@ -29,6 +30,7 @@ namespace Tango.MachineStudio.UI.Authentication public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider { private MachineStudioWebClient _client; + private DispatcherTimer _refreshTokenTimer; private User _currentUser; /// <summary> @@ -57,6 +59,28 @@ namespace Tango.MachineStudio.UI.Authentication public DefaultAuthenticationProvider(MachineStudioWebClient machineStudioWebClient) { _client = machineStudioWebClient; + + _refreshTokenTimer = new DispatcherTimer(); + _refreshTokenTimer.Interval = TimeSpan.FromMinutes(30); + _refreshTokenTimer.Tick += _refreshTokenTimer_Tick; + _refreshTokenTimer.Stop(); + } + + private async void _refreshTokenTimer_Tick(object sender, EventArgs e) + { + if (ObservablesContext.GetActualDataSource().Type == DataSourceType.AccessToken) + { + try + { + LogManager.Log("Refreshing database access token..."); + var response = await _client.RefreshToken(new RefreshTokenRequest()); + ObservablesContext.UpdateAccessToken(response.AccessToken, response.Expiration); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error occurred while trying to refresh the database access token."); + } + } } /// <summary> @@ -66,11 +90,16 @@ namespace Tango.MachineStudio.UI.Authentication /// <param name="password">The password.</param> /// <returns></returns> /// <exception cref="AuthenticationException">Login failed for user " + email</exception> - public AuthenticationLoginResult Login(string email, string password, bool bypassVersionCheck = false) + public AuthenticationLoginResult Login(string email, string password, LoginMethod method, bool bypassVersionCheck = false, Action<String> logAction = null) { + _refreshTokenTimer.Stop(); + var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>(); - _client.Environment = settings.DeploymentSlot; + if (!App.StartupArgs.Contains("-webDebug")) + { + _client.Environment = settings.DeploymentSlot; + } var appVersion = AssemblyHelper.GetCurrentAssemblyVersion().ToString(); @@ -83,12 +112,15 @@ namespace Tango.MachineStudio.UI.Authentication try { + logAction?.Invoke("Logging in to machine service..."); + response = _client.Login(new LoginRequest() { Email = email, Password = password, Version = appVersion, + Method = method, }).Result; } @@ -115,9 +147,20 @@ namespace Tango.MachineStudio.UI.Authentication }; } + if (response.PasswordChangeRequired) + { + return new AuthenticationLoginResult() + { + Response = response + }; + } + try { - ObservablesStaticCollections.Instance.Initialize(); + ObservablesStaticCollections.Instance.Initialize((x) => + { + logAction.Invoke(x); + }); } catch (Exception ex) { @@ -126,6 +169,7 @@ namespace Tango.MachineStudio.UI.Authentication using (ObservablesContext db = ObservablesContext.CreateDefault()) { + logAction.Invoke("Loading user permissions..."); User user = new UserBuilder(db).Set(x => x.Email.ToLower() == email.ToLower()).WithRolesAndPermissions().WithOrganization().Build(); if (user == null) @@ -135,6 +179,10 @@ namespace Tango.MachineStudio.UI.Authentication CurrentUser = user; + _refreshTokenTimer.Start(); + + logAction.Invoke("Starting application..."); + return new AuthenticationLoginResult() { User = user, @@ -149,6 +197,7 @@ namespace Tango.MachineStudio.UI.Authentication public void Logout() { CurrentUser = null; + _refreshTokenTimer.Stop(); } } } |
