Library refreshing uses token authentication
parent
98bf631a9a
commit
bf4f66ccb3
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@ namespace LANCommander.Playnite.Extension
|
|||
if (savedSettings != null)
|
||||
{
|
||||
ServerUrl = savedSettings.ServerUrl;
|
||||
AccessToken = savedSettings.AccessToken;
|
||||
RefreshToken = savedSettings.RefreshToken;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue