Skip to content

Commit c2f5e6c

Browse files
authored
Print token data in certain cases (#1056)
Makes the exception messages of some of the recently added token validations include token data, and also makes `GameAuthenticationProvider` always print various information about the request and the received token as long as a certain config option is enabled. This is primarily meant to help understand future issues if they ever happen.
2 parents 7ca1750 + 6e51785 commit c2f5e6c

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

Refresh.Core/Configuration/GameServerConfig.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Refresh.Core.Configuration;
1010
[SuppressMessage("ReSharper", "RedundantDefaultMemberInitializer")]
1111
public class GameServerConfig : Config
1212
{
13-
public override int CurrentConfigVersion => 25;
13+
public override int CurrentConfigVersion => 26;
1414
public override int Version { get; set; } = 0;
1515

1616
protected override void Migrate(int oldVer, dynamic oldConfig)
@@ -117,6 +117,11 @@ protected override void Migrate(int oldVer, dynamic oldConfig)
117117
/// </summary>
118118
public bool PrintRoomStateWhenNoFoundRooms { get; set; } = true;
119119

120+
/// <summary>
121+
/// Whether to unconditionally print data like token, token owner, remote IP, request URI etc during authentication outside of exceptions
122+
/// </summary>
123+
public bool PrintAuthenticationData { get; set; } = false;
124+
120125
public string[] Sha1DigestKeys = ["CustomServerDigest"];
121126
public string[] HmacDigestKeys = ["CustomServerDigest"];
122127

Refresh.Database/GameDatabaseContext.Tokens.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Token GenerateTokenForUser(GameUser user, TokenType type, TokenGame game,
9090
#if DEBUG
9191
if(Debugger.IsAttached) Debugger.Break();
9292
#endif
93-
throw new InvalidDataException($"GetTokenFromTokenData - Token data or type does not match!");
93+
throw new InvalidDataException($"GetTokenFromTokenData - Token data or type does not match (expected {tokenData} | {type}, got {token.TokenData} | {token.TokenType} by {token.User})!");
9494
}
9595

9696
return token;

Refresh.GameServer/Authentication/GameAuthenticationProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,16 @@ public GameAuthenticationProvider(GameServerConfig? config, Logger logger)
7676
if ((this._config?.MaintenanceMode ?? false) && user.Role != GameUserRole.Admin)
7777
return null;
7878

79+
if (this._config?.PrintAuthenticationData ?? false)
80+
this._logger.LogInfo(BunkumCategory.Authentication, $"Authenticating request from {request.RemoteEndpoint} to {request.Uri.AbsolutePath} by {user} using token {tokenData}");
81+
7982
// Additional validation of the token gotten from DB. Exceptions will be caught, logged and InternalServerError will be returned automatically.
8083
if (token.TokenData != tokenData)
8184
{
8285
#if DEBUG
8386
if(Debugger.IsAttached) Debugger.Break();
8487
#endif
85-
throw new InvalidDataException($"{typeof(GameAuthenticationProvider)} - Token from DB does not match token received from client!");
88+
throw new InvalidDataException($"{typeof(GameAuthenticationProvider)} - Token from DB ({token.TokenData}) does not match token received from client ({tokenData})!");
8689
}
8790

8891
if (token.User.UserId != token.UserId)

0 commit comments

Comments
 (0)