增加测试
This commit is contained in:
1 parent
6d4629e4ac
commit
497e9f1c34
14 files changed
+700
-74
No files matched your search
@@ -7,6 +7,7 @@ namespace Store.Management.Api.Authentication;
|
||||
public sealed class LoginRequest
|
||||
{
|
||||
[Required] public string UserName { get; set; } = "";
|
||||
/// <summary>服务端要求的 UTF-8 密码的 32 位大写 MD5,由调用方计算。</summary>
|
||||
[Required] public string Password { get; set; } = "";
|
||||
[StringLength(100)] public string? DeviceId { get; set; }
|
||||
[StringLength(100)] public string? DeviceName { get; set; }
|
||||
@@ -39,27 +40,38 @@ internal sealed class AuthApi(ApiTransport transport, TokenSession session) : IA
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
var result = await transport.GetRequiredAsync<LoginResult>("Auth/Login", Method.Post, request,
|
||||
LoginResult 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(); }
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task LogoutAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
var token = session.Token;
|
||||
string? 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();
|
||||
}
|
||||
finally { session.ClearIfCurrent(token); _gate.Release(); }
|
||||
}
|
||||
|
||||
public Task<CurrentUser> MeAsync(CancellationToken cancellationToken = default)
|
||||
|
||||
Reference in new issue
Block a user