- 新增 IApiResponse 统一响应契约,覆盖普通响应和分页响应
- 扩展端点映射,支持 IApiResponse 和带请求 DTO 的 MapXxx 重载
- 增加 Body、Query、Route Values 到请求 DTO 的自动绑定
- 增加 PC 端路由模式匹配,支持 {id} 和 {id:int}
- API 与 PC 端点上下文补充路由参数传递
- 调整 OpenAPI 请求类型处理,避免 GET/DELETE 被标记为 JSON Body
- 鉴权端点迁移到强类型请求绑定和 IApiResponse 返回
- 增加统一端点文件流响应支持
73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using Authentication;
|
||
using Avalonia_Common.Core;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Services.AuthService;
|
||
using System;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Avalonia_PC.Authentication
|
||
{
|
||
/// <summary>
|
||
/// PC 端鉴权端点服务,实现 <see cref="IPcAuthEndpointService"/>,
|
||
/// 处理授权码登录、Token 刷新和登出操作。
|
||
/// </summary>
|
||
public sealed class PcAuthEndpointService(PcGlobalTokenService tokenService) : IPcAuthEndpointService
|
||
{
|
||
/// <inheritdoc />
|
||
public async Task<IApiResponse> AuthorizeAsync(PcAuthorizeRequest request, ServiceEndpointContext ctx)
|
||
{
|
||
var token = await tokenService.AuthorizeAsync(request.AuthorizationCode);
|
||
if (token is null)
|
||
{
|
||
ctx.StatusCode = 401;
|
||
return ResponseHelper.Failure(401, "授权失败");
|
||
}
|
||
|
||
return ResponseHelper.Ok(token, "授权成功");
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public async Task<IApiResponse> RefreshAsync(PcRefreshRequest request, ServiceEndpointContext ctx)
|
||
{
|
||
var token = request.Token ?? ExtractBearerToken(ctx.GetHeader("Authorization"));
|
||
var refreshed = await tokenService.RefreshAsync(token);
|
||
if (refreshed is null)
|
||
{
|
||
ctx.StatusCode = 401;
|
||
return ResponseHelper.Failure(401, "授权已失效");
|
||
}
|
||
|
||
return ResponseHelper.Ok(refreshed, "刷新成功");
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public Task<IApiResponse> LogoutAsync(PcLogoutRequest request, ServiceEndpointContext ctx)
|
||
{
|
||
var token = request.Token ?? ExtractBearerToken(ctx.GetHeader("Authorization"));
|
||
tokenService.Logout(token);
|
||
return Task.FromResult<IApiResponse>(ResponseHelper.Succeed("退出成功"));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从 Authorization 头中提取 Bearer Token。
|
||
/// </summary>
|
||
/// <param name="authorization">Authorization 头的值。</param>
|
||
/// <returns>提取的 Token 字符串;若无法提取则返回 null。</returns>
|
||
private static string? ExtractBearerToken(string? authorization)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(authorization))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Bearer Token 的前缀常量。
|
||
/// </summary>
|
||
const string prefix = "Bearer ";
|
||
return authorization.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
|
||
? authorization[prefix.Length..].Trim()
|
||
: authorization.Trim();
|
||
}
|
||
}
|
||
}
|