using Foxchat.Core; using Foxchat.Core.Models.Http; using Foxchat.Identity.Authorization; using Foxchat.Identity.Database; using Foxchat.Identity.Database.Models; using Microsoft.AspNetCore.Mvc; namespace Foxchat.Identity.Controllers.Oauth; [ApiController] [Authenticate] [Route("/_fox/ident/oauth/apps")] public class AppsController(ILogger logger, IdentityContext db) : ControllerBase { [HttpPost] public async Task CreateApplication([FromBody] Apps.CreateRequest req) { var app = Application.Create(req.Name, req.Scopes, req.RedirectUris); await db.AddAsync(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 Apps.CreateResponse( app.Id, app.ClientId, app.ClientSecret, app.Name, app.Scopes, app.RedirectUris )); } [HttpGet] public IActionResult GetSelfApp([FromQuery(Name = "with_secret")] bool withSecret) { var token = HttpContext.GetToken(); if (token is not { Account: null }) throw new ApiError.Forbidden("This endpoint requires a client token."); var app = token.Application; return Ok(new Apps.GetSelfResponse( app.Id, app.ClientId, withSecret ? app.ClientSecret : null, app.Name, app.Scopes, app.RedirectUris )); } }