Skip to content

Commit

Permalink
Several improvements to logging (#407)
Browse files Browse the repository at this point in the history
- Shows role instead of user id in logs, e.g. `jvyden420 (Admin)`
- Logs authentication successes, e.g. when a user logs in, logs out, or
refreshes a token
- Logs assets that were uploaded and who uploaded them
  • Loading branch information
jvyden authored Apr 16, 2024
2 parents 4ab417d + 2ab22db commit 0d7b4ba
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public ApiResponse<IApiAuthenticationResponse> Authenticate(RequestContext conte

Token token = database.GenerateTokenForUser(user, TokenType.Api, TokenGame.Website, TokenPlatform.Website);
Token refreshToken = database.GenerateTokenForUser(user, TokenType.ApiRefresh, TokenGame.Website, TokenPlatform.Website, GameDatabaseContext.RefreshTokenExpirySeconds);

context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} successfully logged in through the API");

return new ApiAuthenticationResponse
{
Expand All @@ -109,6 +111,8 @@ public ApiResponse<IApiAuthenticationResponse> RefreshToken(RequestContext conte
GameUser user = refreshToken.User;

Token token = database.GenerateTokenForUser(user, TokenType.Api, TokenGame.Website, TokenPlatform.Website);

context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} successfully refreshed their token through the API");

return new ApiAuthenticationResponse
{
Expand All @@ -134,6 +138,8 @@ public ApiOkResponse ResetPassword(RequestContext context, GameDatabaseContext d

database.SetUserPassword(user, passwordBcrypt);
database.RevokeTokenByTokenData(body.ResetToken, TokenType.PasswordReset);

context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} successfully reset their password");

return new ApiOkResponse();
}
Expand All @@ -159,7 +165,7 @@ public ApiOkResponse SendPasswordResetEmail(RequestContext context,
context.Logger.LogTrace(RefreshContext.PasswordReset, "Reset token: {0}", token.TokenData);
smtpService.SendPasswordResetRequest(user, token.TokenData);

context.Logger.LogTrace(RefreshContext.PasswordReset, "Email sent, token will expire at {0}", token.ExpiresAt);
context.Logger.LogInfo(RefreshContext.PasswordReset, "Email sent, token will expire at {0}", token.ExpiresAt);
return new ApiOkResponse();
}

Expand All @@ -168,6 +174,7 @@ public ApiOkResponse SendPasswordResetEmail(RequestContext context,
public ApiOkResponse RevokeThisToken(RequestContext context, GameDatabaseContext database, Token token)
{
database.RevokeToken(token);
context.Logger.LogInfo(BunkumCategory.Authentication, $"{token.User} logged out");
return new ApiOkResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public class AuthenticationEndpoints : EndpointGroup
//Clear the user's force match
database.ClearForceMatch(user);

context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} successfully logged in on {game} via {platform}");

if (game == TokenGame.LittleBigPlanetPSP)
{
return new TicketLoginResponse
Expand Down Expand Up @@ -270,7 +272,8 @@ public Response RevokeThisToken(RequestContext context, GameDatabaseContext data

if (!result)
return Unauthorized;


context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} logged out");
return OK;
}
}
Expand Down
1 change: 1 addition & 0 deletions Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public Response UploadAsset(RequestContext context, string hash, string type, by

database.IncrementUserFilesizeQuota(user, body.Length);

context.Logger.LogInfo(BunkumCategory.UserContent, $"{user} uploaded a {gameAsset.AssetType} ({body.Length / 1024f:F1} KB)");
return OK;
}

Expand Down
2 changes: 1 addition & 1 deletion Refresh.GameServer/Types/UserData/GameUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ [Ignored] public GameUserRole Role
// ReSharper disable once InconsistentNaming
internal byte _Role { get; set; }

public override string ToString() => $"{this.Username} ({this.UserId})";
public override string ToString() => $"{this.Username} ({this.Role})";

#region Rate-limiting
public bool RateLimitUserIdIsEqual(object obj)
Expand Down

0 comments on commit 0d7b4ba

Please sign in to comment.