Skip to content

Commit

Permalink
PAS-463 | BEEEP Migrate api key creation pages (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashendrickx authored Jun 5, 2024
1 parent 7b4faa4 commit 5d244e0
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@page "/app/{AppId}/settings/create-public-key"

@using Passwordless.AdminConsole.Components.Pages.App.Settings.SettingsComponents
@using Passwordless.AdminConsole.EventLog.DTOs
@using Passwordless.AdminConsole.EventLog.Loggers
@using Passwordless.AdminConsole.Helpers
@using Passwordless.AdminConsole.Services.PasswordlessManagement
@using Passwordless.Common.Constants
@using Passwordless.Common.EventLog.Enums
@using Passwordless.Common.Models.Apps

@inherits BaseApplicationPage

@inject IPasswordlessManagementClient ManagementClient
@inject IHttpContextAccessor HttpContextAccessor
@inject NavigationManager NavigationManager
@inject IEventLogger EventLogger
@inject ILogger<CreatePublicKey> Logger

<Page Title="Create Public Key">
<CreateApiKeyComponent Scopes="@Scopes" OnCreateClicked="@OnCreatedAsync" TScope="PublicKeyScopes" />
</Page>

@code {
public IReadOnlyCollection<PublicKeyScopes>? Scopes { get; private set; }

public int OrganizationId { get; private set; }

protected override void OnInitialized()
{
Scopes = Enum.GetValues(typeof(PublicKeyScopes)).Cast<PublicKeyScopes>().ToArray();
OrganizationId = HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentNullException(nameof(OrganizationId));
}

public async Task OnCreatedAsync(HashSet<PublicKeyScopes> selectedScopes)
{
try
{
var request = new CreatePublicKeyRequest(selectedScopes);
await ManagementClient.CreateApiKeyAsync(AppId, request);

var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(),
EventType.AdminApiKeyCreated,
$"Created public key for application {AppId}.",
Severity.Informational,
AppId,
HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentException("The organization ID is missing."),
DateTime.UtcNow);
EventLogger.LogEvent(eventDto);
}
catch (Exception)
{
Logger.LogError("Failed to create public key for application: {appId}", AppId);
}

await InvokeAsync(() => NavigationManager.NavigateTo($"app/{AppId}/settings"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@page "/app/{AppId}/settings/create-secret-key"

@using System.Text
@using Passwordless.AdminConsole.Components.Pages.App.Settings.SettingsComponents
@using Passwordless.AdminConsole.EventLog.DTOs
@using Passwordless.AdminConsole.EventLog.Loggers
@using Passwordless.AdminConsole.Helpers
@using Passwordless.AdminConsole.Services.PasswordlessManagement
@using Passwordless.Common.Constants
@using Passwordless.Common.EventLog.Enums
@using Passwordless.Common.Models.Apps

@inherits BaseApplicationPage

@inject IPasswordlessManagementClient ManagementClient
@inject IHttpContextAccessor HttpContextAccessor
@inject NavigationManager NavigationManager
@inject IEventLogger EventLogger
@inject ILogger<CreatePublicKey> Logger

<Page Title="Create Secret Key">
<CreateApiKeyComponent Scopes="@Scopes" OnCreateClicked="@OnCreatedAsync" TScope="SecretKeyScopes" />
</Page>

@code {
public IReadOnlyCollection<SecretKeyScopes>? Scopes { get; private set; }

public int OrganizationId { get; private set; }

protected override void OnInitialized()
{
Scopes = Enum.GetValues(typeof(SecretKeyScopes)).Cast<SecretKeyScopes>().ToArray();
OrganizationId = HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentNullException(nameof(OrganizationId));
}

public async Task OnCreatedAsync(HashSet<SecretKeyScopes> selectedScopes)
{
string? encodedApiKey;
try
{
var request = new CreateSecretKeyRequest(selectedScopes);
var response = await ManagementClient.CreateApiKeyAsync(AppId, request);

var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(),
EventType.AdminApiKeyCreated,
$"Created secret key for application {AppId}.",
Severity.Informational,
AppId,
HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentException("The organization ID is missing."),
DateTime.UtcNow);
EventLogger.LogEvent(eventDto);

encodedApiKey = Base64Url.Encode(Encoding.UTF8.GetBytes(response.ApiKey));
}
catch (Exception)
{
Logger.LogError("Failed to create secret key for application: {appId}", AppId);
return;
}

await InvokeAsync(() =>
NavigationManager.NavigateTo($"app/{AppId}/settings/secret-key-created?EncodedApiKey={encodedApiKey}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected override async Task OnInitializedAsync()

private void OnCreateFormSubmitted()
{
switch (CreateForm!.Type)
switch (CreateForm.Type)
{
case "public":
NavigationManager.NavigateTo($"app/{AppId}/settings/create-public-key");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@typeparam TScope where TScope : struct, IConvertible

<Panel>
<EditForm class="flex flex-col space-y-4" FormName="@FormName" Model="FormModel" OnValidSubmit="@OnValidSubmitAsync">
<h3>Scopes</h3>
@foreach (var scope in Scopes)
{
<div>
<input type="checkbox" id="@scope" name="FormModel.SelectedScopes" value="@scope">
<label for="@scope">@scope</label>
</div>
}
<div id="create-api-key-actions">
<button id="create-api-key-btn" class="btn-primary" type="submit">Create</button>
</div>
<ValidationSummary />
<CustomValidationErrors EditContext="FormContext" />

</EditForm>
</Panel>

@code {
private const string FormName = "create-api-key-form";

[Parameter]
public required IReadOnlyCollection<TScope> Scopes { get; set; }

[Parameter]
public required EventCallback<HashSet<TScope>> OnCreateClicked { get; set; }

[SupplyParameterFromForm(FormName = FormName)]
public CreateApiKeyFormModel? FormModel { get; set; }

public EditContext? FormContext { get; set; }

public ValidationMessageStore? FormValidationMessageStore { get; set; }

protected override void OnInitialized()
{
FormModel ??= new();
FormContext = new EditContext(FormModel);
FormValidationMessageStore = new ValidationMessageStore(FormContext);
}

private async Task OnValidSubmitAsync()
{
if (!FormModel!.SelectedScopes.Any())
{
FormValidationMessageStore!.Add(() => FormModel.SelectedScopes, "Please select at least one scope.");
return;
}
await OnCreateClicked.InvokeAsync(FormModel.SelectedScopes.ToHashSet());
}

public class CreateApiKeyFormModel
{
public List<TScope> SelectedScopes { get; set; } = new();
}
}
8 changes: 0 additions & 8 deletions src/AdminConsole/Pages/App/Settings/CreatePublicKey.cshtml

This file was deleted.

79 changes: 0 additions & 79 deletions src/AdminConsole/Pages/App/Settings/CreatePublicKey.cshtml.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/AdminConsole/Pages/App/Settings/CreateSecretKey.cshtml

This file was deleted.

82 changes: 0 additions & 82 deletions src/AdminConsole/Pages/App/Settings/CreateSecretKey.cshtml.cs

This file was deleted.

Loading

0 comments on commit 5d244e0

Please sign in to comment.