diff --git a/PetriePanel.sln b/PetriePanel.sln index 1f6a1a5..6edcff5 100644 --- a/PetriePanel.sln +++ b/PetriePanel.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI", "WebAPI\WebAPI.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{94D0887A-5A98-4738-A15B-E191EF3DDB7E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{E23FD66C-9126-45A3-8615-9963CB74652B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Debug|Any CPU.Build.0 = Debug|Any CPU {94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.ActiveCfg = Release|Any CPU {94D0887A-5A98-4738-A15B-E191EF3DDB7E}.Release|Any CPU.Build.0 = Release|Any CPU + {E23FD66C-9126-45A3-8615-9963CB74652B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E23FD66C-9126-45A3-8615-9963CB74652B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E23FD66C-9126-45A3-8615-9963CB74652B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E23FD66C-9126-45A3-8615-9963CB74652B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Shared/APIService.cs b/Shared/APIService.cs new file mode 100644 index 0000000..6589697 --- /dev/null +++ b/Shared/APIService.cs @@ -0,0 +1,18 @@ +using System.Net.Http; +using Microsoft.Extensions.Logging; +namespace Shared +{ + public abstract class APIService + { + protected ILogger Logger { get; set; } + protected HttpClient Client { get; set; } + + protected APIService(ILogger logger, HttpClient httpClient) + { + Logger = logger; + Client = httpClient ?? new HttpClient(); + } + + + } +} \ No newline at end of file diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj new file mode 100644 index 0000000..f5178c0 --- /dev/null +++ b/Shared/Shared.csproj @@ -0,0 +1,11 @@ + + + + net5.0 + + + + + + + diff --git a/Web/Shared/Forms/ServerCreationForm.razor b/Web/Shared/Forms/ServerCreationForm.razor index 94ad991..fff3c76 100644 --- a/Web/Shared/Forms/ServerCreationForm.razor +++ b/Web/Shared/Forms/ServerCreationForm.razor @@ -1,4 +1,3 @@ -@using Web.Shared.Models diff --git a/Web/Web.csproj b/Web/Web.csproj index 4ba4a91..05e47de 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -10,4 +10,12 @@ + + + + + + + + diff --git a/Web/_Imports.razor b/Web/_Imports.razor index 048e19d..a4e606e 100644 --- a/Web/_Imports.razor +++ b/Web/_Imports.razor @@ -8,4 +8,5 @@ @using Microsoft.JSInterop @using Web @using Web.Shared -@using Web.Shared.Forms \ No newline at end of file +@using Web.Shared.Forms +@using WebAPI.Data.Dto \ No newline at end of file diff --git a/WebAPI/Controllers/AccountController.cs b/WebAPI/Controllers/AccountController.cs index a7b2a9f..5bf7b4f 100644 --- a/WebAPI/Controllers/AccountController.cs +++ b/WebAPI/Controllers/AccountController.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using WebAPI.Auth; using WebAPI.Data; using WebAPI.Data.Models; @@ -13,32 +15,45 @@ namespace WebAPI.Controllers [Authorize] [Route("api/[controller]")] [ApiController] - public class AccountController : ControllerBase + public class AccountController : BaseController { private readonly PterodactylService _pterodactylService; - private readonly AppDbContext _appDbContext; - public AccountController(PterodactylService pterodactylService, AppDbContext appDbContext) + public AccountController(IServiceProvider serviceProvider, PterodactylService pterodactylService) : base(serviceProvider) { _pterodactylService = pterodactylService; - _appDbContext = appDbContext; } [HttpGet] [Route("IsInitialized")] public async Task IsUserInitialized() { - return true; + return DbUser != default; } [HttpGet] [Route("Initialize")] - public async Task InitializeUser(string email) + public async Task InitializeUser(string email) { if (await IsUserInitialized()) { - return; + return true; } + + var name = User.FindFirst(ClaimTypes.Name)?.Value.Split(' ') ?? "No Name".Split(' '); + var firstName = name.FirstOrDefault(); + var lastName = name.ElementAt(1); + var username = User.FindFirst(OIDCClaimTypes.Username)?.Value ?? "nouser"; + var externalId = User.FindFirst(OIDCClaimTypes.Subject)?.Value ?? "nosubject"; + var pterodactylResult = await _pterodactylService.SendPterodactylUserCreate(username, email, firstName, lastName, externalId); + int clientId = pterodactylResult.Attributes.Id; + AppDbContext.Users.Add(new User + { + ExternalAuthIdentifier = externalId, + PterodactylUserId = clientId + }); + return true; } + } } \ No newline at end of file diff --git a/WebAPI/Controllers/BaseController.cs b/WebAPI/Controllers/BaseController.cs index 2e84a12..9835b61 100644 --- a/WebAPI/Controllers/BaseController.cs +++ b/WebAPI/Controllers/BaseController.cs @@ -1,13 +1,29 @@ using System; using System.Linq; using System.Security.Claims; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Net.Http.Headers; +using WebAPI.Auth; +using WebAPI.Data; +using WebAPI.Data.Models; namespace WebAPI.Controllers { public class BaseController : ControllerBase { + [Inject] + protected AppDbContext AppDbContext { get; private set; } + + public BaseController(IServiceProvider serviceProvider) + { + AppDbContext = serviceProvider.GetRequiredService(); + } + + protected User DbUser => AppDbContext.Users.FirstOrDefault(user => + user.ExternalAuthIdentifier == User.FindFirstValue(OIDCClaimTypes.Subject)); } } \ No newline at end of file diff --git a/WebAPI/Controllers/HelloWorldController.cs b/WebAPI/Controllers/HelloWorldController.cs index 6fa777a..75c14e9 100644 --- a/WebAPI/Controllers/HelloWorldController.cs +++ b/WebAPI/Controllers/HelloWorldController.cs @@ -16,17 +16,12 @@ namespace WebAPI.Controllers [ApiController] public class HelloWorldController : BaseController { - private readonly PterodactylService _pterodactylService; - - public HelloWorldController(PterodactylService pterodactylService) - { - _pterodactylService = pterodactylService; - } [HttpGet] public async Task HelloWorld() { - return JsonConvert.SerializeObject(User.Claims.Select(claim => new {claim.Type, claim.Value})); + return AppDbContext.ToString(); + //return JsonConvert.SerializeObject(User.Claims.Select(claim => new {claim.Type, claim.Value})); } [HttpGet] @@ -35,6 +30,9 @@ namespace WebAPI.Controllers { return "success"; } - + + public HelloWorldController(IServiceProvider serviceProvider) : base(serviceProvider) + { + } } } \ No newline at end of file diff --git a/Web/Shared/Models/ServerModels.cs b/WebAPI/Data/Dto/ServerModels.cs similarity index 97% rename from Web/Shared/Models/ServerModels.cs rename to WebAPI/Data/Dto/ServerModels.cs index d33476f..2b2c721 100644 --- a/Web/Shared/Models/ServerModels.cs +++ b/WebAPI/Data/Dto/ServerModels.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Text.Json.Serialization; -namespace Web.Shared.Models +namespace WebAPI.Data.Dto { public class ServerLimitsModel {