blob: fdf9baedd8ca68d9d78b3ab4f55b51f1d55d31b4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
}
}
|