summaryrefslogtreecommitdiff
path: root/Controllers
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-06-19 00:34:17 +1000
committerJake Mannens <jake@asger.xyz>2026-08-21 01:05:38 +1000
commitb9b0f08222287e8c24ab16b3fc610e04213942f1 (patch)
treed97d825c880b6bec25b6488891d69bee0482ff6f /Controllers
parentbedb356c07b2a62ce676503f0fcc5a477c2bdbf4 (diff)
Added basic infrastructure for JWT authentication
Diffstat (limited to 'Controllers')
-rw-r--r--Controllers/ApiLoginController.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/Controllers/ApiLoginController.cs b/Controllers/ApiLoginController.cs
new file mode 100644
index 0000000..fdf9bae
--- /dev/null
+++ b/Controllers/ApiLoginController.cs
@@ -0,0 +1,46 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.IdentityModel.Tokens;
+using System.IdentityModel.Tokens.Jwt;
+using System.Security.Claims;
+using System.Security.Cryptography;
+
+namespace HyperBooru.Controllers;
+
+[ApiController]
+[Route("/api/auth")]
+public class ApiLoginController : Controller {
+ private readonly RSA rsa;
+
+ public ApiLoginController(RSA rsa) =>
+ this.rsa = rsa;
+
+ [HttpPost]
+ public IActionResult Login([FromBody] LoginRequest request) {
+ var claims = new[] {
+ new Claim(ClaimTypes.Name, request.Username),
+ // TODO: Populate with the user's actual GUID
+ new Claim("uid", Guid.Empty.ToString().ToLower()),
+ new Claim("nsfw", request.NsfwClaim.ToString().ToLower())
+ };
+
+ var creds = new SigningCredentials(
+ new RsaSecurityKey(rsa),
+ SecurityAlgorithms.RsaSha256);
+
+ var token = new JwtSecurityToken(
+ claims: claims,
+ expires: DateTime.UtcNow.AddDays(30),
+ signingCredentials: creds);
+
+ var jwt = new JwtSecurityTokenHandler().WriteToken(token);
+
+ return Ok(new { token = jwt });
+ }
+
+ public record LoginRequest {
+ public required string Username { get; set; }
+ public required string Password { get; set; }
+
+ public bool NsfwClaim { get; set; } = false;
+ }
+}