-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAS-463 | BEEEP Migrate api key creation pages (#602)
- Loading branch information
1 parent
7b4faa4
commit 5d244e0
Showing
10 changed files
with
182 additions
and
215 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/AdminConsole/Components/Pages/App/Settings/CreatePublicKey.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/AdminConsole/Components/Pages/App/Settings/CreateSecretKey.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...AdminConsole/Components/Pages/App/Settings/SettingsComponents/CreateApiKeyComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
src/AdminConsole/Pages/App/Settings/CreatePublicKey.cshtml.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
82 changes: 0 additions & 82 deletions
82
src/AdminConsole/Pages/App/Settings/CreateSecretKey.cshtml.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.