更新框架
This commit is contained in:
1 parent
ef50fa0620
commit
84b28f9672
72 files changed
+2222
-282
No files matched your search
@@ -0,0 +1,67 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using RestSharp;
|
||||
using Store.Management.Api.Transport;
|
||||
|
||||
namespace Store.Management.Api.Authentication;
|
||||
|
||||
public sealed class LoginRequest
|
||||
{
|
||||
[Required] public string UserName { get; set; } = "";
|
||||
[Required] public string Password { get; set; } = "";
|
||||
[StringLength(100)] public string? DeviceId { get; set; }
|
||||
[StringLength(100)] public string? DeviceName { get; set; }
|
||||
}
|
||||
|
||||
public sealed class LoginResult
|
||||
{
|
||||
public string TokenType { get; init; } = "Bearer";
|
||||
public string Token { get; init; } = "";
|
||||
public uint ExpiresInMinutes { get; init; }
|
||||
}
|
||||
|
||||
public sealed record CurrentUser(string UserName, string? NickName);
|
||||
|
||||
public interface IAuthApi
|
||||
{
|
||||
Task<LoginResult> LoginAsync(LoginRequest request, CancellationToken cancellationToken = default);
|
||||
Task LogoutAsync(CancellationToken cancellationToken = default);
|
||||
Task<CurrentUser> MeAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
internal sealed class AuthApi(ApiTransport transport, TokenSession session) : IAuthApi
|
||||
{
|
||||
// 登录和退出串行执行,避免迟到的登录响应覆盖退出操作。
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
|
||||
public async Task<LoginResult> LoginAsync(LoginRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var result = await transport.GetRequiredAsync<LoginResult>("Auth/Login", Method.Post, request,
|
||||
authenticated: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (string.IsNullOrWhiteSpace(result.Token) || result.Token.Contains('\r') || result.Token.Contains('\n') ||
|
||||
!string.Equals(result.TokenType, "Bearer", StringComparison.OrdinalIgnoreCase))
|
||||
throw new ApiException(ApiErrorKind.InvalidResponse, "登录响应缺少有效的 Bearer Token。");
|
||||
session.Set(result.Token);
|
||||
return result;
|
||||
}
|
||||
finally { _gate.Release(); }
|
||||
}
|
||||
|
||||
public async Task LogoutAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
var token = session.Token;
|
||||
try
|
||||
{
|
||||
if (token is not null)
|
||||
await transport.SendAsync<object>("Auth/Logout", Method.Post, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally { session.ClearIfCurrent(token); _gate.Release(); }
|
||||
}
|
||||
|
||||
public Task<CurrentUser> MeAsync(CancellationToken cancellationToken = default)
|
||||
=> transport.GetRequiredAsync<CurrentUser>("Auth/Me", Method.Get, cancellationToken: cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Store.Management.Api.Authentication;
|
||||
|
||||
/// <summary>会话仅保存在内存中,不将密码或 Token 写入配置与日志。</summary>
|
||||
public sealed class TokenSession
|
||||
{
|
||||
private string? _token;
|
||||
public bool IsAuthenticated => Volatile.Read(ref _token) is not null;
|
||||
internal string? Token => Volatile.Read(ref _token);
|
||||
internal void Set(string token) => Interlocked.Exchange(ref _token, token);
|
||||
public void Clear() => Interlocked.Exchange(ref _token, null);
|
||||
// 旧请求的 401 不应清除重新登录后建立的新会话。
|
||||
internal void ClearIfCurrent(string? token)
|
||||
{
|
||||
if (token is not null) Interlocked.CompareExchange(ref _token, null, token);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user