mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-13 13:22:28 +00:00
Refactor deserialization and authentication (#550)
* Refactor deserialization and more * Refactor authentication flow * Fix unit tests * Make deserialization better
This commit is contained in:
parent
505b5eb03b
commit
b3a00da554
48 changed files with 575 additions and 589 deletions
|
@ -1,19 +1,20 @@
|
|||
#nullable enable
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Extensions;
|
||||
using LBPUnion.ProjectLighthouse.Levels;
|
||||
using LBPUnion.ProjectLighthouse.Levels.Categories;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class CollectionController : ControllerBase
|
||||
|
@ -28,9 +29,6 @@ public class CollectionController : ControllerBase
|
|||
[HttpGet("playlists/{playlistId:int}/slots")]
|
||||
public async Task<IActionResult> GetPlaylistSlots(int playlistId)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
|
||||
if (targetPlaylist == null) return this.BadRequest();
|
||||
|
||||
|
@ -50,8 +48,7 @@ public class CollectionController : ControllerBase
|
|||
[HttpPost("playlists/{playlistId:int}/order_slots")]
|
||||
public async Task<IActionResult> UpdatePlaylist(int playlistId, int slotId)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
GameToken token = this.GetToken();
|
||||
|
||||
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
|
||||
if (targetPlaylist == null) return this.BadRequest();
|
||||
|
@ -66,7 +63,7 @@ public class CollectionController : ControllerBase
|
|||
return this.Ok(this.GetUserPlaylists(token.UserId));
|
||||
}
|
||||
|
||||
Playlist? newPlaylist = await this.getPlaylistFromBody();
|
||||
Playlist? newPlaylist = await this.DeserializeBody<Playlist>("playlist", "levels");
|
||||
|
||||
if (newPlaylist == null) return this.BadRequest();
|
||||
|
||||
|
@ -116,14 +113,13 @@ public class CollectionController : ControllerBase
|
|||
[HttpPost("playlists")]
|
||||
public async Task<IActionResult> CreatePlaylist()
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
GameToken token = this.GetToken();
|
||||
|
||||
int playlistCount = await this.database.Playlists.CountAsync(p => p.CreatorId == token.UserId);
|
||||
|
||||
if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest();
|
||||
|
||||
Playlist? playlist = await this.getPlaylistFromBody();
|
||||
Playlist? playlist = await this.DeserializeBody<Playlist>("playlist");
|
||||
|
||||
if (playlist == null) return this.BadRequest();
|
||||
|
||||
|
@ -139,10 +135,7 @@ public class CollectionController : ControllerBase
|
|||
[HttpGet("user/{username}/playlists")]
|
||||
public async Task<IActionResult> GetUserPlaylists(string username)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
int targetUserId = await this.database.Users.Where(u => u.Username == username).Select(u => u.UserId).FirstOrDefaultAsync();
|
||||
int targetUserId = await this.database.UserIdFromUsername(username);
|
||||
if (targetUserId == 0) return this.BadRequest();
|
||||
|
||||
return this.Ok(this.GetUserPlaylists(targetUserId));
|
||||
|
@ -152,8 +145,9 @@ public class CollectionController : ControllerBase
|
|||
[HttpGet("genres")]
|
||||
public async Task<IActionResult> GenresAndSearches()
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
GameToken token = this.GetToken();
|
||||
|
||||
User? user = await this.database.UserFromGameToken(token);
|
||||
|
||||
string categoriesSerialized = CategoryHelper.Categories.Aggregate
|
||||
(
|
||||
|
@ -196,13 +190,9 @@ public class CollectionController : ControllerBase
|
|||
[HttpGet("searches/{endpointName}")]
|
||||
public async Task<IActionResult> GetCategorySlots(string endpointName, [FromQuery] int pageStart, [FromQuery] int pageSize)
|
||||
{
|
||||
(User, GameToken)? userAndToken = await this.database.UserAndGameTokenFromRequest(this.Request);
|
||||
GameToken token = this.GetToken();
|
||||
|
||||
if (userAndToken == null) return this.StatusCode(403, "");
|
||||
|
||||
// ReSharper disable once PossibleInvalidOperationException
|
||||
User user = userAndToken.Value.Item1;
|
||||
GameToken gameToken = userAndToken.Value.Item2;
|
||||
User? user = await this.database.UserFromGameToken(token);
|
||||
|
||||
Category? category = CategoryHelper.Categories.FirstOrDefault(c => c.Endpoint == endpointName);
|
||||
if (category == null) return this.NotFound();
|
||||
|
@ -223,7 +213,7 @@ public class CollectionController : ControllerBase
|
|||
totalSlots = category.GetTotalSlots(this.database);
|
||||
}
|
||||
|
||||
string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameToken.GameVersion));
|
||||
string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(token.GameVersion));
|
||||
|
||||
return this.Ok
|
||||
(
|
||||
|
@ -243,19 +233,4 @@ public class CollectionController : ControllerBase
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
private async Task<Playlist?> getPlaylistFromBody()
|
||||
{
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
string rootElement = bodyString.StartsWith("<playlist>") ? "playlist" : "levels";
|
||||
XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute(rootElement));
|
||||
Playlist? playlist = (Playlist?)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
SanitizationHelper.SanitizeStringsInClass(playlist);
|
||||
|
||||
return playlist;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue