Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0c8a4d7
refactor chat controller
WeylonSantana Jan 11, 2025
48487f3
refactor demo controller
WeylonSantana Jan 11, 2025
6df318f
add produce response types to the game object routes
WeylonSantana Jan 11, 2025
80aa16f
add produce response types to the guild controller
WeylonSantana Jan 11, 2025
bcd91c7
add produce response types to the info controller
WeylonSantana Jan 12, 2025
32725ad
add produce response types to the logs controller
WeylonSantana Jan 12, 2025
19f7996
add produce response types and refactor player controller
WeylonSantana Jan 12, 2025
b0dbb21
refactor user controller
WeylonSantana Jan 12, 2025
b29a36f
add produce response types to the server variables controller
WeylonSantana Jan 12, 2025
2b7b9e9
re-sorting some files to a new folder structure and making working
WeylonSantana Jan 13, 2025
285c9d5
fix: valid player not found on player route
WeylonSantana Jan 13, 2025
4f83a3f
move lookupkey, sort and sortdirection to the right namespace
WeylonSantana Jan 13, 2025
71ec70b
create safe fetch on user
WeylonSantana Jan 13, 2025
a0c5de2
create safe fetch on player
WeylonSantana Jan 13, 2025
0f609b5
rename files to be more descriptive
WeylonSantana Jan 13, 2025
985df9a
fix: build broken
WeylonSantana Jan 14, 2025
343597a
converting into a record struct
WeylonSantana Jan 14, 2025
1be454e
merging user/player tryfetch
WeylonSantana Jan 14, 2025
a75c075
fix broken logic
WeylonSantana Jan 14, 2025
a427bc1
check if password is hashed on register by api
WeylonSantana Jan 14, 2025
a043ba1
better password checking
WeylonSantana Jan 14, 2025
4108ba6
cache password on user register
WeylonSantana Jan 14, 2025
1c5e6f8
cache password on validate password too
WeylonSantana Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Intersect (Core)/Security/PasswordUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public static string ComputePasswordHash(string password)
{
return BitConverter.ToString(SHA256.HashData(Encoding.UTF8.GetBytes(password ?? string.Empty))).Replace("-", string.Empty);
}

public static bool IsValidClientPasswordHash(string? hashToValidate) =>
hashToValidate is { Length: 64 } && hashToValidate.All(char.IsAsciiHexDigit);
}
14 changes: 7 additions & 7 deletions Intersect.Server.Core/Collections/Indexing/LookupKey.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.ComponentModel;
using System.ComponentModel;
using System.Globalization;
using Intersect.Server.Localization;
using Intersect.Utilities;

namespace Intersect.Server.Web.RestApi.Payloads;
namespace Intersect.Server.Collections.Indexing;

// TODO: Figure out how to get LookupKey to show up in swagger.json components/schemas despite being a "string", one or more of the following commented out attributes may help
// [SwaggerSubType(typeof(Guid))]
Expand All @@ -14,15 +14,15 @@ namespace Intersect.Server.Web.RestApi.Payloads;
public partial struct LookupKey
{

public bool HasName => !string.IsNullOrWhiteSpace(Name);
public readonly bool HasName => !string.IsNullOrWhiteSpace(Name);

public bool HasId => Guid.Empty != Id;
public readonly bool HasId => Guid.Empty != Id;

public bool IsNameInvalid => !HasId && Name != null;
public readonly bool IsNameInvalid => !HasId && Name != null;

public bool IsIdInvalid => !HasId && Name == null;
public readonly bool IsIdInvalid => !HasId && Name == null;

public bool IsInvalid => !HasId && !HasName;
public readonly bool IsInvalid => !HasId && !HasName;

public Guid Id { get; private set; }

Expand Down
9 changes: 3 additions & 6 deletions Intersect.Server.Core/Collections/Sorting/Sort.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace Intersect.Server.Web.RestApi.Payloads;

namespace Intersect.Server.Collections.Sorting;

public partial struct Sort
{

public string[] By { get; set; }

public SortDirection Direction { get; set; }
Expand All @@ -21,14 +19,13 @@ public static Sort[] From(string[] sortBy, SortDirection[] sortDirections)
{
var filteredBy =
sortBy?.Where(by => !string.IsNullOrWhiteSpace(by)).Take(sortDirections?.Length ?? 0).ToList() ??
new List<string>();
[];

var filteredDirections = sortDirections?.Take(filteredBy.Count).ToList() ?? new List<SortDirection>();
var filteredDirections = sortDirections?.Take(filteredBy.Count).ToList() ?? [];

return filteredBy.Select(
(by, index) => From(by ?? throw new InvalidOperationException(), filteredDirections[index])
)
.ToArray();
}

}
7 changes: 2 additions & 5 deletions Intersect.Server.Core/Collections/Sorting/SortDirection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
namespace Intersect.Server.Web.RestApi.Payloads;

namespace Intersect.Server.Collections.Sorting;

public enum SortDirection
{

Ascending,

Descending

Descending,
}
2 changes: 1 addition & 1 deletion Intersect.Server.Core/Database/PlayerData/Players/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
using Intersect.Logging;
using Intersect.Utilities;
using Intersect.Server.Localization;
using Intersect.Server.Web.RestApi.Payloads;
using static Intersect.Server.Database.Logging.Entities.GuildHistory;
using Intersect.Server.Collections.Sorting;

namespace Intersect.Server.Database.PlayerData.Players;

Expand Down
54 changes: 24 additions & 30 deletions Intersect.Server.Core/Database/PlayerData/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Intersect.Logging;
using Intersect.Reflection;
using Intersect.Security;
using Intersect.Server.Collections.Indexing;
using Intersect.Server.Collections.Sorting;
using Intersect.Server.Core;
using Intersect.Server.Database.Logging.Entities;
using Intersect.Server.Database.PlayerData.Api;
Expand All @@ -19,7 +21,6 @@
using Intersect.Server.General;
using Intersect.Server.Localization;
using Intersect.Server.Networking;
using Intersect.Server.Web.RestApi.Payloads;
using Intersect.Utilities;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand Down Expand Up @@ -529,18 +530,33 @@ public UserSaveResult Save(PlayerContext? playerContext, bool force = false, boo
return user;
}

public static Tuple<Client, User> Fetch(Guid userId)
public static bool TryFind(LookupKey lookupKey, [NotNullWhen(true)] out User? user)
{
var client = Globals.Clients.Find(queryClient => userId == queryClient?.User?.Id);

return new Tuple<Client, User>(client, client?.User ?? FindById(userId));
using var playerContext = DbInterface.CreatePlayerContext();
return TryFind(lookupKey, playerContext, out user);
}

public static Tuple<Client, User> Fetch(string userName)
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user) => TryFetch(lookupKey, out user, out _);

public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user, out Client? client)
{
var client = Globals.Clients.Find(queryClient => Entity.CompareName(userName, queryClient?.User?.Name));
if (lookupKey.IsInvalid)
{
user = default;
client = default;
return false;
}

return new Tuple<Client, User>(client, client?.User ?? Find(userName));
if (lookupKey.HasId)
{
client = Globals.Clients.Find(queryClient => lookupKey.Id == queryClient?.User?.Id);
user = client?.User ?? FindById(lookupKey.Id);
return user != default;
}

client = Globals.Clients.Find(queryClient => Entity.CompareName(lookupKey.Name, queryClient?.User?.Name));
user = client?.User ?? Find(lookupKey.Name);
return user != default;
}

public static bool TryLogin(
Expand Down Expand Up @@ -620,28 +636,6 @@ out LoginFailureReason failureReason
}
}

public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user) =>
TryFetch(lookupKey, out user, out _);

public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user, out Client? client)
{
if (lookupKey is { HasName: false, HasId: false })
{
user = default;
client = default;
return false;
}

(client, user) = lookupKey.HasId ? Fetch(lookupKey.Id) : Fetch(lookupKey.Name);
return user != default;
}

public static bool TryFind(LookupKey lookupKey, [NotNullWhen(true)] out User? user)
{
using var playerContext = DbInterface.CreatePlayerContext();
return TryFind(lookupKey, playerContext, out user);
}

public static bool TryFind(LookupKey lookupKey, PlayerContext playerContext, [NotNullWhen(true)] out User? user)
{
if (lookupKey.HasId)
Expand Down
64 changes: 31 additions & 33 deletions Intersect.Server.Core/Entities/Player.Database.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using Intersect.Logging;
using Intersect.Server.Database;
using Intersect.Server.Database.PlayerData;
using Intersect.Server.General;
using Intersect.Server.Networking;
using Intersect.Server.Web.RestApi.Payloads;
using Intersect.Server.Database.PlayerData.Players;
using Intersect.Utilities;

using Microsoft.EntityFrameworkCore;

using Newtonsoft.Json;
using Intersect.Server.Collections.Indexing;
using Intersect.Server.Collections.Sorting;

namespace Intersect.Server.Entities;

Expand All @@ -38,42 +39,39 @@ public partial class Player

#region Lookup

public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out Tuple<Client, Player>? tuple)
{
tuple = Fetch(lookupKey);
return tuple != default;
}

public static Tuple<Client, Player> Fetch(LookupKey lookupKey, bool loadRelationships = false,
bool loadBags = false)
public static bool TryFetch(
LookupKey lookupKey,
out Player? player,
bool loadRelationships = false,
bool loadBags = false
)
=> TryFetch(lookupKey, out _, out player, loadRelationships, loadBags);

public static bool TryFetch(
LookupKey lookupKey,
[NotNullWhen(true)] out Client? client,
out Player? player,
bool loadRelationships = false,
bool loadBags = false
)
{
if (lookupKey is { HasName: false, HasId: false })
if (lookupKey.IsInvalid)
{
return new Tuple<Client, Player>(null, null);
client = default;
player = default;
return false;
}

// HasName checks if null or empty
// ReSharper disable once AssignNullToNotNullAttribute
return lookupKey.HasId
? Fetch(lookupKey.Id)
: Fetch(lookupKey.Name, loadRelationships: loadRelationships, loadBags: loadBags);
}

public static Tuple<Client, Player> Fetch(string playerName, bool loadRelationships = false, bool loadBags = false)
{
var client = Globals.Clients.Find(queryClient => Entity.CompareName(playerName, queryClient?.Entity?.Name));

return new Tuple<Client, Player>(
client,
client?.Entity ?? Find(playerName, loadRelationships: loadRelationships, loadBags: loadBags)
);
}

public static Tuple<Client, Player> Fetch(Guid playerId)
{
var client = Globals.Clients.Find(queryClient => playerId == queryClient?.Entity?.Id);
if (lookupKey.HasId)
{
client = Globals.Clients.Find(queryClient => lookupKey.Id == queryClient?.Entity?.Id);
player = client?.Entity ?? Find(lookupKey.Id);
return player != default;
}

return new Tuple<Client, Player>(client, client?.Entity ?? Player.Find(playerId));
client = Globals.Clients.Find(queryClient => CompareName(lookupKey.Name, queryClient?.Entity?.Name));
player = client?.Entity ?? Find(lookupKey.Name, loadRelationships: loadRelationships, loadBags: loadBags);
return player != default;
}

public static Player Find(Guid playerId)
Expand Down
3 changes: 1 addition & 2 deletions Intersect.Server.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Intersect.Server.Web.RestApi.Payloads;

using Intersect.Server.Collections.Sorting;
using Microsoft.EntityFrameworkCore;

namespace Intersect.Server.Extensions;
Expand Down
5 changes: 2 additions & 3 deletions Intersect.Server.Core/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;

using Intersect.Server.Web.RestApi.Payloads;
using System.Linq.Expressions;
using Intersect.Server.Collections.Sorting;

using Microsoft.EntityFrameworkCore;

Expand Down
11 changes: 6 additions & 5 deletions Intersect.Server/Web/Net7/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Intersect.Server.Web.Configuration;
using Intersect.Server.Web.Constraints;
using Intersect.Server.Web.Middleware;
using Intersect.Server.Web.RestApi.Payloads;
using Intersect.Server.Web.RestApi.Types.Chat;
using Intersect.Server.Web.RestApi.Routes;
using Intersect.Server.Web.Serialization;
using Intersect.Server.Web.Swagger.Filters;
Expand All @@ -32,6 +32,7 @@
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using Intersect.Server.Collections.Indexing;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

namespace Intersect.Server.Web;
Expand Down Expand Up @@ -251,10 +252,10 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
options.TokenValidationParameters.ValidIssuer ??= tokenGenerationOptions.Issuer;
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = async _ => {},
OnChallenge = async _ => {},
OnMessageReceived = async _ => {},
OnTokenValidated = async _ => {},
OnAuthenticationFailed = async _ => { },
OnChallenge = async _ => { },
OnMessageReceived = async _ => { },
OnTokenValidated = async _ => { },
};
SymmetricSecurityKey issuerKey = new(tokenGenerationOptions.SecretData);
options.TokenValidationParameters.IssuerSigningKey = issuerKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Intersect.Server.Collections.Indexing;
using Intersect.Server.Localization;
using Intersect.Server.Web.RestApi.Payloads;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Intersect.Server.Web.RestApi.Payloads;
using Intersect.Server.Collections.Indexing;
using Intersect.Server.Web.RestApi.Types;
using Intersect.Server.Web.Swagger.Extensions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;
Expand Down
18 changes: 0 additions & 18 deletions Intersect.Server/Web/RestApi/Payloads/AdminChange.cs

This file was deleted.

20 changes: 0 additions & 20 deletions Intersect.Server/Web/RestApi/Payloads/AuthorizedChange.cs

This file was deleted.

Loading
Loading