using Foxchat.Core; using Foxchat.Core.Models.Http; using Foxchat.Identity.Middleware; using Foxchat.Identity.Database; using Foxchat.Identity.Database.Models; using Foxchat.Identity.Utils; using Microsoft.AspNetCore.Mvc; namespace Foxchat.Identity.Controllers.Oauth; [ApiController] [ClientAuthenticate] [Route("/_fox/ident/oauth/apps")] public class AppsController(ILogger logger, IdentityContext db) : ControllerBase { [HttpPost] public async Task CreateApplication([FromBody] AppsApi.CreateRequest req) { var app = Application.Create(req.Name, req.Scopes, req.RedirectUris); 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); return Ok(new AppsApi.CreateResponse( app.Id, app.ClientId, app.ClientSecret, app.Name, app.Scopes, app.RedirectUris )); } [HttpGet] public IActionResult GetSelfApp([FromQuery(Name = "with_secret")] bool withSecret) { var app = HttpContext.GetApplicationOrThrow(); return Ok(new AppsApi.GetSelfResponse( app.Id, app.ClientId, withSecret ? app.ClientSecret : null, app.Name, app.Scopes, app.RedirectUris )); } }