2023-01-22 22:15:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Selector.MAUI.Services;
|
|
|
|
|
|
|
|
|
|
public class SessionManager
|
|
|
|
|
{
|
2023-01-28 14:41:03 +00:00
|
|
|
|
private string lastStoredKey;
|
2023-01-22 22:15:55 +00:00
|
|
|
|
private DateTime lastRefresh;
|
|
|
|
|
private readonly ISelectorNetClient _selectorNetClient;
|
|
|
|
|
private readonly ILogger<SessionManager> _logger;
|
|
|
|
|
|
|
|
|
|
public SessionManager(ISelectorNetClient selectorNetClient, ILogger<SessionManager> logger)
|
|
|
|
|
{
|
|
|
|
|
_selectorNetClient = selectorNetClient;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsLoggedIn => !string.IsNullOrWhiteSpace(lastStoredKey);
|
|
|
|
|
|
|
|
|
|
public async Task LoadUserFromDisk()
|
|
|
|
|
{
|
2023-01-25 22:11:57 +00:00
|
|
|
|
//var lastToken = await SecureStorage.Default.GetAsync(jwt_keychain_key);
|
2023-01-28 14:41:03 +00:00
|
|
|
|
var lastToken = Preferences.Default.Get(Constants.JwtPrefKey, string.Empty);
|
2023-01-22 22:15:55 +00:00
|
|
|
|
|
|
|
|
|
lastStoredKey = lastToken;
|
|
|
|
|
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(lastToken))
|
|
|
|
|
{
|
|
|
|
|
await Authenticate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<SelectorNetClient.TokenResponseStatus> Authenticate(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Making login token request");
|
|
|
|
|
|
|
|
|
|
var tokenResponse = await _selectorNetClient.GetToken(username, password);
|
|
|
|
|
|
|
|
|
|
return await HandleTokenResponse(tokenResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<SelectorNetClient.TokenResponseStatus> Authenticate()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("Making token request with current key");
|
|
|
|
|
|
|
|
|
|
var tokenResponse = await _selectorNetClient.GetToken(lastStoredKey);
|
|
|
|
|
|
|
|
|
|
return await HandleTokenResponse(tokenResponse);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 18:55:59 +00:00
|
|
|
|
private Task<SelectorNetClient.TokenResponseStatus> HandleTokenResponse(SelectorNetClient.TokenResponse tokenResponse)
|
2023-01-22 22:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
switch (tokenResponse.Status)
|
|
|
|
|
{
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.OK:
|
|
|
|
|
_logger.LogInformation("Token response ok");
|
|
|
|
|
lastStoredKey = tokenResponse.Token;
|
|
|
|
|
lastRefresh = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
//await SecureStorage.Default.SetAsync(jwt_keychain_key, lastStoredKey);
|
2023-01-25 22:11:57 +00:00
|
|
|
|
// I know, but I can't get secure storage to work
|
2023-01-28 14:41:03 +00:00
|
|
|
|
Preferences.Default.Set(Constants.JwtPrefKey, lastStoredKey);
|
2023-01-22 22:15:55 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.Malformed:
|
|
|
|
|
_logger.LogInformation("Token request failed, missing username or password");
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.UserSearchFailed:
|
|
|
|
|
_logger.LogInformation("Token request failed, no user by that name");
|
|
|
|
|
|
2024-02-23 08:38:19 +00:00
|
|
|
|
break;
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.ExpiredCreds:
|
|
|
|
|
_logger.LogInformation("Token expired, log back in");
|
|
|
|
|
lastStoredKey = null;
|
|
|
|
|
lastRefresh = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
Preferences.Default.Remove(Constants.JwtPrefKey);
|
|
|
|
|
|
2023-01-22 22:15:55 +00:00
|
|
|
|
break;
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.BadCreds:
|
|
|
|
|
_logger.LogInformation("Token request failed, bad password");
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 18:55:59 +00:00
|
|
|
|
return Task.FromResult(tokenResponse.Status);
|
2023-01-22 22:15:55 +00:00
|
|
|
|
}
|
2023-01-25 22:11:57 +00:00
|
|
|
|
|
|
|
|
|
public void SignOut()
|
|
|
|
|
{
|
|
|
|
|
lastStoredKey = null;
|
|
|
|
|
Preferences.Default.Clear();
|
|
|
|
|
}
|
2023-01-22 22:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|