namespace Fengling.Console.Controllers; /// /// OAuth客户端管理控制器 /// [ApiController] [Route("api/console/[controller]")] [Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)] public class OAuthClientsController : ControllerBase { private readonly IOAuthClientService _service; private readonly ILogger _logger; public OAuthClientsController( IOAuthClientService service, ILogger logger) { _service = service; _logger = logger; } /// /// 获取OAuth客户端列表 /// /// 分页查询参数,支持按显示名称、客户端ID和状态筛选 /// 分页的OAuth客户端列表,包含总数量、分页信息和客户端详情 /// 成功返回OAuth客户端分页列表 /// 服务器内部错误 [HttpGet] [Produces("application/json")] [ProducesResponseType(typeof(OAuthClientListDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> GetClients([FromQuery] OAuthClientQueryDto query) { try { var (items, totalCount) = await _service.GetClientsAsync(query.Page, query.PageSize, query.DisplayName, query.ClientId, query.Status); var result = new OAuthClientListDto { Items = items.ToList(), TotalCount = totalCount, Page = query.Page, PageSize = query.PageSize }; return Ok(result); } catch (Exception ex) { _logger.LogError(ex, "Error getting clients"); return StatusCode(500, new { message = ex.Message }); } } /// /// 获取OAuth客户端选项 /// /// 包含客户端类型、授权类型、授权范围等可选值的配置选项 /// 成功返回客户端配置选项 [HttpGet("options")] [Produces("application/json")] [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] public ActionResult GetClientOptions() { return Ok(_service.GetClientOptions()); } /// /// 获取单个OAuth客户端详情 /// /// 客户端唯一标识符 /// OAuth客户端的完整配置信息 /// 成功返回客户端详情 /// 客户端不存在 /// 服务器内部错误 [HttpGet("{id}")] [Produces("application/json")] [ProducesResponseType(typeof(OAuthClientDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> GetClient(string id) { try { var client = await _service.GetClientAsync(id); if (client == null) { return NotFound(); } return Ok(client); } catch (Exception ex) { _logger.LogError(ex, "Error getting client {Id}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 创建新的OAuth客户端 /// /// 创建客户端所需的配置信息 /// 创建的OAuth客户端详情 /// 成功创建客户端 /// 请求参数无效或客户端ID已存在 /// 服务器内部错误 [HttpPost] [Produces("application/json")] [ProducesResponseType(typeof(OAuthClientDto), StatusCodes.Status201Created)] [ProducesResponseType(typeof(object), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> CreateClient([FromBody] CreateClientDto dto) { try { var result = await _service.CreateClientAsync(dto); return StatusCode(201, result); } catch (InvalidOperationException ex) { return BadRequest(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error creating client"); return StatusCode(500, new { message = ex.Message }); } } /// /// 为指定客户端生成新的密钥 /// /// 客户端唯一标识符 /// 包含新生成的客户端密钥信息 /// 成功生成新密钥 /// 客户端不存在 /// 服务器内部错误 [HttpPost("{id}/generate-secret")] [Produces("application/json")] [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task GenerateSecret(string id) { try { var result = await _service.GenerateSecretAsync(id); return Ok(result); } catch (KeyNotFoundException ex) { return NotFound(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error generating secret for client {Id}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 删除指定的OAuth客户端 /// /// 客户端唯一标识符 /// 无内容响应 /// 成功删除客户端 /// 客户端不存在 /// 服务器内部错误 [HttpDelete("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task DeleteClient(string id) { try { await _service.DeleteClientAsync(id); return NoContent(); } catch (KeyNotFoundException ex) { return NotFound(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error deleting client {Id}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 更新指定的OAuth客户端 /// /// 客户端唯一标识符 /// 需要更新的客户端配置信息 /// 无内容响应 /// 成功更新客户端 /// 客户端不存在 /// 服务器内部错误 [HttpPut("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task UpdateClient(string id, [FromBody] UpdateClientDto dto) { try { await _service.UpdateClientAsync(id, dto); return NoContent(); } catch (KeyNotFoundException ex) { return NotFound(new { message = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error updating client {Id}", id); return StatusCode(500, new { message = ex.Message }); } } }