Selector/Selector.MAUI/Services/SessionManager.cs
2023-01-25 22:11:57 +00:00

94 lines
2.9 KiB
C#

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()
{
//var lastToken = await SecureStorage.Default.GetAsync(jwt_keychain_key);
var lastToken = Preferences.Default.Get(jwt_keychain_key, string.Empty);
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);
// I know, but I can't get secure storage to work
Preferences.Default.Set(jwt_keychain_key, lastStoredKey);
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;
}
public void SignOut()
{
lastStoredKey = null;
Preferences.Default.Clear();
}
}