Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Tickets;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
Expand All @@ -20,7 +21,7 @@
public class LoginController : ControllerBase
{
private readonly DatabaseContext database;
public LoginController(DatabaseContext database)

Check notice on line 24 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert constructor into primary constructor

Convert into primary constructor
{
this.database = database;
}
Expand Down Expand Up @@ -74,7 +75,7 @@
case Platform.PS3:
case Platform.Vita:
case Platform.UnitTest:
user = await database.Users.FirstOrDefaultAsync(u => u.LinkedPsnId == npTicket.UserId);

Check notice on line 78 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
break;
case Platform.PSP:
case Platform.Unknown:
Expand All @@ -86,7 +87,7 @@
if (user == null)
{
// Check if there is an account with that username already
UserEntity? targetUsername = await database.Users.FirstOrDefaultAsync(u => u.Username == npTicket.Username);

Check notice on line 90 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
if (targetUsername != null)
{
ulong targetPlatform = npTicket.Platform == Platform.RPCS3
Expand All @@ -101,7 +102,7 @@
}

// if there is already a pending link request don't create another
bool linkAttemptExists = await database.PlatformLinkAttempts.AnyAsync(p =>

Check notice on line 105 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
p.Platform == npTicket.Platform &&
p.PlatformId == npTicket.UserId &&
p.UserId == targetUsername.UserId);
Expand All @@ -116,8 +117,8 @@
Timestamp = TimeHelper.TimestampMillis,
PlatformId = npTicket.UserId,
};
database.PlatformLinkAttempts.Add(linkAttempt);

Check notice on line 120 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
await database.SaveChangesAsync();

Check notice on line 121 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
Logger.Success($"User '{npTicket.Username}' tried to login but platform isn't linked, platform={npTicket.Platform}", LogArea.Login);
return this.Forbid();
}
Expand Down Expand Up @@ -147,11 +148,11 @@
}

// create account for user if they don't exist
user = await database.CreateUser(username, "$");

Check notice on line 151 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
user.Password = null;
user.LinkedRpcnId = npTicket.Platform == Platform.RPCS3 ? npTicket.UserId : 0;
user.LinkedPsnId = npTicket.Platform != Platform.RPCS3 ? npTicket.UserId : 0;
await database.SaveChangesAsync();

Check notice on line 155 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing

if (DiscordConfiguration.Instance.DiscordIntegrationEnabled)
{
Expand All @@ -171,7 +172,7 @@
// automatically change username if it doesn't match
else if (user.Username != npTicket.Username)
{
bool usernameExists = await database.Users.AnyAsync(u => u.Username == npTicket.Username);

Check notice on line 175 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
if (usernameExists)
{
Logger.Warn($"{npTicket.Platform} user changed their name to a name that is already taken," +
Expand All @@ -180,17 +181,17 @@
}
Logger.Info($"User's username has changed, old='{user.Username}', new='{npTicket.Username}', platform={npTicket.Platform}", LogArea.Login);
user.Username = username;
await database.PlatformLinkAttempts.RemoveWhere(p => p.UserId == user.UserId);

Check notice on line 184 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
// unlink other platforms because the names no longer match
if (npTicket.Platform == Platform.RPCS3)
user.LinkedPsnId = 0;
else
user.LinkedRpcnId = 0;

await database.SaveChangesAsync();

Check notice on line 191 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
}

GameTokenEntity? token = await database.GameTokens.Include(t => t.User)

Check notice on line 194 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
.FirstOrDefaultAsync(t => t.User.Username == npTicket.Username && t.TicketHash == npTicket.TicketHash);

if (token != null)
Expand All @@ -199,7 +200,7 @@
return this.Forbid();
}

token = await database.AuthenticateUser(user, npTicket, ipAddress);

Check notice on line 203 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing
if (token == null)
{
Logger.Warn($"Unable to find/generate a token for username {npTicket.Username}", LogArea.Login);
Expand All @@ -212,11 +213,19 @@
return this.Forbid();
}

if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent)
{
if (!PatchworkHelper.UserHasValidPatchworkUserAgent(token, this.Request.Headers.UserAgent.ToString()))
{
return this.Forbid();
}
}

Logger.Success($"Successfully logged in user {user.Username} as {token.GameVersion} client", LogArea.Login);

user.LastLogin = TimeHelper.TimestampMillis;

await database.SaveChangesAsync();

Check notice on line 228 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Add/remove 'this.' qualifier

Qualifier 'this.' is missing

// Create a new room on LBP2/3/Vita
if (token.GameVersion != GameVersion.LittleBigPlanet1) RoomHelper.CreateRoom(user.UserId, token.GameVersion, token.Platform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

private readonly DatabaseContext database;

public LogoutController(DatabaseContext database)

Check notice on line 20 in ProjectLighthouse.Servers.GameServer/Controllers/Login/LogoutController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert constructor into primary constructor

Convert into primary constructor
{
this.database = database;
}
Expand All @@ -38,5 +38,4 @@
return this.Ok();
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;
using System.Text;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
Expand Down Expand Up @@ -42,7 +42,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.";

public MessageController(DatabaseContext database)

Check notice on line 45 in ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert constructor into primary constructor

Convert into primary constructor
{
this.database = database;
}
Expand All @@ -54,6 +54,10 @@
public async Task<IActionResult> Announce()
{
GameTokenEntity token = this.GetToken();
UserEntity? user = await this.database.UserFromGameToken(token);

if (user == null)
return this.Forbid();

string username = await this.database.UsernameFromGameToken(token);

Expand All @@ -72,7 +76,7 @@
$"user.UserId: {token.UserId}\n" +
$"token.GameVersion: {token.GameVersion}\n" +
$"token.TicketHash: {token.TicketHash}\n" +
$"token.ExpiresAt: {token.ExpiresAt.ToString()}\n" +

Check warning on line 79 in ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Specify string culture explicitly

Specify string culture explicitly
"---DEBUG INFO---");
#endif

Expand Down Expand Up @@ -128,7 +132,7 @@

if (message.StartsWith("/setemail ") && ServerConfiguration.Instance.Mail.MailEnabled)
{
string email = message[(message.IndexOf(" ", StringComparison.Ordinal)+1)..];

Check notice on line 135 in ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

RoslynAnalyzers Use char overload

Use 'string.IndexOf(char)' instead of 'string.IndexOf(string)' when you have a string with a single char
if (!SanitizationHelper.IsValidEmail(email)) return this.Ok();

if (await this.database.Users.AnyAsync(u => u.EmailAddress == email)) return this.Ok();
Expand Down
42 changes: 42 additions & 0 deletions ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Configuration.ConfigurationCategories;

Check warning on line 2 in ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant using directive

Using directive is not required by the code and can be safely removed
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Users;

namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;

public static class PatchworkHelper
{
static int patchworkMajorVer = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum;

Check notice on line 10 in ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Field can be made readonly (private accessibility)

Field can be made readonly
static int patchworkMinorVer = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum;

Check notice on line 11 in ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Field can be made readonly (private accessibility)

Field can be made readonly
public static bool UserHasValidPatchworkUserAgent(GameTokenEntity token, string userAgent)
{
string userAgentPrefix = "PatchworkLBP";

Check notice on line 14 in ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert local variable or field into constant (private accessibility)

Convert into constant
char gameVersion = userAgent[userAgentPrefix.Length];
int numericVersion = 0;

if (userAgent.StartsWith(userAgentPrefix))
return false;

if (char.IsLetterOrDigit(gameVersion))
{
if (gameVersion == 'V')
numericVersion = 4;
}
else
numericVersion = gameVersion - '0';

// Don't want it to be 0 still because of Unknown (-1) in GameVersion
if (numericVersion == 0)
return false;

if (numericVersion - 1 != (int)token.GameVersion && !Enum.IsDefined(typeof(GameVersion), numericVersion - 1))
return false;

string[] patchworkVer = userAgent.Split(' ')[1].Split('.');
if (int.Parse(patchworkVer[0]) >= patchworkMajorVer && int.Parse(patchworkVer[1]) >= patchworkMinorVer)

Check notice on line 37 in ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

'if-return' statement can be rewritten as 'return' statement

Convert into 'return' statement
return true;

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
public class AuthenticationConfiguration
{
public bool RegistrationEnabled { get; set; } = true;
public bool AutomaticAccountCreation { get; set; } = true;

Check notice on line 6 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public bool VerifyTickets { get; set; } = true;

Check notice on line 7 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

public bool AllowRPCNSignup { get; set; } = true;

Check notice on line 9 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

public bool AllowPSNSignup { get; set; } = true;

Check notice on line 11 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

// Require use of Zaprit's "Patchwork" prx plugin's user agent when connecting to the server
// Major and minor version minimums can be left alone if patchwork is not required
public bool RequirePatchworkUserAgent { get; set; } = false;

Check notice on line 15 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public int PatchworkMajorVersionMinimum { get; set; } = 0;

Check notice on line 16 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public int PatchworkMinorVersionMinimum { get; set; } = 0;

Check notice on line 17 in ProjectLighthouse/Configuration/ConfigurationCategories/AuthenticationConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

}
3 changes: 1 addition & 2 deletions ProjectLighthouse/Configuration/ServerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,38 @@
// This is so Lighthouse can properly identify outdated configurations and update them with newer settings accordingly.
// If you are modifying anything here, this value MUST be incremented.
// Thanks for listening~
public override int ConfigVersion { get; set; } = 27;
public override int ConfigVersion { get; set; } = 29;

public override string ConfigName { get; set; } = "lighthouse.yml";
public string WebsiteListenUrl { get; set; } = "http://localhost:10060";

Check notice on line 17 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public string GameApiListenUrl { get; set; } = "http://localhost:10061";

Check notice on line 18 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public string ApiListenUrl { get; set; } = "http://localhost:10062";

Check notice on line 19 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

public string DbConnectionString { get; set; } = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse";
public string RedisConnectionString { get; set; } = "redis://localhost:6379";

Check notice on line 22 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public string ExternalUrl { get; set; } = "http://localhost:10060";

Check notice on line 23 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public string GameApiExternalUrl { get; set; } = "http://localhost:10060/LITTLEBIGPLANETPS3_XML";

Check notice on line 24 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public string EulaText { get; set; } = "";
#if !DEBUG
public string AnnounceText { get; set; } = "You are now logged in as %user.";
#else
public string AnnounceText { get; set; } = "You are now logged in as %user (id: %id).";
#endif
public bool CheckForUnsafeFiles { get; set; } = true;

Check notice on line 31 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public bool LogChatFiltering { get; set; } = false;

Check notice on line 32 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public bool LogChatMessages { get; set; } = false;

Check notice on line 33 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

public AuthenticationConfiguration Authentication { get; set; } = new();

Check notice on line 34 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public CaptchaConfiguration Captcha { get; set; } = new();

Check notice on line 35 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public DigestKeyConfiguration DigestKey { get; set; } = new();

Check notice on line 36 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public MatchmakingConfiguration Matchmaking { get; set; } = new();

Check notice on line 37 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public GoogleAnalyticsConfiguration GoogleAnalytics { get; set; } = new();

Check notice on line 38 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public MailConfiguration Mail { get; set; } = new();

Check notice on line 39 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public UserGeneratedContentLimitConfiguration UserGeneratedContentLimits { get; set; } = new();

Check notice on line 40 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public WebsiteConfiguration WebsiteConfiguration { get; set; } = new();

Check notice on line 41 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public CustomizationConfiguration Customization { get; set; } = new();

Check notice on line 42 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public RateLimitConfiguration RateLimitConfiguration { get; set; } = new();

Check notice on line 43 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public TwoFactorConfiguration TwoFactorConfiguration { get; set; } = new();

Check notice on line 44 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public RichPresenceConfiguration RichPresenceConfiguration { get; set; } = new();

Check notice on line 45 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only
public NotificationConfiguration NotificationConfiguration { get; set; } = new();

Check notice on line 46 in ProjectLighthouse/Configuration/ServerConfiguration.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Auto-property can be made get-only (non-private accessibility)

Auto-property can be made get-only

public override ConfigurationBase<ServerConfiguration> Deserialize(IDeserializer deserializer, string text) => deserializer.Deserialize<ServerConfiguration>(text);
Expand Down
Loading