diff --git a/DragaliaAPI/Middleware/ExceptionHandlerMiddleware.cs b/DragaliaAPI/Middleware/ExceptionHandlerMiddleware.cs index 91e2af95c..80acecbd8 100644 --- a/DragaliaAPI/Middleware/ExceptionHandlerMiddleware.cs +++ b/DragaliaAPI/Middleware/ExceptionHandlerMiddleware.cs @@ -5,6 +5,7 @@ using DragaliaAPI.Models; using DragaliaAPI.Services.Exceptions; using MessagePack; +using Microsoft.CodeAnalysis.Elfie.Serialization; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Primitives; using Microsoft.IdentityModel.Tokens; @@ -51,11 +52,10 @@ IDistributedCache cache if (await cache.GetStringAsync(redisKey) is not null) { - logger.LogWarning("Detected repeated SecurityTokenExpiredException."); - throw new DragaliaException( - ResultCode.CommonAuthError, - "Detected repeated SecurityTokenExpiredException." - ); + logger.LogError("Detected repeated SecurityTokenExpiredException."); + await WriteResultCode(context, ResultCode.CommonAuthError); + + return; } logger.LogDebug("Issuing ID token refresh request."); @@ -63,7 +63,6 @@ IDistributedCache cache context.Response.StatusCode = 400; context.Response.Headers.Add("Is-Required-Refresh-Id-Token", "true"); - return; } catch (Exception ex) when (serializeException) { @@ -75,9 +74,6 @@ IDistributedCache cache return; } - context.Response.ContentType = CustomMessagePackOutputFormatter.ContentType; - context.Response.StatusCode = 200; - ResultCode code = ex is DragaliaException dragaliaException ? dragaliaException.Code : ResultCode.CommonServerError; @@ -88,11 +84,7 @@ IDistributedCache cache code ); - DragaliaResponse gameResponse = new(new DataHeaders(code), code); - - await context.Response.Body.WriteAsync( - MessagePackSerializer.Serialize(gameResponse, CustomResolver.Options) - ); + await WriteResultCode(context, code); } } @@ -100,4 +92,15 @@ await context.Response.Body.WriteAsync( { return deviceId is null ? null : $"refresh_sent:{deviceId}"; } + + private async Task WriteResultCode(HttpContext context, ResultCode code) + { + context.Response.ContentType = CustomMessagePackOutputFormatter.ContentType; + context.Response.StatusCode = 200; + DragaliaResponse gameResponse = new(new DataHeaders(code), code); + + await context.Response.Body.WriteAsync( + MessagePackSerializer.Serialize(gameResponse, CustomResolver.Options) + ); + } } diff --git a/DragaliaAPI/Services/Game/AuthService.cs b/DragaliaAPI/Services/Game/AuthService.cs index f60c19c45..6eaeba7db 100644 --- a/DragaliaAPI/Services/Game/AuthService.cs +++ b/DragaliaAPI/Services/Game/AuthService.cs @@ -92,6 +92,13 @@ IDateTimeProvider dateTimeProvider TokenValidationResult result = await this.ValidateToken(idToken); JwtSecurityToken jwt = (JwtSecurityToken)result.SecurityToken; + this.logger.LogDebug( + "Token {token} validity: from {fromTime} to {toTime}", + jwt.ToString(), + jwt.ValidFrom, + jwt.ValidTo + ); + using IDisposable accIdLog = LogContext.PushProperty( CustomClaimType.AccountId, jwt.Subject