aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs
diff options
context:
space:
mode:
authorShlomo Hecht <shlomo@twine-s.com>2020-01-27 09:19:11 +0200
committerShlomo Hecht <shlomo@twine-s.com>2020-01-27 09:19:11 +0200
commitddda6089bff56e80703c8d2dce297919edc58bf1 (patch)
tree7702c5cf169124d522eacc7f1a9e0878373baedd /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs
parent1d4d327571d4c0c9f4e17411551bd4dae1e2aed0 (diff)
parentbf2f3245339b9fd9148a2ad25b5ba3320e970cc1 (diff)
downloadTango-ddda6089bff56e80703c8d2dce297919edc58bf1.tar.gz
Tango-ddda6089bff56e80703c8d2dce297919edc58bf1.zip
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
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.cs55
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();
}
}
}