2023-01-22 22:15:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Selector.MAUI.Services;
|
|
|
|
|
|
|
|
|
|
public class SessionManager
|
|
|
|
|
{
|
|
|
|
|
private const string jwt_keychain_key = "last_jwt_key";
|
|
|
|
|
|
|
|
|
|
private string lastStoredKey;
|
|
|
|
|
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);
|
|
|
|
|
var lastToken = Preferences.Default.Get(jwt_keychain_key, 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<SelectorNetClient.TokenResponseStatus> HandleTokenResponse(SelectorNetClient.TokenResponse tokenResponse)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
Preferences.Default.Set(jwt_keychain_key, 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");
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case SelectorNetClient.TokenResponseStatus.BadCreds:
|
|
|
|
|
_logger.LogInformation("Token request failed, bad password");
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tokenResponse.Status;
|
|
|
|
|
}
|
2023-01-25 22:11:57 +00:00
|
|
|
|
|
|
|
|
|
public void SignOut()
|
|
|
|
|
{
|
|
|
|
|
lastStoredKey = null;
|
|
|
|
|
Preferences.Default.Clear();
|
|
|
|
|
}
|
2023-01-22 22:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|