mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-02 21:34:05 +02:00
104 lines
3.4 KiB
Plaintext
104 lines
3.4 KiB
Plaintext
@page "/setup"
|
|
@using Phantom.Common.Data
|
|
@using Phantom.Common.Data.Web.Users
|
|
@using Phantom.Common.Data.Web.Users.CreateOrUpdateAdministratorUserResults
|
|
@using Phantom.Common.Messages.Web.ToController
|
|
@using Phantom.Utils.Cryptography
|
|
@using Phantom.Web.Services
|
|
@using Phantom.Web.Services.Authentication
|
|
@using Phantom.Web.Services.Rpc
|
|
@using System.ComponentModel.DataAnnotations
|
|
@using System.Security.Cryptography
|
|
@attribute [AllowAnonymous]
|
|
@inject ApplicationProperties ApplicationProperties
|
|
@inject UserLoginManager LoginManager
|
|
@inject ControllerConnection ControllerConnection
|
|
|
|
<h1>Administrator Setup</h1>
|
|
|
|
<Form Model="form" OnSubmit="DoLogin">
|
|
<div style="max-width: 400px;">
|
|
<div class="row">
|
|
<div class="mb-3">
|
|
<FormTextInput Id="account-username" Label="Username" @bind-Value="form.Username" autocomplete="off" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="mb-3">
|
|
<FormTextInput Id="account-password" Label="Password" Type="FormTextInputType.Password" @bind-Value="form.Password" autocomplete="new-password" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="mb-3">
|
|
<FormTextInput Id="administration-token" Label="Administration Token" Type="FormTextInputType.Password" @bind-Value="form.AdministrationToken" autocomplete="off" />
|
|
</div>
|
|
</div>
|
|
|
|
<FormButtonSubmit Label="Continue" class="btn btn-primary" />
|
|
</div>
|
|
|
|
<FormSubmitError />
|
|
</Form>
|
|
|
|
@code {
|
|
|
|
private readonly CreateAdministratorAccountFormModel form = new();
|
|
|
|
private sealed class CreateAdministratorAccountFormModel : FormModel {
|
|
[Required]
|
|
public string Username { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public string Password { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public string AdministrationToken { get; set; } = string.Empty;
|
|
}
|
|
|
|
private async Task DoLogin(EditContext context) {
|
|
await form.SubmitModel.StartSubmitting();
|
|
|
|
if (!IsAdministratorTokenValid()) {
|
|
form.SubmitModel.StopSubmitting("Invalid administrator token.");
|
|
return;
|
|
}
|
|
|
|
var createOrUpdateAdministratorResult = await CreateOrUpdateAdministrator();
|
|
if (createOrUpdateAdministratorResult.TryGetError(out var error)) {
|
|
form.SubmitModel.StopSubmitting(error);
|
|
return;
|
|
}
|
|
|
|
var signInResult = await LoginManager.LogIn(form.Username, form.Password);
|
|
if (!signInResult) {
|
|
form.SubmitModel.StopSubmitting("Error logging in.");
|
|
}
|
|
}
|
|
|
|
private bool IsAdministratorTokenValid() {
|
|
byte[] formTokenBytes;
|
|
try {
|
|
formTokenBytes = TokenGenerator.GetBytesOrThrow(form.AdministrationToken);
|
|
} catch (Exception) {
|
|
return false;
|
|
}
|
|
|
|
return CryptographicOperations.FixedTimeEquals(formTokenBytes, ApplicationProperties.AdministratorToken);
|
|
}
|
|
|
|
private async Task<Result<string>> CreateOrUpdateAdministrator() {
|
|
var reply = await ControllerConnection.Send<CreateOrUpdateAdministratorUserMessage, CreateOrUpdateAdministratorUserResult>(new CreateOrUpdateAdministratorUserMessage(form.Username, form.Password), Timeout.InfiniteTimeSpan);
|
|
return reply switch {
|
|
Success => Result.Ok,
|
|
CreationFailed fail => fail.Error.ToSentences("\n"),
|
|
UpdatingFailed fail => fail.Error.ToSentences("\n"),
|
|
AddingToRoleFailed => "Could not assign administrator role to user.",
|
|
null => "Timed out.",
|
|
_ => "Unknown error."
|
|
};
|
|
}
|
|
|
|
}
|