Increased token timeout to 2 hours, introduced a background task to refresh token at about 70 min interval

This commit is contained in:
Daniel Schick 2023-09-22 15:31:48 +02:00 committed by puls200
parent 215d419180
commit 6872df4278
2 changed files with 33 additions and 6 deletions

View File

@ -14,6 +14,7 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using log4net;
using static BreCalClient.Extensions; using static BreCalClient.Extensions;
namespace BreCalClient namespace BreCalClient
@ -23,12 +24,15 @@ namespace BreCalClient
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private ILog _log = LogManager.GetLogger(typeof(MainWindow));
private const int SHIPCALL_UPDATE_INTERVAL_SECONDS = 30; private const int SHIPCALL_UPDATE_INTERVAL_SECONDS = 30;
#region Fields #region Fields
private Timer _timer;
Credentials _credentials;
private readonly Dictionary<int, ShipcallControlModel> _allShipcallsDict = new(); private readonly Dictionary<int, ShipcallControlModel> _allShipcallsDict = new();
private readonly Dictionary<int, ShipcallControl> _allShipCallsControlDict = new(); private readonly Dictionary<int, ShipcallControl> _allShipCallsControlDict = new();
@ -109,12 +113,11 @@ namespace BreCalClient
return; return;
} }
Credentials credentials = new(username: textUsername.Text.Trim(), _credentials = new(username: textUsername.Text.Trim(), password: textPassword.Password.Trim());
password: textPassword.Password.Trim());
try try
{ {
_loginResult = await _api.LoginPostAsync(credentials); _loginResult = await _api.LoginPostAsync(_credentials);
if (_loginResult != null) if (_loginResult != null)
{ {
if (_loginResult.Id > 0) if (_loginResult.Id > 0)
@ -122,7 +125,8 @@ namespace BreCalClient
this.busyIndicator.IsBusy = false; this.busyIndicator.IsBusy = false;
this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this.LoadStaticLists(); this.LoadStaticLists();
this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}";
_timer = new Timer(RefreshToken, null, 4000000, Timeout.Infinite);
} }
} }
labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}";
@ -139,6 +143,29 @@ namespace BreCalClient
} }
} }
private void RefreshToken(object? state)
{
try
{
_loginResult = _api.LoginPost(_credentials);
if (_loginResult != null)
{
if (_loginResult.Id > 0)
{
this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token;
}
}
else
{
_log.Error("Token refresh: Renewed login returned empty login result");
}
}
catch (Exception ex)
{
_log.ErrorFormat("Error refreshing token: {0}", ex.Message);
}
}
private void buttonExit_Click(object sender, RoutedEventArgs e) private void buttonExit_Click(object sender, RoutedEventArgs e)
{ {
this.Close(); this.Close();

View File

@ -28,7 +28,7 @@ def GetUser(options):
"user_name": data[0].user_name, "user_name": data[0].user_name,
"user_phone": data[0].user_phone "user_phone": data[0].user_phone
} }
token = jwt_handler.generate_jwt(payload=result, lifetime=60) # generate token valid 60 mins token = jwt_handler.generate_jwt(payload=result, lifetime=120) # generate token valid 60 mins
result["token"] = token # add token to user data result["token"] = token # add token to user data
return json.dumps(result), 200, {'Content-Type': 'application/json; charset=utf-8'} return json.dumps(result), 200, {'Content-Type': 'application/json; charset=utf-8'}