ProjectLighthouse/ProjectLighthouse.Servers.Website/Controllers/ExternalAuth/AuthenticationController.cs
Josh 19ea44e0e2
Rework login and registration systems (#600)
* Initial work for verifying login ticket signatures

* Add candidate psn public key

* Add candidate psn public key and fix nuget packages

* Finalize npticket changes

* Add support for ticket version 3.0

* Rework login system to link platform accounts instead of using ip addresses

* Make linked accounts green instead of blue

* Fix api building

* Fix unit tests

* Actually fix unit tests

* Set unit test user's linked platform

* Why was this the wrong default value?

* Fix username change code

* Make TicketHash hash the entire ticket instead of just the serial

* Send password setup email when user sets their email for the first time

* Changes from self review
2022-12-26 01:03:14 -08:00

93 lines
No EOL
2.8 KiB
C#

#nullable enable
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers.ExternalAuth;
[ApiController]
[Route("/authentication")]
public class AuthenticationController : ControllerBase
{
private readonly Database database;
public AuthenticationController(Database database)
{
this.database = database;
}
[HttpGet("unlink/{platform}")]
public async Task<IActionResult> UnlinkPlatform(string platform)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
Platform[] invalidTokens;
if (platform == "psn")
{
user.LinkedPsnId = 0;
invalidTokens = new[] { Platform.PS3, Platform.Vita, };
}
else
{
user.LinkedRpcnId = 0;
invalidTokens = new[] { Platform.RPCS3, };
}
this.database.GameTokens.RemoveWhere(t => t.UserId == user.UserId && invalidTokens.Contains(t.Platform));
await this.database.SaveChangesAsync();
return this.Redirect("~/authentication");
}
[HttpGet("approve/{id:int}")]
public async Task<IActionResult> Approve(int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("/login");
PlatformLinkAttempt? linkAttempt = await this.database.PlatformLinkAttempts
.FirstOrDefaultAsync(l => l.PlatformLinkAttemptId == id);
if (linkAttempt == null) return this.NotFound();
if (linkAttempt.UserId != user.UserId) return this.NotFound();
if (linkAttempt.Platform == Platform.RPCS3)
{
user.LinkedRpcnId = linkAttempt.PlatformId;
}
else
{
user.LinkedPsnId = linkAttempt.PlatformId;
}
this.database.PlatformLinkAttempts.Remove(linkAttempt);
await this.database.SaveChangesAsync();
return this.Redirect("~/authentication");
}
[HttpGet("deny/{id:int}")]
public async Task<IActionResult> Deny(int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("/login");
PlatformLinkAttempt? linkAttempt = await this.database.PlatformLinkAttempts
.FirstOrDefaultAsync(l => l.PlatformLinkAttemptId == id);
if (linkAttempt == null) return this.NotFound();
if (linkAttempt.UserId != user.UserId) return this.NotFound();
this.database.PlatformLinkAttempts.Remove(linkAttempt);
await this.database.SaveChangesAsync();
return this.Redirect("~/authentication");
}
}