|
|
@@ -1,3 +1,4 @@
|
|
|
+using System.Security.Claims;
|
|
|
using Fido2NetLib;
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
@@ -42,13 +43,17 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
{
|
|
|
IssuerSigningKey = jwk,
|
|
|
ValidIssuer = "http://localhost:5172",
|
|
|
- ValidAudience = "http://localhost:5172"
|
|
|
+ ValidAudience = "http://localhost:5172",
|
|
|
+ NameClaimType = ClaimTypes.NameIdentifier, // important to get the "sub" claim mapped to User.Identity.Name
|
|
|
+ RoleClaimType = ClaimTypes.Role,
|
|
|
};
|
|
|
});
|
|
|
|
|
|
builder.Services.AddAuthorization(authorizationOptions =>
|
|
|
{
|
|
|
- authorizationOptions.AddPolicy("MagicClaim", policyBuilder => policyBuilder.RequireClaim("permissions", "MagicClaim"));
|
|
|
+ authorizationOptions.AddPolicy("ProtectedPolicy", policyBuilder => policyBuilder
|
|
|
+ .RequireClaim("permissions", "MagicClaim")
|
|
|
+ .RequireRole("grunt"));
|
|
|
});
|
|
|
builder.Services.AddMemoryCache();
|
|
|
builder.Services.Configure<JwtConfig>(config => config.Key = jwk);
|
|
|
@@ -88,10 +93,25 @@ app.MapPost("/verifyCredential", async ([FromBody] AuthenticatorAssertionRawResp
|
|
|
.WithName("VerifyCredential")
|
|
|
.WithOpenApi();
|
|
|
|
|
|
-app.MapGet("/protected", () => Results.Json("Success!")).WithName("Protected").RequireAuthorization(policy =>
|
|
|
-{
|
|
|
- policy.RequireAuthenticatedUser();
|
|
|
- //policy.RequireClaim("MagicClaim");
|
|
|
-});
|
|
|
+app
|
|
|
+ .MapGet("/protected", (HttpContext context) =>
|
|
|
+ {
|
|
|
+ var data = new Dictionary<string, object>
|
|
|
+ {
|
|
|
+ ["Status"] = "Success!",
|
|
|
+ ["UserName"] = context.User?.Identity?.Name ?? "<unknown>",
|
|
|
+ ["Permissions"] = context.User?.Claims.Where(c => c.Type == "permissions").Select(c => c.Value) ?? Enumerable.Empty<string>(),
|
|
|
+ ["IsAdmin"] = context.User?.IsInRole("admin"),
|
|
|
+ ["IsGrunt"] = context.User?.IsInRole("grunt"),
|
|
|
+ ["IsNoob"] = context.User?.IsInRole("noob"),
|
|
|
+ };
|
|
|
+ return data;
|
|
|
+ })
|
|
|
+ .WithName("Protected")
|
|
|
+ .RequireAuthorization("ProtectedPolicy");
|
|
|
+ // .RequireAuthorization(policy =>
|
|
|
+ // {
|
|
|
+ // policy.RequireAuthenticatedUser();
|
|
|
+ // });
|
|
|
|
|
|
app.Run();
|