using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.BL.Entities;
using Tango.MachineStudio.Common.Authentication;
using Tango.BL;
using Tango.BL.Enumerations;
using System.Data.Entity;
using Tango.Transport.Web;
using Tango.Settings;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.StudioApplication;
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
{
/// <summary>
/// Represents the default Machine Studio <see cref="IAuthenticationProvider">Authentication provider</see>
/// </summary>
/// <seealso cref="Tango.Core.ExtendedObject" />
/// <seealso cref="Tango.MachineStudio.Common.Authentication.IAuthenticationProvider" />
public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider
{
private MachineStudioWebClient _client;
private DispatcherTimer _refreshTokenTimer;
private User _currentUser;
/// <summary>
/// Gets the current logged-in user.
/// </summary>
public User CurrentUser
{
get { return _currentUser; }
set
{
_currentUser = value;
CurrentUserChanged?.Invoke(this, _currentUser);
RaisePropertyChangedAuto();
}
}
/// <summary>
/// Occurs when the current logged-in user has changed.
/// </summary>
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;
_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>
/// Performs a user login by the specified email and password.
/// </summary>
/// <param name="email">The email.</param>
/// <param name="password">The password.</param>
/// <returns></returns>
/// <exception cref="AuthenticationException">Login failed for user " + email</exception>
public AuthenticationLoginResult Login(string email, string password, LoginMethod method, bool bypassVersionCheck = false, Action<String> logAction = null)
{
_refreshTokenTimer.Stop();
var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>();
if (!App.StartupArgs.Contains("-webDebug"))
{
_client.Environment = settings.DeploymentSlot;
}
var appVersion = AssemblyHelper.GetCurrentAssemblyVersion().ToString();
if (settings.ForceVersionUpdate)
{
appVersion = "1.0.0.0";
}
LoginResponse response = null;
try
{
logAction?.Invoke("Logging in to machine service...");