Library refreshing uses token authentication

dashboard
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
{
private readonly RestClient Client;
private AuthToken Token;
public AuthToken Token;
public LANCommanderClient()
{
@ -30,6 +30,16 @@ namespace LANCommander.Playnite.Extension
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)
{
var response = Client.Post<AuthResponse>(new RestRequest("/api/Auth").AddJsonBody(new AuthRequest()
@ -74,9 +84,7 @@ namespace LANCommander.Playnite.Extension
public IEnumerable<Game> GetGames()
{
var response = Client.Get<IEnumerable<Game>>(new RestRequest("/api/Games"));
return response.Data;
return GetRequest<IEnumerable<Game>>("/api/Games");
}
}
}

View File

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

View File

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

View File

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