Skip to content

Commit f511723

Browse files
committed
Additional auth checks
1 parent 67ef4c4 commit f511723

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

Refresh.Database/GameDatabaseContext.Tokens.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Refresh.Database.Models.Authentication;
55
using Refresh.Database.Models.Users;
66
using Refresh.Database.Models.Relations;
7+
using System.Diagnostics;
78

89
namespace Refresh.Database;
910

@@ -84,6 +85,14 @@ public Token GenerateTokenForUser(GameUser user, TokenType type, TokenGame game,
8485
return null;
8586
}
8687

88+
if (token.TokenData != tokenData || token.TokenType != type)
89+
{
90+
#if DEBUG
91+
if(Debugger.IsAttached) Debugger.Break();
92+
#endif
93+
throw new InvalidDataException($"GetTokenFromTokenData - Token data or type does not match!");
94+
}
95+
8796
return token;
8897
}
8998

Refresh.Database/GameDatabaseContext.Users.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Refresh.Database.Models.Levels;
1010
using Refresh.Database.Models.Photos;
1111
using Refresh.Database.Models.Assets;
12+
using System.Diagnostics;
1213

1314
namespace Refresh.Database;
1415

@@ -64,8 +65,20 @@ public partial class GameDatabaseContext // Users
6465
public GameUser? GetUserByEmailAddress(string? emailAddress)
6566
{
6667
if (emailAddress == null) return null;
68+
6769
emailAddress = emailAddress.ToLowerInvariant();
68-
return this.GameUsersIncluded.FirstOrDefault(u => u.EmailAddress == emailAddress);
70+
GameUser? user = this.GameUsersIncluded.FirstOrDefault(u => u.EmailAddress == emailAddress);
71+
if (user == null) return null;
72+
73+
if (user.EmailAddress != emailAddress)
74+
{
75+
#if DEBUG
76+
if(Debugger.IsAttached) Debugger.Break();
77+
#endif
78+
throw new InvalidDataException($"GetUserByEmailAddress - Found user's email does not match given email!");
79+
}
80+
81+
return user;
6982
}
7083

7184
[Pure]

Refresh.Interfaces.APIv3/Endpoints/AuthenticationApiEndpoints.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Net;
23
using AttribDoc.Attributes;
34
using Bunkum.Core;
@@ -105,6 +106,21 @@ public ApiResponse<IApiAuthenticationResponse> Authenticate(RequestContext conte
105106

106107
Token token = database.GenerateTokenForUser(user, TokenType.Api, TokenGame.Website, TokenPlatform.Website, ipAddress);
107108
Token refreshToken = database.GenerateTokenForUser(user, TokenType.ApiRefresh, TokenGame.Website, TokenPlatform.Website, ipAddress, GameDatabaseContext.RefreshTokenExpirySeconds);
109+
110+
if (user.UserId != token.UserId)
111+
{
112+
#if DEBUG
113+
if(Debugger.IsAttached) Debugger.Break();
114+
#endif
115+
throw new InvalidDataException($"API login - API token owner ({token.User}) does not match user received from DB ({user})!");
116+
}
117+
if (user.UserId != refreshToken.UserId)
118+
{
119+
#if DEBUG
120+
if(Debugger.IsAttached) Debugger.Break();
121+
#endif
122+
throw new InvalidDataException($"API login - Refresh token owner ({refreshToken.User}) does not match user received from DB ({user})!");
123+
}
108124

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

@@ -131,8 +147,15 @@ public ApiResponse<IApiAuthenticationResponse> RefreshToken(RequestContext conte
131147
GameUser user = refreshToken.User;
132148

133149
Token token = database.GenerateTokenForUser(user, TokenType.Api, TokenGame.Website, TokenPlatform.Website, context.RemoteIp());
150+
if (token.UserId != refreshToken.UserId)
151+
{
152+
#if DEBUG
153+
if(Debugger.IsAttached) Debugger.Break();
154+
#endif
155+
throw new InvalidDataException($"RefreshToken - Owner of new token ({token.User}) does not match owner of refresh token ({refreshToken.User})!");
156+
}
157+
134158
database.ResetApiRefreshTokenExpiry(refreshToken);
135-
136159
context.Logger.LogInfo(BunkumCategory.Authentication, $"{user} successfully refreshed their token through the API");
137160

138161
return new ApiAuthenticationResponse

Refresh.Interfaces.Game/Endpoints/Handshake/AuthenticationEndpoints.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Xml.Serialization;
23
using Bunkum.Core;
34
using Bunkum.Core.Endpoints;
@@ -70,6 +71,15 @@ public class AuthenticationEndpoints : EndpointGroup
7071

7172
user = database.CreateUserFromQueuedRegistration(registration, platform);
7273

74+
// Additional check to prevent spamming random users with emails
75+
if (user.Username != ticket.Username)
76+
{
77+
#if DEBUG
78+
if(Debugger.IsAttached) Debugger.Break();
79+
#endif
80+
throw new InvalidDataException($"Game login - Username {user.Username} from registration does not match username {ticket.Username} in NP ticket!");
81+
}
82+
7383
if (integrationConfig.SmtpEnabled)
7484
{
7585
EmailVerificationCode code = database.CreateEmailVerificationCode(user);
@@ -188,6 +198,14 @@ public class AuthenticationEndpoints : EndpointGroup
188198
context.Logger.LogWarning(BunkumCategory.Authentication, $"{ticket.Username}'s Patchwork version is invalid: {context.RequestHeaders["User-Agent"]}");
189199
return null;
190200
}
201+
202+
if (user.Username != ticket.Username)
203+
{
204+
#if DEBUG
205+
if(Debugger.IsAttached) Debugger.Break();
206+
#endif
207+
throw new InvalidDataException($"Game login - Username {user.Username} from DB GameUser does not match username {ticket.Username} in NP ticket!");
208+
}
191209

192210
// !!
193211
// Past this point, login is considered to be complete.

0 commit comments

Comments
 (0)