Skip to content

Commit 4d47a1b

Browse files
authored
Move to an API endpoint to solve connections (#818)
* Move to an API endpoint to solve connections * format * update the mds.jwt cache
1 parent f93fcc5 commit 4d47a1b

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/AdminConsole/Components/Pages/App/Settings/ManageAuthenticators.razor

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@page "/app/{AppId}/settings/authenticators/manage"
22

3+
@using Microsoft.AspNetCore.Mvc
34
@using Passwordless.Common.Models.MDS
45
@using Passwordless.AdminConsole.Services.PasswordlessManagement
56
@using Passwordless.Common.Models.Authenticators
@@ -98,18 +99,27 @@
9899
document.addEventListener("change", (event) => {
99100
if (event.target.name === "ManageForm.Selected") {
100101
let checkbox = event.target;
102+
let oldValue = checkbox.checked;
101103

102104
const formData = new FormData();
103-
formData.append("ManageForm.Selected", checkbox.value);
104-
formData.append("ManageForm.Action", checkbox.checked ? "add" : "remove");
105-
formData.append("_handler", checkbox.form["_handler"].value);
105+
formData.append("Selected", checkbox.value);
106+
formData.append("Action", checkbox.checked ? "add" : "remove");
106107
formData.append("__RequestVerificationToken", checkbox.form["__RequestVerificationToken"].value);
107108

108-
fetch(checkbox.form.action, {
109+
const appId = "@AppId";
110+
111+
fetch(`/app/${appId}/settings/authenticators/manage/api`, {
109112
method: "POST",
110-
body: formData
113+
body: formData,
114+
headers: {
115+
'X-Requested-With': 'XMLHttpRequest'
116+
}
117+
}).then(response => {
118+
if (!response.ok) {
119+
throw new Error(`HTTP ${response.status}`);
120+
}
111121
}).catch(() => {
112-
event.target.checked = !event.target.checked;
122+
event.target.checked = !oldValue;
113123
})
114124
}
115125
});
@@ -163,7 +173,7 @@
163173
_isInitialized = true;
164174
}
165175

166-
private async Task OnAuthenticatorModified()
176+
public async Task<IActionResult> OnAuthenticatorModified()
167177
{
168178
switch (ManageForm.Action)
169179
{
@@ -180,6 +190,8 @@
180190
break;
181191
}
182192
}
193+
194+
return new OkObjectResult("hello");
183195
}
184196

185197
public class FilterViewModel

src/AdminConsole/Endpoints/ComplimentaryEndpoints.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
using Microsoft.AspNetCore.Antiforgery;
12
using Microsoft.AspNetCore.Components;
23
using Microsoft.AspNetCore.Identity;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.Extensions.Options;
6+
using Passwordless.AdminConsole.Authorization;
57
using Passwordless.AdminConsole.Billing.Configuration;
68
using Passwordless.AdminConsole.Identity;
79
using Passwordless.AdminConsole.Services;
810
using Passwordless.AdminConsole.Services.MagicLinks;
11+
using Passwordless.AdminConsole.Services.PasswordlessManagement;
12+
using Passwordless.Common.Models.Authenticators;
913
using Stripe;
1014
using Stripe.Checkout;
1115
using static Microsoft.AspNetCore.Http.Results;
@@ -17,6 +21,8 @@ public static class ComplimentaryEndpoints
1721
public static void MapComplimentaryEndpoints(this IEndpointRouteBuilder builder)
1822
{
1923
builder.MapGet("/Account/Magic", AccountMagicEndpoint);
24+
builder.MapPost("/app/{appId}/settings/authenticators/manage/api", ManageAuthenticatorAsync)
25+
.RequireAuthorization(CustomPolicy.HasAppRole);
2026
}
2127

2228
/// <summary>
@@ -48,4 +54,47 @@ public static async Task<IResult> AccountMagicEndpoint(
4854
// Only allow the url to be a relative url, to prevent open redirect attacks
4955
return LocalRedirect(returnUrl);
5056
}
57+
58+
public static async Task<IResult> ManageAuthenticatorAsync(
59+
[FromRoute] string appId,
60+
[FromForm] AuthenticatorManagementRequest request,
61+
[FromServices] IScopedPasswordlessClient passwordlessClient,
62+
HttpContext context)
63+
{
64+
try
65+
{
66+
switch (request.Action?.ToLowerInvariant())
67+
{
68+
case "add":
69+
{
70+
var addRequest = new AddAuthenticatorsRequest(request.Selected, true);
71+
await passwordlessClient.AddAuthenticatorsAsync(addRequest);
72+
break;
73+
}
74+
case "remove":
75+
{
76+
var removeRequest = new RemoveAuthenticatorsRequest(request.Selected);
77+
await passwordlessClient.RemoveAuthenticatorsAsync(removeRequest);
78+
break;
79+
}
80+
default:
81+
return BadRequest(new { error = "Invalid action. Must be 'add' or 'remove'." });
82+
}
83+
84+
return Ok(new { success = true });
85+
}
86+
catch (Exception ex)
87+
{
88+
return Problem(
89+
title: "Error managing authenticator",
90+
detail: ex.Message,
91+
statusCode: 500);
92+
}
93+
}
94+
95+
public class AuthenticatorManagementRequest
96+
{
97+
public Guid[] Selected { get; set; } = Array.Empty<Guid>();
98+
public string? Action { get; set; }
99+
}
51100
}

src/Api/mds.jwt

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)