Skip to content

Commit 18fcec9

Browse files
chore: add more annotations to routes and sort some files (#2467)
* refactor chat controller * refactor demo controller * add produce response types to the game object routes * add produce response types to the guild controller * add produce response types to the info controller * add produce response types to the logs controller * add produce response types and refactor player controller * refactor user controller * add produce response types to the server variables controller * re-sorting some files to a new folder structure and making working * fix: valid player not found on player route * move lookupkey, sort and sortdirection to the right namespace * create safe fetch on user * create safe fetch on player * rename files to be more descriptive * fix: build broken * converting into a record struct * merging user/player tryfetch * fix broken logic * check if password is hashed on register by api * better password checking * cache password on user register * cache password on validate password too
1 parent 5a8e52b commit 18fcec9

File tree

65 files changed

+1413
-1762
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1413
-1762
lines changed

Intersect (Core)/Security/PasswordUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ public static string ComputePasswordHash(string password)
99
{
1010
return BitConverter.ToString(SHA256.HashData(Encoding.UTF8.GetBytes(password ?? string.Empty))).Replace("-", string.Empty);
1111
}
12+
13+
public static bool IsValidClientPasswordHash(string? hashToValidate) =>
14+
hashToValidate is { Length: 64 } && hashToValidate.All(char.IsAsciiHexDigit);
1215
}

Intersect.Server.Core/Collections/Indexing/LookupKey.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
22
using System.Globalization;
33
using Intersect.Server.Localization;
44
using Intersect.Utilities;
55

6-
namespace Intersect.Server.Web.RestApi.Payloads;
6+
namespace Intersect.Server.Collections.Indexing;
77

88
// 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
99
// [SwaggerSubType(typeof(Guid))]
@@ -14,15 +14,15 @@ namespace Intersect.Server.Web.RestApi.Payloads;
1414
public partial struct LookupKey
1515
{
1616

17-
public bool HasName => !string.IsNullOrWhiteSpace(Name);
17+
public readonly bool HasName => !string.IsNullOrWhiteSpace(Name);
1818

19-
public bool HasId => Guid.Empty != Id;
19+
public readonly bool HasId => Guid.Empty != Id;
2020

21-
public bool IsNameInvalid => !HasId && Name != null;
21+
public readonly bool IsNameInvalid => !HasId && Name != null;
2222

23-
public bool IsIdInvalid => !HasId && Name == null;
23+
public readonly bool IsIdInvalid => !HasId && Name == null;
2424

25-
public bool IsInvalid => !HasId && !HasName;
25+
public readonly bool IsInvalid => !HasId && !HasName;
2626

2727
public Guid Id { get; private set; }
2828

Intersect.Server.Core/Collections/Sorting/Sort.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
namespace Intersect.Server.Web.RestApi.Payloads;
2-
1+
namespace Intersect.Server.Collections.Sorting;
32

43
public partial struct Sort
54
{
6-
75
public string[] By { get; set; }
86

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

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

2826
return filteredBy.Select(
2927
(by, index) => From(by ?? throw new InvalidOperationException(), filteredDirections[index])
3028
)
3129
.ToArray();
3230
}
33-
3431
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
namespace Intersect.Server.Web.RestApi.Payloads;
2-
1+
namespace Intersect.Server.Collections.Sorting;
32

43
public enum SortDirection
54
{
6-
75
Ascending,
86

9-
Descending
10-
7+
Descending,
118
}

Intersect.Server.Core/Database/PlayerData/Players/Guild.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
using Intersect.Logging;
1616
using Intersect.Utilities;
1717
using Intersect.Server.Localization;
18-
using Intersect.Server.Web.RestApi.Payloads;
1918
using static Intersect.Server.Database.Logging.Entities.GuildHistory;
19+
using Intersect.Server.Collections.Sorting;
2020

2121
namespace Intersect.Server.Database.PlayerData.Players;
2222

Intersect.Server.Core/Database/PlayerData/User.cs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Intersect.Logging;
1111
using Intersect.Reflection;
1212
using Intersect.Security;
13+
using Intersect.Server.Collections.Indexing;
14+
using Intersect.Server.Collections.Sorting;
1315
using Intersect.Server.Core;
1416
using Intersect.Server.Database.Logging.Entities;
1517
using Intersect.Server.Database.PlayerData.Api;
@@ -19,7 +21,6 @@
1921
using Intersect.Server.General;
2022
using Intersect.Server.Localization;
2123
using Intersect.Server.Networking;
22-
using Intersect.Server.Web.RestApi.Payloads;
2324
using Intersect.Utilities;
2425
using Microsoft.EntityFrameworkCore;
2526
using Newtonsoft.Json;
@@ -529,18 +530,33 @@ public UserSaveResult Save(PlayerContext? playerContext, bool force = false, boo
529530
return user;
530531
}
531532

532-
public static Tuple<Client, User> Fetch(Guid userId)
533+
public static bool TryFind(LookupKey lookupKey, [NotNullWhen(true)] out User? user)
533534
{
534-
var client = Globals.Clients.Find(queryClient => userId == queryClient?.User?.Id);
535-
536-
return new Tuple<Client, User>(client, client?.User ?? FindById(userId));
535+
using var playerContext = DbInterface.CreatePlayerContext();
536+
return TryFind(lookupKey, playerContext, out user);
537537
}
538538

539-
public static Tuple<Client, User> Fetch(string userName)
539+
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user) => TryFetch(lookupKey, out user, out _);
540+
541+
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user, out Client? client)
540542
{
541-
var client = Globals.Clients.Find(queryClient => Entity.CompareName(userName, queryClient?.User?.Name));
543+
if (lookupKey.IsInvalid)
544+
{
545+
user = default;
546+
client = default;
547+
return false;
548+
}
542549

543-
return new Tuple<Client, User>(client, client?.User ?? Find(userName));
550+
if (lookupKey.HasId)
551+
{
552+
client = Globals.Clients.Find(queryClient => lookupKey.Id == queryClient?.User?.Id);
553+
user = client?.User ?? FindById(lookupKey.Id);
554+
return user != default;
555+
}
556+
557+
client = Globals.Clients.Find(queryClient => Entity.CompareName(lookupKey.Name, queryClient?.User?.Name));
558+
user = client?.User ?? Find(lookupKey.Name);
559+
return user != default;
544560
}
545561

546562
public static bool TryLogin(
@@ -620,28 +636,6 @@ out LoginFailureReason failureReason
620636
}
621637
}
622638

623-
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user) =>
624-
TryFetch(lookupKey, out user, out _);
625-
626-
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out User? user, out Client? client)
627-
{
628-
if (lookupKey is { HasName: false, HasId: false })
629-
{
630-
user = default;
631-
client = default;
632-
return false;
633-
}
634-
635-
(client, user) = lookupKey.HasId ? Fetch(lookupKey.Id) : Fetch(lookupKey.Name);
636-
return user != default;
637-
}
638-
639-
public static bool TryFind(LookupKey lookupKey, [NotNullWhen(true)] out User? user)
640-
{
641-
using var playerContext = DbInterface.CreatePlayerContext();
642-
return TryFind(lookupKey, playerContext, out user);
643-
}
644-
645639
public static bool TryFind(LookupKey lookupKey, PlayerContext playerContext, [NotNullWhen(true)] out User? user)
646640
{
647641
if (lookupKey.HasId)

Intersect.Server.Core/Entities/Player.Database.cs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using System.ComponentModel.DataAnnotations.Schema;
1+
using System.ComponentModel.DataAnnotations.Schema;
22
using System.Diagnostics.CodeAnalysis;
33
using Intersect.Logging;
44
using Intersect.Server.Database;
55
using Intersect.Server.Database.PlayerData;
66
using Intersect.Server.General;
77
using Intersect.Server.Networking;
8-
using Intersect.Server.Web.RestApi.Payloads;
98
using Intersect.Server.Database.PlayerData.Players;
109
using Intersect.Utilities;
1110

1211
using Microsoft.EntityFrameworkCore;
1312

1413
using Newtonsoft.Json;
14+
using Intersect.Server.Collections.Indexing;
15+
using Intersect.Server.Collections.Sorting;
1516

1617
namespace Intersect.Server.Entities;
1718

@@ -38,42 +39,39 @@ public partial class Player
3839

3940
#region Lookup
4041

41-
public static bool TryFetch(LookupKey lookupKey, [NotNullWhen(true)] out Tuple<Client, Player>? tuple)
42-
{
43-
tuple = Fetch(lookupKey);
44-
return tuple != default;
45-
}
46-
47-
public static Tuple<Client, Player> Fetch(LookupKey lookupKey, bool loadRelationships = false,
48-
bool loadBags = false)
42+
public static bool TryFetch(
43+
LookupKey lookupKey,
44+
out Player? player,
45+
bool loadRelationships = false,
46+
bool loadBags = false
47+
)
48+
=> TryFetch(lookupKey, out _, out player, loadRelationships, loadBags);
49+
50+
public static bool TryFetch(
51+
LookupKey lookupKey,
52+
[NotNullWhen(true)] out Client? client,
53+
out Player? player,
54+
bool loadRelationships = false,
55+
bool loadBags = false
56+
)
4957
{
50-
if (lookupKey is { HasName: false, HasId: false })
58+
if (lookupKey.IsInvalid)
5159
{
52-
return new Tuple<Client, Player>(null, null);
60+
client = default;
61+
player = default;
62+
return false;
5363
}
5464

55-
// HasName checks if null or empty
56-
// ReSharper disable once AssignNullToNotNullAttribute
57-
return lookupKey.HasId
58-
? Fetch(lookupKey.Id)
59-
: Fetch(lookupKey.Name, loadRelationships: loadRelationships, loadBags: loadBags);
60-
}
61-
62-
public static Tuple<Client, Player> Fetch(string playerName, bool loadRelationships = false, bool loadBags = false)
63-
{
64-
var client = Globals.Clients.Find(queryClient => Entity.CompareName(playerName, queryClient?.Entity?.Name));
65-
66-
return new Tuple<Client, Player>(
67-
client,
68-
client?.Entity ?? Find(playerName, loadRelationships: loadRelationships, loadBags: loadBags)
69-
);
70-
}
71-
72-
public static Tuple<Client, Player> Fetch(Guid playerId)
73-
{
74-
var client = Globals.Clients.Find(queryClient => playerId == queryClient?.Entity?.Id);
65+
if (lookupKey.HasId)
66+
{
67+
client = Globals.Clients.Find(queryClient => lookupKey.Id == queryClient?.Entity?.Id);
68+
player = client?.Entity ?? Find(lookupKey.Id);
69+
return player != default;
70+
}
7571

76-
return new Tuple<Client, Player>(client, client?.Entity ?? Player.Find(playerId));
72+
client = Globals.Clients.Find(queryClient => CompareName(lookupKey.Name, queryClient?.Entity?.Name));
73+
player = client?.Entity ?? Find(lookupKey.Name, loadRelationships: loadRelationships, loadBags: loadBags);
74+
return player != default;
7775
}
7876

7977
public static Player Find(Guid playerId)

Intersect.Server.Core/Extensions/EnumerableExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Intersect.Server.Web.RestApi.Payloads;
2-
1+
using Intersect.Server.Collections.Sorting;
32
using Microsoft.EntityFrameworkCore;
43

54
namespace Intersect.Server.Extensions;

Intersect.Server.Core/Extensions/QueryableExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System.Linq.Expressions;
2-
3-
using Intersect.Server.Web.RestApi.Payloads;
1+
using System.Linq.Expressions;
2+
using Intersect.Server.Collections.Sorting;
43

54
using Microsoft.EntityFrameworkCore;
65

Intersect.Server/Web/Net7/ApiService.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Intersect.Server.Web.Configuration;
1111
using Intersect.Server.Web.Constraints;
1212
using Intersect.Server.Web.Middleware;
13-
using Intersect.Server.Web.RestApi.Payloads;
13+
using Intersect.Server.Web.RestApi.Types.Chat;
1414
using Intersect.Server.Web.RestApi.Routes;
1515
using Intersect.Server.Web.Serialization;
1616
using Intersect.Server.Web.Swagger.Filters;
@@ -32,6 +32,7 @@
3232
using Microsoft.IdentityModel.Tokens;
3333
using Microsoft.OpenApi.Models;
3434
using Newtonsoft.Json.Converters;
35+
using Intersect.Server.Collections.Indexing;
3536
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
3637

3738
namespace Intersect.Server.Web;
@@ -251,10 +252,10 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
251252
options.TokenValidationParameters.ValidIssuer ??= tokenGenerationOptions.Issuer;
252253
options.Events = new JwtBearerEvents
253254
{
254-
OnAuthenticationFailed = async _ => {},
255-
OnChallenge = async _ => {},
256-
OnMessageReceived = async _ => {},
257-
OnTokenValidated = async _ => {},
255+
OnAuthenticationFailed = async _ => { },
256+
OnChallenge = async _ => { },
257+
OnMessageReceived = async _ => { },
258+
OnTokenValidated = async _ => { },
258259
};
259260
SymmetricSecurityKey issuerKey = new(tokenGenerationOptions.SecretData);
260261
options.TokenValidationParameters.IssuerSigningKey = issuerKey;

0 commit comments

Comments
 (0)