Library refreshing uses token authentication

This commit is contained in:
Pat Hartl 2023-01-06 02:39:00 -06:00
parent 98bf631a9a
commit bf4f66ccb3
4 changed files with 55 additions and 14 deletions

View file

@ -12,7 +12,7 @@ namespace LANCommander.Playnite.Extension
internal class LANCommanderClient internal class LANCommanderClient
{ {
private readonly RestClient Client; private readonly RestClient Client;
private AuthToken Token; public AuthToken Token;
public LANCommanderClient() public LANCommanderClient()
{ {
@ -30,6 +30,16 @@ namespace LANCommander.Playnite.Extension
return response.Data; return response.Data;
} }
private T GetRequest<T>(string route)
{
var request = new RestRequest(route)
.AddHeader("Authorization", $"Bearer {Token.AccessToken}");
var response = Client.Get<T>(request);
return response.Data;
}
public AuthResponse Authenticate(string username, string password) public AuthResponse Authenticate(string username, string password)
{ {
var response = Client.Post<AuthResponse>(new RestRequest("/api/Auth").AddJsonBody(new AuthRequest() var response = Client.Post<AuthResponse>(new RestRequest("/api/Auth").AddJsonBody(new AuthRequest()
@ -74,9 +84,7 @@ namespace LANCommander.Playnite.Extension
public IEnumerable<Game> GetGames() public IEnumerable<Game> GetGames()
{ {
var response = Client.Get<IEnumerable<Game>>(new RestRequest("/api/Games")); return GetRequest<IEnumerable<Game>>("/api/Games");
return response.Data;
} }
} }
} }

View file

@ -34,16 +34,44 @@ namespace LANCommander.Playnite.Extension
{ {
try try
{ {
// Implement LANCommander client here var token = new SDK.Models.AuthToken()
var games = LANCommander.GetGames().Select(g => new GameMetadata()
{ {
Name = g.Title, AccessToken = Settings.AccessToken,
Description = g.Description, RefreshToken = Settings.RefreshToken,
GameId = g.Id.ToString(), };
ReleaseDate = new ReleaseDate(g.ReleasedOn),
SortingName = g.SortTitle, var tokenIsValid = LANCommander.ValidateToken(token);
Version = g.Archives != null && g.Archives.Count() > 0 ? g.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version : null,
}); if (!tokenIsValid)
{
try
{
LANCommander.RefreshToken(token);
}
catch
{
ShowAuthenticationWindow();
}
}
LANCommander.Token = token;
var games = LANCommander
.GetGames()
.Where(g => g.Archives != null && g.Archives.Count() > 0)
.Select(g =>
{
return new GameMetadata()
{
IsInstalled = false,
Name = g.Title,
SortingName = g.SortTitle,
Description = g.Description,
GameId = g.Id.ToString(),
ReleaseDate = new ReleaseDate(g.ReleasedOn),
Version = g.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version,
};
});
return games; return games;
} }
@ -63,7 +91,7 @@ namespace LANCommander.Playnite.Extension
return new PlayniteSettingsView(this); return new PlayniteSettingsView(this);
} }
public void ShowAuthenticationWindow() public System.Windows.Window ShowAuthenticationWindow()
{ {
var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions() var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions()
{ {
@ -78,6 +106,8 @@ namespace LANCommander.Playnite.Extension
window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow(); window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow();
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
window.ShowDialog(); window.ShowDialog();
return window;
} }
} }
} }

View file

@ -30,6 +30,8 @@ namespace LANCommander.Playnite.Extension
if (savedSettings != null) if (savedSettings != null)
{ {
ServerUrl = savedSettings.ServerUrl; ServerUrl = savedSettings.ServerUrl;
AccessToken = savedSettings.AccessToken;
RefreshToken = savedSettings.RefreshToken;
} }
} }

View file

@ -79,6 +79,7 @@ namespace LANCommander.Controllers.Api
} }
[HttpPost("Validate")] [HttpPost("Validate")]
[Authorize(AuthenticationSchemes = "Bearer")]
public IActionResult Validate() public IActionResult Validate()
{ {
if (User != null && User.Identity != null && User.Identity.IsAuthenticated) if (User != null && User.Identity != null && User.Identity.IsAuthenticated)