diff --git a/LANCommander.Playnite.Extension/LANCommanderClient.cs b/LANCommander.Playnite.Extension/LANCommanderClient.cs index 344bbeb..8ccd7b0 100644 --- a/LANCommander.Playnite.Extension/LANCommanderClient.cs +++ b/LANCommander.Playnite.Extension/LANCommanderClient.cs @@ -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(string route) + { + var request = new RestRequest(route) + .AddHeader("Authorization", $"Bearer {Token.AccessToken}"); + + var response = Client.Get(request); + + return response.Data; + } + public AuthResponse Authenticate(string username, string password) { var response = Client.Post(new RestRequest("/api/Auth").AddJsonBody(new AuthRequest() @@ -74,9 +84,7 @@ namespace LANCommander.Playnite.Extension public IEnumerable GetGames() { - var response = Client.Get>(new RestRequest("/api/Games")); - - return response.Data; + return GetRequest>("/api/Games"); } } } diff --git a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs index 084e32c..29743de 100644 --- a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs @@ -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; } } } diff --git a/LANCommander.Playnite.Extension/PlayniteSettings.cs b/LANCommander.Playnite.Extension/PlayniteSettings.cs index cf22d2f..65b0449 100644 --- a/LANCommander.Playnite.Extension/PlayniteSettings.cs +++ b/LANCommander.Playnite.Extension/PlayniteSettings.cs @@ -30,6 +30,8 @@ namespace LANCommander.Playnite.Extension if (savedSettings != null) { ServerUrl = savedSettings.ServerUrl; + AccessToken = savedSettings.AccessToken; + RefreshToken = savedSettings.RefreshToken; } } diff --git a/LANCommander/Controllers/Api/AuthController.cs b/LANCommander/Controllers/Api/AuthController.cs index 2f6ce58..230f0c6 100644 --- a/LANCommander/Controllers/Api/AuthController.cs +++ b/LANCommander/Controllers/Api/AuthController.cs @@ -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)