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.Tasks;
using System.Windows;
using log4net;
using static BreCalClient.Extensions;
namespace BreCalClient
@ -23,11 +24,14 @@ namespace BreCalClient
/// </summary>
public partial class MainWindow : Window
{
private ILog _log = LogManager.GetLogger(typeof(MainWindow));
private const int SHIPCALL_UPDATE_INTERVAL_SECONDS = 30;
#region Fields
private Timer _timer;
Credentials _credentials;
private readonly Dictionary<int, ShipcallControlModel> _allShipcallsDict = new();
@ -109,12 +113,11 @@ namespace BreCalClient
return;
}
Credentials credentials = new(username: textUsername.Text.Trim(),
password: textPassword.Password.Trim());
_credentials = new(username: textUsername.Text.Trim(), password: textPassword.Password.Trim());
try
{
_loginResult = await _api.LoginPostAsync(credentials);
_loginResult = await _api.LoginPostAsync(_credentials);
if (_loginResult != null)
{
if (_loginResult.Id > 0)
@ -123,6 +126,7 @@ namespace BreCalClient
this._api.Configuration.ApiKey["Authorization"] = _loginResult.Token;
this.LoadStaticLists();
this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}";
_timer = new Timer(RefreshToken, null, 4000000, Timeout.Infinite);
}
}
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)
{
this.Close();

View File

@ -28,7 +28,7 @@ def GetUser(options):
"user_name": data[0].user_name,
"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
return json.dumps(result), 200, {'Content-Type': 'application/json; charset=utf-8'}