fix: align backend APIs and upload flow
This commit is contained in:
@@ -11,8 +11,8 @@ namespace IM.Jwt
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
string GetToken(IEnumerable<Claim> claims, JwtOptions options);
|
||||
Task<string> CreateRefreshTokenAsync(Guid userId, CancellationToken cancellationToken = default);
|
||||
Task<string> CreateRefreshTokenAsync(Guid userId, CancellationToken cancellationToken = default, string? stamp = null, int? days = null);
|
||||
Task RevokeRefreshTokenAsync(string refreshToken);
|
||||
Task<(bool ok, Guid userId)> ValidateRefreshTokenAsync(string token, CancellationToken cancellation = default);
|
||||
Task<(bool ok, Guid userId, string? stamp)> ValidateRefreshTokenAsync(string token, CancellationToken cancellation = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ namespace IM.Jwt
|
||||
var bytes = RandomNumberGenerator.GetBytes(32);
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
public async Task<string> CreateRefreshTokenAsync(Guid userId, CancellationToken cancellationToken = default)
|
||||
public async Task<string> CreateRefreshTokenAsync(Guid userId, CancellationToken cancellationToken = default, string? stamp = null, int? days = null)
|
||||
{
|
||||
string token = GenerateTokenStr();
|
||||
var payload = new { UserId = userId, CreateAt = DateTime.Now };
|
||||
var payload = new { UserId = userId, CreateAt = DateTime.UtcNow, Stamp = stamp };
|
||||
string json = JsonConvert.SerializeObject(payload);
|
||||
//token写入redis
|
||||
await _redis.StringSetAsync(RedisHelper.GetRefreshTokenKey(token), json, TimeSpan.FromDays(_options.Value.RefreshTokenDays));
|
||||
await _redis.StringSetAsync(RedisHelper.GetRefreshTokenKey(token), json, TimeSpan.FromDays(days is > 0 ? days.Value : _options.Value.RefreshTokenDays));
|
||||
return token;
|
||||
}
|
||||
|
||||
@@ -51,19 +51,19 @@ namespace IM.Jwt
|
||||
await _redis.KeyDeleteAsync(RedisHelper.GetRefreshTokenKey(refreshToken));
|
||||
}
|
||||
|
||||
public async Task<(bool ok, Guid userId)> ValidateRefreshTokenAsync(string token, CancellationToken cancellation = default)
|
||||
public async Task<(bool ok, Guid userId, string? stamp)> ValidateRefreshTokenAsync(string token, CancellationToken cancellation = default)
|
||||
{
|
||||
var json = await _redis.StringGetAsync(RedisHelper.GetRefreshTokenKey(token));
|
||||
if (json.IsNullOrEmpty) return (false, Guid.Empty);
|
||||
if (json.IsNullOrEmpty) return (false, Guid.Empty, null);
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(json.ToString());
|
||||
var userId = doc.RootElement.GetProperty("UserId").GetGuid();
|
||||
return (true, userId);
|
||||
return (true, userId, doc.RootElement.TryGetProperty("Stamp", out var stamp) ? stamp.GetString() : null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (false, Guid.Empty);
|
||||
return (false, Guid.Empty, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace IM.Jwt
|
||||
context.HandleResponse();
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.StatusCode = StatusCodes.Status200OK;
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
|
||||
var result = Result<object>.Fail(ResultCode.AUTH_FAILED);
|
||||
await context.Response.WriteAsJsonAsync(result);
|
||||
@@ -71,7 +71,7 @@ namespace IM.Jwt
|
||||
{
|
||||
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.StatusCode = StatusCodes.Status200OK;
|
||||
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||||
var result = Result<object>.Fail(ResultCode.PERMISSION_DENIED);
|
||||
await context.Response.WriteAsJsonAsync(result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user