summaryrefslogtreecommitdiff
path: root/Controllers
diff options
context:
space:
mode:
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;
+ }
+}