add basic account creation
This commit is contained in:
parent
7a0247b551
commit
7959b64fe6
4 changed files with 44 additions and 5 deletions
|
@ -2,21 +2,47 @@ using Foxchat.Identity.Middleware;
|
||||||
using Foxchat.Identity.Database;
|
using Foxchat.Identity.Database;
|
||||||
using Foxchat.Identity.Utils;
|
using Foxchat.Identity.Utils;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Foxchat.Identity.Database.Models;
|
||||||
|
using Foxchat.Core;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using NodaTime;
|
||||||
|
|
||||||
namespace Foxchat.Identity.Controllers.Oauth;
|
namespace Foxchat.Identity.Controllers.Oauth;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Authenticate]
|
[Authenticate]
|
||||||
[Route("/_fox/ident/oauth/password")]
|
[Route("/_fox/ident/oauth/password")]
|
||||||
public class PasswordAuthController(ILogger logger, IdentityContext db) : ControllerBase
|
public class PasswordAuthController(ILogger logger, IdentityContext db, IClock clock) : ControllerBase
|
||||||
{
|
{
|
||||||
|
private readonly PasswordHasher<Account> _passwordHasher = new();
|
||||||
|
|
||||||
[HttpPost("register")]
|
[HttpPost("register")]
|
||||||
public async Task<IActionResult> Register()
|
public async Task<IActionResult> Register([FromBody] RegisterRequest req)
|
||||||
{
|
{
|
||||||
var app = HttpContext.GetApplicationOrThrow();
|
var app = HttpContext.GetApplicationOrThrow();
|
||||||
|
var appToken = HttpContext.GetToken() ?? throw new UnreachableException(); // GetApplicationOrThrow already gets the token and throws if it's null
|
||||||
|
|
||||||
throw new NotImplementedException();
|
if (req.Scopes.Except(appToken.Scopes).Any())
|
||||||
|
throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token", req.Scopes.Except(appToken.Scopes));
|
||||||
|
|
||||||
|
var acct = new Account
|
||||||
|
{
|
||||||
|
Username = req.Username,
|
||||||
|
Email = req.Email,
|
||||||
|
Role = Account.AccountRole.User
|
||||||
|
};
|
||||||
|
|
||||||
|
await db.AddAsync(acct);
|
||||||
|
var hashedPassword = await Task.Run(() => _passwordHasher.HashPassword(acct, req.Password));
|
||||||
|
acct.Password = hashedPassword;
|
||||||
|
var (tokenStr, token) = Token.Create(acct, app, req.Scopes, clock.GetCurrentInstant() + Duration.FromDays(365));
|
||||||
|
await db.AddAsync(token);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok(new RegisterResponse(acct.Id, acct.Username, acct.Email, tokenStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
public record RegisterRequest();
|
public record RegisterRequest(string Username, string Password, string Email, string[] Scopes);
|
||||||
|
public record RegisterResponse(Ulid Id, string Username, string Email, string Token);
|
||||||
}
|
}
|
|
@ -26,6 +26,7 @@ public class TokenController(ILogger logger, IdentityContext db, IClock clock) :
|
||||||
case "client_credentials":
|
case "client_credentials":
|
||||||
return await HandleClientCredentialsAsync(app, scopes);
|
return await HandleClientCredentialsAsync(app, scopes);
|
||||||
case "authorization_code":
|
case "authorization_code":
|
||||||
|
// TODO
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ApiError.BadRequest("Unknown grant_type");
|
throw new ApiError.BadRequest("Unknown grant_type");
|
||||||
|
|
|
@ -24,4 +24,17 @@ public class Token : BaseModel
|
||||||
|
|
||||||
return (token, hash);
|
return (token, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static (string, Token) Create(Account? account, Application application, string[] scopes, Instant expires)
|
||||||
|
{
|
||||||
|
var (token, hash) = Generate();
|
||||||
|
return (token, new()
|
||||||
|
{
|
||||||
|
Hash = hash,
|
||||||
|
Scopes = scopes,
|
||||||
|
Expires = expires,
|
||||||
|
Account = account,
|
||||||
|
Application = application,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Foxchat.Core;
|
using Foxchat.Core;
|
||||||
using Foxchat.Core.Models.Http;
|
using Foxchat.Core.Models.Http;
|
||||||
|
|
Loading…
Reference in a new issue