Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure project, implement fake server #12

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using app.Models;
using DiscordBot.Models;
using Refit;

namespace app.Apis;
namespace DiscordBot.Apis;

public interface IServerDiscordApi
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using app.Apis;
using app.Services;
using DiscordBot.Apis;
using DiscordBot.Services;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Serilog;

namespace app.Commands;
namespace DiscordBot.Commands;

public class GeneralCommands : ApplicationCommandModule
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using app.Apis;
using app.Services;
using DiscordBot.Apis;
using DiscordBot.Services;
using DSharpPlus.Entities;
using Serilog;

namespace app.Commands;
namespace DiscordBot.Commands;
using DSharpPlus.SlashCommands;

public class RestartCommands : ApplicationCommandModule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using app.Models;
using app.Services;
using DiscordBot.Models;
using DiscordBot.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers;
namespace DiscordBot.Controllers;

[ApiController]
[Authorize]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using app.Services;
using DiscordBot.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers;
namespace DiscordBot.Controllers;

[ApiController]
[Authorize]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using app.Services;
using DiscordBot.Services;
using DSharpPlus.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers;
namespace DiscordBot.Controllers;

[ApiController]
[Authorize]
Expand Down
4 changes: 4 additions & 0 deletions app.csproj → DiscordBot.App/DiscordBot.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordBot.Data\DiscordBot.Data.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using app.Models;
using DiscordBot.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;

namespace app.Middlewares;
namespace DiscordBot.Middlewares;

public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
Expand Down
13 changes: 6 additions & 7 deletions Program.cs → DiscordBot.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using app.Services;
using app.Settings;
using app;
using app.Models;
using app.Middlewares;
using DiscordBot.Services;
using DiscordBot.Settings;
using DiscordBot;
using DiscordBot.Models;
using DiscordBot.Middlewares;
using Microsoft.AspNetCore.Authentication;
using Serilog;

Expand All @@ -17,8 +17,6 @@
var serverApiSettings = new ServerApiSettings();
builder.Configuration.GetSection(nameof(ServerApiSettings)).Bind(serverApiSettings);



builder.Services.Configure<DiscordSettings>(options => builder.Configuration.GetSection(nameof(DiscordSettings)).Bind(options));
builder.Services.Configure<ServerApiSettings>(options => builder.Configuration.GetSection(nameof(ServerApiSettings)).Bind(options));
builder.Services.Configure<Credentials>(options => builder.Configuration.GetSection(nameof(Credentials)).Bind(options));
Expand All @@ -31,6 +29,7 @@
builder.Services.AddSingleton<RunnerService>();
builder.Services.AddSingleton<GuildJoinService>();
builder.Services.AddHostedService(provider => provider.GetService<RunnerService>());

builder.Services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

Expand Down
6 changes: 3 additions & 3 deletions ServiceExtensions.cs → DiscordBot.App/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using app.Apis;
using app.Settings;
using DiscordBot.Apis;
using DiscordBot.Settings;
using Microsoft.OpenApi.Models;
using Refit;
using System.Net.Http.Headers;
using System.Text;

namespace app;
namespace DiscordBot;

public static class ServiceExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using app.Settings;
using DiscordBot.Settings;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using app.Commands;
using DiscordBot.Commands;
using Serilog;
using DSharpPlus.EventArgs;
using Microsoft.Extensions.Options;
using DSharpPlus.SlashCommands;

namespace app.Services;
namespace DiscordBot.Services;

public class DiscordService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using app.Apis;
using DiscordBot.Apis;
using DSharpPlus;
using DSharpPlus.EventArgs;

namespace app.Services;
namespace DiscordBot.Services;

public class GuildJoinService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using app.Apis;
using DiscordBot.Apis;
using DSharpPlus;
using DSharpPlus.EventArgs;

namespace app.Services;
namespace DiscordBot.Services;

public class LoginService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using app.Apis;
using DiscordBot.Apis;
using DSharpPlus.Entities;
using Serilog;

namespace app.Services;
namespace DiscordBot.Services;

public class RunnerService : BackgroundService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace app.Settings;
namespace DiscordBot.Settings;

public class Roles
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace app.Settings;
namespace DiscordBot.Settings;

public class ServerApiSettings
{
public string Url { get; set; }

Check warning on line 5 in DiscordBot.App/Settings/ServerApiSettings.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Url' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Username { get; set; }
public string Password { get; set; }
}
2 changes: 1 addition & 1 deletion appsettings.json → DiscordBot.App/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"Password": "your_password"
},
"ServerApiSettings": {
"Url": "http://localhost:9696/api/",
"Url": "http://localhost:9697/",
"Username": "admin",
"Password": "test"
},
Expand Down
9 changes: 9 additions & 0 deletions DiscordBot.Data/DiscordBot.Data.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace app.Models;
namespace DiscordBot.Models;

public class Credentials
{
public string Username { get; set; }

Check warning on line 5 in DiscordBot.Data/Models/Credentials.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Password { get; set; }

Check warning on line 6 in DiscordBot.Data/Models/Credentials.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace app.Models;
namespace DiscordBot.Models;

public class DiscordUserDto
{
public string Id { get; set; }

Check warning on line 5 in DiscordBot.Data/Models/DiscordUserDto.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Username { get; set; }

Check warning on line 6 in DiscordBot.Data/Models/DiscordUserDto.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Username' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Discriminator { get; set; }

Check warning on line 7 in DiscordBot.Data/Models/DiscordUserDto.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Discriminator' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string AvatarUrl { get; set; }

Check warning on line 8 in DiscordBot.Data/Models/DiscordUserDto.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'AvatarUrl' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace app.Models;
namespace DiscordBot.Models;

public class ResponsePairDto
{
public ResponsePairDto()

Check warning on line 5 in DiscordBot.Data/Models/ResponsePairDto.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Message' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
}

Expand Down
74 changes: 74 additions & 0 deletions DiscordBot.FakeServer/Controllers/ServerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using DiscordBot.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DiscordBot.FakeServer.Controllers;

[ApiController]
[Authorize]
[Route("[action]")]
public class ServerController : ControllerBase
{
[HttpPost("/remove-read-only")]
public bool PostRemoveReadOnly(string channel, ulong senderId, string senderName, ulong targetId, string targetName)
{
return true;
}

[HttpPost("/read-only")]
public bool PostReadOnly(string channel, ulong senderId, string senderName, ulong targetId, string targetName, string reason)
{
return true;
}

[HttpGet("/apps")]
public string GetApps()
{
return "Currently no apps...";
}

[HttpGet("/account-status")]
public string GetAccountStatus(ulong id)
{
return "[]";
}

[HttpPost("/announce")]
public void PostAnnouncement(string text)
{
}

[HttpPost("/save")]
public bool PostSave()
{
return true;
}

[HttpGet("/ping")]
public bool GetPing()
{
return true;
}

[HttpPost("/kick")]
public bool PostKick(string channel, ulong senderId, string senderName, ulong targetId, string targetName, string reason)
{
return true;
}

[HttpPost("/ban")]
public bool PostBan(string channel, ulong senderId, string senderName, ulong targetId, string targetName, string reason)
{
return true;
}

[HttpPost("/login")]
public ResponsePairDto PostLogin(string name, ulong id, string username, string discriminator, string avatarurl, string password)
{
return new ResponsePairDto()
{
Message = "Success",
Status = true
};
}
}
18 changes: 18 additions & 0 deletions DiscordBot.FakeServer/DiscordBot.FakeServer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordBot.Data\DiscordBot.Data.csproj" />
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions DiscordBot.FakeServer/Middlewares/BasicAuthenticationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using DiscordBot.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;

namespace DiscordBot.Middlewares;

public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IConfiguration _configuration;
private readonly Credentials _credentials;

public BasicAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
IOptions<Credentials> credentials,
IConfiguration configuration) : base(options, logger, encoder, clock)
{
_configuration = configuration;
_credentials = credentials.Value;
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()

Check warning on line 27 in DiscordBot.FakeServer/Middlewares/BasicAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
string authHeader = Request.Headers["Authorization"];

Check warning on line 29 in DiscordBot.FakeServer/Middlewares/BasicAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
if (authHeader != null && authHeader.StartsWith("Basic", StringComparison.InvariantCultureIgnoreCase))
{

var authHeaderValue = authHeader.Replace("Basic ", "", StringComparison.InvariantCultureIgnoreCase);
var decodedAuthHeaderValue = Encoding.UTF8.GetString(Convert.FromBase64String(authHeaderValue));
var userPassArray = decodedAuthHeaderValue.Split(":");
var extractedUsername = userPassArray[0];
var extractedPassword = userPassArray[1];

if (string.Equals(_credentials.Username, extractedUsername) && string.Equals(extractedPassword, _credentials.Password))
{
var claims = new[] { new Claim(ClaimTypes.Name, _credentials.Username) };
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);

return AuthenticateResult.Success(ticket);
}
}
return AuthenticateResult.Fail("Failed to authenticate");
}
}
Loading