Skip to content

Commit 2aac00a

Browse files
Merge branch 'master' into dependabot/nuget/xunit.runner.visualstudio-2.5.3
2 parents b714128 + 482ddc0 commit 2aac00a

33 files changed

+419
-202
lines changed

SAPI.Auth/Auth.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using SAPI.API;
2+
3+
namespace SAPI.Auth;
4+
5+
public class Auth : IExtensionBase
6+
{
7+
public void Init()
8+
{
9+
Database.Init();
10+
}
11+
}

SAPI.Auth/Database.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using LiteDB;
2+
3+
namespace SAPI.Auth
4+
{
5+
internal static class Database
6+
{
7+
private static string dbPath;
8+
private static ILiteCollection<Identity> userCollection;
9+
public static void Init()
10+
{
11+
dbPath = Path.Combine(Directory.GetCurrentDirectory(), "SAPI.Auth.db");
12+
}
13+
14+
public static bool Insert(Identity identity)
15+
{
16+
using LiteDatabase db = new(dbPath);
17+
userCollection = db.GetCollection<Identity>("users");
18+
19+
var results = userCollection.Query()
20+
.Where(x => x.Identifier == identity.Identifier).ToList();
21+
22+
if (results.Count > 0)
23+
return false;
24+
25+
userCollection.EnsureIndex(x => x.Identifier);
26+
userCollection.Insert(identity);
27+
28+
return true;
29+
}
30+
31+
public static bool GetPassword(string identifier, out string hashedPassword)
32+
{
33+
using LiteDatabase db = new(dbPath);
34+
userCollection = db.GetCollection<Identity>("users");
35+
var results = userCollection.Query()
36+
.Where(x => x.Identifier == identifier)
37+
.Select(x => x.Password)
38+
.ToList();
39+
40+
if (results.Count > 0)
41+
{
42+
hashedPassword = results[0];
43+
return true;
44+
}
45+
46+
hashedPassword = null;
47+
return false;
48+
}
49+
}
50+
}

SAPI.Auth/Identity.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace SAPI.Auth
2+
{
3+
public sealed class Identity
4+
{
5+
public int Id { get; private set; }
6+
7+
/// <summary>
8+
/// Identifier is what your clients will use to authenticate (username, password). You can also define a username or email yourself, although it won't be used to authenticate
9+
/// </summary>
10+
public string? Identifier { get; set; }
11+
12+
public string Password
13+
{
14+
get => hashedPassword;
15+
set => password = value;
16+
}
17+
18+
private string password;
19+
private string hashedPassword;
20+
21+
public bool Create()
22+
{
23+
hashedPassword = BCrypt.Net.BCrypt.EnhancedHashPassword(password, 13);
24+
return Database.Insert(this);
25+
}
26+
27+
public bool Verify()
28+
{
29+
30+
if (Database.GetPassword(Identifier, out string hashedPassword))
31+
{
32+
return BCrypt.Net.BCrypt.EnhancedVerify(password, hashedPassword);
33+
}
34+
return false;
35+
}
36+
}
37+
}

SAPI.Auth/SAPI.Auth.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\SAPI\SAPI.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
15+
<PackageReference Include="LiteDB" Version="5.0.17" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Compile Remove="EndpointOptions.cs" />
20+
</ItemGroup>
21+
22+
</Project>

SAPI.Auth/Session.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Net;
2+
using SAPI.API.Utilities;
3+
4+
namespace SAPI.Auth
5+
{
6+
public static class Session
7+
{
8+
public static List<SessionToken> SessionTokens = new();
9+
10+
public static void GenerateSessionToken(Identity identity, Packet packet)
11+
{
12+
SessionToken token = new()
13+
{
14+
Token = Guid.NewGuid(),
15+
IdentityId = identity.Id,
16+
ValidationTime = DateTime.UtcNow
17+
};
18+
19+
Console.WriteLine(token.Token.ToString());
20+
21+
22+
23+
24+
//packet.Response.SetCookie(new Cookie("SESSION", token.Token.ToString()));
25+
// Cookies.GiveCookie(new Cookie("SESSION", token.Token.ToString()), ref packet);
26+
}
27+
}
28+
29+
public struct SessionToken
30+
{
31+
public Guid Token { get; init; }
32+
public int IdentityId { get; init; }
33+
public DateTime ValidationTime { get; init; }
34+
}
35+
}

SAPI.TestRunner/SAPI.TestRunner.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<PackageReference Include="FluentAssertions" Version="6.12.0" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
1515
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
16-
<PackageReference Include="xunit" Version="2.5.0" />
1716
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
17+
<PackageReference Include="xunit" Version="2.5.3" />
1818

1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>

SAPI.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPI.TestRunner", "SAPI.Tes
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPI.Tutorials.ServerBrowser", "SAPI.Tutorials.ServerBrowser\SAPI.Tutorials.ServerBrowser.csproj", "{8600C169-B6AA-4325-9EA9-7F8EA603EB13}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPI.Auth", "SAPI.Auth\SAPI.Auth.csproj", "{85E1C07E-C2F1-4595-B4D8-FF36E3B293FB}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{8600C169-B6AA-4325-9EA9-7F8EA603EB13}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{8600C169-B6AA-4325-9EA9-7F8EA603EB13}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{8600C169-B6AA-4325-9EA9-7F8EA603EB13}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{85E1C07E-C2F1-4595-B4D8-FF36E3B293FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{85E1C07E-C2F1-4595-B4D8-FF36E3B293FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{85E1C07E-C2F1-4595-B4D8-FF36E3B293FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{85E1C07E-C2F1-4595-B4D8-FF36E3B293FB}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

SAPI/API/Utilities/Auth.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Net;
2+
using System.Text;
23

34
namespace SAPI.API.Utilities
45
{
@@ -16,11 +17,11 @@ public static class Auth
1617
/// </summary>
1718
/// <param name="keys">List of all API keys authorized</param>
1819
/// <param name="packet">Packet ref you got from server</param>
19-
public static bool CheckForApiKey(List<string> keys, ref Packet packet)
20+
public static bool CheckForApiKey(List<string> keys, HttpListenerContext context)
2021
{
2122
try
2223
{
23-
if (GetApiKey(out string? _key, ref packet))
24+
if (GetApiKey(out string? _key, context))
2425
foreach (var key in keys)
2526
{
2627
if (_key == key)
@@ -41,11 +42,11 @@ public static bool CheckForApiKey(List<string> keys, ref Packet packet)
4142
/// <param name="credentialsList">List of all usernames and passwords authorized</param>
4243
/// <param name="hashingFunction">Password hashing algorithm. Takes un-hashed password as parameter, returns hashed password</param>
4344
/// <param name="packet">Packet ref you got from server</param>
44-
public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsList, Func<string, string> hashingFunction, ref Packet packet)
45+
public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsList, Func<string, string> hashingFunction, HttpListenerContext context)
4546
{
4647
try
4748
{
48-
if (GetBasicCredentials(out BasicCredentials? credentials, ref packet))
49+
if (GetBasicCredentials(out BasicCredentials? credentials, context))
4950
{
5051
credentials.HashedPassword = hashingFunction(credentials.Password);
5152
foreach (BasicCredentials _credentials in credentialsList)
@@ -63,9 +64,9 @@ public static bool CheckForBasicCredentials(List<BasicCredentials> credentialsLi
6364
return false;
6465
}
6566

66-
public static bool GetApiKey(out string? key, ref Packet packet)
67+
public static bool GetApiKey(out string? key, HttpListenerContext context)
6768
{
68-
key = packet.Request.Headers.Get("x-api-key");
69+
key = context.Request.Headers.Get("x-api-key");
6970

7071
if (key is null)
7172
return false;
@@ -78,14 +79,14 @@ public static bool GetApiKey(out string? key, ref Packet packet)
7879
/// </summary>
7980
/// <param name="credentials">Variable contains passed user credentials</param>
8081
/// <param name="packet">Packet ref you got from server</param>
81-
public static bool GetBasicCredentials(out BasicCredentials? credentials, ref Packet packet)
82+
public static bool GetBasicCredentials(out BasicCredentials? credentials, HttpListenerContext context)
8283
{
8384
credentials = null;
8485
try
8586
{
86-
if (packet.Request.Headers.Get("Authorization").Contains("Basic "))
87+
if (context.Request.Headers.Get("Authorization").Contains("Basic "))
8788
{
88-
string authData = packet.Request.Headers.GetValues("Authorization").GetValue(0).ToString().Substring(6);
89+
string authData = context.Request.Headers.GetValues("Authorization").GetValue(0).ToString().Substring(6);
8990

9091
byte[] decodedBase64 = Convert.FromBase64String(authData);
9192

SAPI/API/Utilities/Cookies.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class Cookies
1111
/// <param name="cookie">Return Cookie</param>
1212
/// <param name="request">Pass from Task()</param>
1313
/// <returns>Does cookie exist?</returns>
14-
public static bool CheckForCookie(string cookieName, out Cookie? cookie, ref Packet packet)
14+
public static bool CheckForCookie(string cookieName, out Cookie? cookie, HttpListenerContext context)
1515
{
16-
cookie = packet.Request.Cookies[cookieName];
16+
cookie = context.Request.Cookies[cookieName];
1717

1818
return cookie != null;
1919
}
@@ -24,9 +24,9 @@ public static bool CheckForCookie(string cookieName, out Cookie? cookie, ref Pac
2424
/// <param name="cookieName">Name of cookie to be saved</param>
2525
/// <param name="cookieValue">Value of cookie to be saved</param>
2626
/// <param name="response">Pass from Task()</param>
27-
public static void GiveCookie(Cookie cookie, ref Packet packet)
27+
public static void GiveCookie(Cookie cookie, HttpListenerContext context)
2828
{
29-
packet.Response.AppendCookie(cookie);
29+
context.Response.AppendCookie(cookie);
3030
}
3131
}
3232
}

SAPI/API/Utilities/Error.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static class Error
5151
/// </summary>
5252
/// <param name="httpStatus">It's the status code send to client</param>
5353
/// <param name="packet">Response ref you got from server - argument in Task()</param>
54-
public static void Page(HttpStatus httpStatus, ref Packet packet)
54+
public static void Page(HttpStatus httpStatus, HttpListenerContext context)
5555
{
5656
string statusName = httpStatusNames[httpStatus];
5757
int statusCode = httpStatusCodes[httpStatus];
@@ -76,7 +76,7 @@ public static void Page(HttpStatus httpStatus, ref Packet packet)
7676
" </body>" +
7777
"</html>";
7878

79-
Html.HtmlResponse(page, ref packet, statusCode);
79+
Html.HtmlResponse(page, context, statusCode);
8080
}
8181
}
8282
}

0 commit comments

Comments
 (0)