chat: add hello controller
This commit is contained in:
parent
6f6e19bbb5
commit
7b4cbd4fb7
12 changed files with 114 additions and 53 deletions
|
@ -17,7 +17,7 @@ public class AppsController(ILogger logger, IdentityContext db) : ControllerBase
|
|||
public async Task<IActionResult> CreateApplication([FromBody] Apps.CreateRequest req)
|
||||
{
|
||||
var app = Application.Create(req.Name, req.Scopes, req.RedirectUris);
|
||||
await db.AddAsync(app);
|
||||
db.Add(app);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
logger.Information("Created new application {Name} with ID {Id} and client ID {ClientId}", app.Name, app.Id, app.ClientId);
|
||||
|
|
|
@ -21,10 +21,13 @@ public class PasswordAuthController(ILogger logger, IdentityContext db, IClock c
|
|||
public async Task<IActionResult> Register([FromBody] RegisterRequest req)
|
||||
{
|
||||
var app = HttpContext.GetApplicationOrThrow();
|
||||
var appToken = HttpContext.GetToken() ?? throw new UnreachableException(); // GetApplicationOrThrow already gets the token and throws if it's null
|
||||
var appToken =
|
||||
HttpContext.GetToken() ??
|
||||
throw new UnreachableException(); // GetApplicationOrThrow already gets the token and throws if it's null
|
||||
|
||||
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));
|
||||
throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token",
|
||||
req.Scopes.Except(appToken.Scopes));
|
||||
|
||||
var acct = new Account
|
||||
{
|
||||
|
@ -33,12 +36,12 @@ public class PasswordAuthController(ILogger logger, IdentityContext db, IClock c
|
|||
Role = Account.AccountRole.User
|
||||
};
|
||||
|
||||
await db.AddAsync(acct);
|
||||
db.Add(acct);
|
||||
var hashedPassword = await Task.Run(() => _passwordHasher.HashPassword(acct, req.Password));
|
||||
acct.Password = hashedPassword;
|
||||
// TODO: make token expiry configurable
|
||||
var (tokenStr, token) = Token.Create(acct, app, req.Scopes, clock.GetCurrentInstant() + Duration.FromDays(365));
|
||||
await db.AddAsync(token);
|
||||
db.Add(token);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return Ok(new AuthResponse(acct.Id, acct.Username, acct.Email, tokenStr));
|
||||
|
@ -51,26 +54,28 @@ public class PasswordAuthController(ILogger logger, IdentityContext db, IClock c
|
|||
var appToken = HttpContext.GetToken() ?? throw new UnreachableException();
|
||||
|
||||
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));
|
||||
throw new ApiError.Forbidden("Cannot request token scopes that are not allowed for this token",
|
||||
req.Scopes.Except(appToken.Scopes));
|
||||
|
||||
var acct = await db.Accounts.FirstOrDefaultAsync(a => a.Email == req.Email)
|
||||
?? throw new ApiError.NotFound("No user with that email found, or password is incorrect");
|
||||
?? throw new ApiError.NotFound("No user with that email found, or password is incorrect");
|
||||
|
||||
var pwResult = await Task.Run(() => _passwordHasher.VerifyHashedPassword(acct, acct.Password, req.Password));
|
||||
if (pwResult == PasswordVerificationResult.Failed)
|
||||
throw new ApiError.NotFound("No user with that email found, or password is incorrect");
|
||||
if (pwResult == PasswordVerificationResult.SuccessRehashNeeded)
|
||||
acct.Password = await Task.Run(() => _passwordHasher.HashPassword(acct, req.Password));
|
||||
|
||||
|
||||
|
||||
var (tokenStr, token) = Token.Create(acct, app, req.Scopes, clock.GetCurrentInstant() + Duration.FromDays(365));
|
||||
await db.AddAsync(token);
|
||||
db.Add(token);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return Ok(new AuthResponse(acct.Id, acct.Username, acct.Email, tokenStr));
|
||||
}
|
||||
|
||||
public record RegisterRequest(string Username, string Password, string Email, string[] Scopes);
|
||||
|
||||
public record LoginRequest(string Email, string Password, string[] Scopes);
|
||||
|
||||
public record AuthResponse(Ulid Id, string Username, string Email, string Token);
|
||||
}
|
|
@ -41,7 +41,7 @@ public class TokenController(ILogger logger, IdentityContext db, IClock clock) :
|
|||
var expiry = clock.GetCurrentInstant() + Duration.FromDays(365);
|
||||
var (token, tokenObj) = Token.Create(null, app, scopes, expiry);
|
||||
|
||||
await db.AddAsync(tokenObj);
|
||||
db.Add(tokenObj);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
logger.Debug("Created token with scopes {Scopes} for application {ApplicationId}", scopes, app.Id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue