Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashendrickx committed Jun 5, 2024
1 parent 592d3f2 commit 369dfd9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@using Passwordless.AdminConsole.Services.PasswordlessManagement
@using Passwordless.Common.Constants
@using Passwordless.Common.EventLog.Enums
@using Passwordless.Common.Extensions
@using Passwordless.Common.Models.Apps

@inherits BaseApplicationPage
Expand All @@ -19,29 +18,25 @@
@inject ILogger<CreatePublicKey> Logger

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

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

public int OrganizationId { get; private set; }

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

public async Task OnCreatedAsync(List<string> selectedScopes)
public async Task OnCreatedAsync(HashSet<PublicKeyScopes> selectedScopes)
{
var selectedScopeValues = selectedScopes
.Select(x => x.AsPublicKeyScope())
.ToHashSet();

try
{
var request = new CreatePublicKeyRequest(selectedScopeValues);
var request = new CreatePublicKeyRequest(selectedScopes);
await ManagementClient.CreateApiKeyAsync(AppId, request);

var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@using Passwordless.AdminConsole.Services.PasswordlessManagement
@using Passwordless.Common.Constants
@using Passwordless.Common.EventLog.Enums
@using Passwordless.Common.Extensions
@using Passwordless.Common.Models.Apps

@inherits BaseApplicationPage
Expand All @@ -20,30 +19,26 @@
@inject ILogger<CreatePublicKey> Logger

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

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

public int OrganizationId { get; private set; }

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

public async Task OnCreatedAsync(List<string> selectedScopes)
public async Task OnCreatedAsync(HashSet<SecretKeyScopes> selectedScopes)
{
var selectedScopeValues = selectedScopes
.Select(x => x.AsSecretKeyScope())
.ToHashSet();

string? encodedApiKey;
try
{
var request = new CreateSecretKeyRequest(selectedScopeValues);
var request = new CreateSecretKeyRequest(selectedScopes);
var response = await ManagementClient.CreateApiKeyAsync(AppId, request);

var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@typeparam TScope where TScope : struct, IConvertible

<Panel>
<EditForm class="flex flex-col space-y-4" FormName="@FormName" Model="FormModel" OnValidSubmit="@OnValidSubmitAsync">
<h3>Scopes</h3>
Expand All @@ -21,10 +23,10 @@
private const string FormName = "create-api-key-form";

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

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

[SupplyParameterFromForm(FormName = FormName)]
public CreateApiKeyFormModel? FormModel { get; set; }
Expand All @@ -42,16 +44,16 @@

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

public class CreateApiKeyFormModel
{
public List<string> SelectedScopes { get; set; } = new();
public List<TScope> SelectedScopes { get; set; } = new();
}
}

0 comments on commit 369dfd9

Please sign in to comment.