Skip to content

Commit

Permalink
Update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SapiensAnatis committed Jul 27, 2023
1 parent fd0ab4e commit 8b3093b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
31 changes: 17 additions & 14 deletions DragaliaAPI/Middleware/ExceptionHandlerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,19 +52,17 @@ 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.");
await cache.SetStringAsync(redisKey, "true", CacheOptions);

context.Response.StatusCode = 400;
context.Response.Headers.Add("Is-Required-Refresh-Id-Token", "true");
return;
}
catch (Exception ex) when (serializeException)
{
Expand All @@ -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;
Expand All @@ -88,16 +84,23 @@ IDistributedCache cache
code
);

DragaliaResponse<DataHeaders> gameResponse = new(new DataHeaders(code), code);

await context.Response.Body.WriteAsync(
MessagePackSerializer.Serialize(gameResponse, CustomResolver.Options)
);
await WriteResultCode(context, code);
}
}

private static string? GetRedisKey(string? deviceId)
{
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<DataHeaders> gameResponse = new(new DataHeaders(code), code);

await context.Response.Body.WriteAsync(
MessagePackSerializer.Serialize(gameResponse, CustomResolver.Options)
);
}
}
7 changes: 7 additions & 0 deletions DragaliaAPI/Services/Game/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b3093b

Please sign in to comment.