|
|
@@ -1,5 +1,7 @@
|
|
|
using Fido2NetLib;
|
|
|
+using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.IdentityModel.Tokens;
|
|
|
using Passwordless;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
@@ -15,6 +17,22 @@ builder.Services.AddFido2(options =>
|
|
|
options.Origins = ["http://localhost:5172"];
|
|
|
options.TimestampDriftTolerance = 300000;
|
|
|
});
|
|
|
+builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
+ .AddJwtBearer(options =>
|
|
|
+ {
|
|
|
+ options.RequireHttpsMetadata = false; // dev only!!!
|
|
|
+ options.Authority = "http://localhost:5172";
|
|
|
+ options.TokenValidationParameters = new TokenValidationParameters
|
|
|
+ {
|
|
|
+ ValidIssuer = "http://localhost:5172",
|
|
|
+ ValidAudience = "http://localhost:5172"
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+builder.Services.AddAuthorization(authorizationOptions =>
|
|
|
+{
|
|
|
+ authorizationOptions.AddPolicy("MagicClaim", policyBuilder => policyBuilder.RequireClaim("permissions", "MagicClaim"));
|
|
|
+});
|
|
|
builder.Services.AddMemoryCache();
|
|
|
builder.Services.AddTransient<OptionsCache>();
|
|
|
builder.Services.AddTransient<CredentialManager>();
|
|
|
@@ -30,6 +48,7 @@ if (app.Environment.IsDevelopment())
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
app.UseHttpsRedirection();
|
|
|
+app.UseAuthorization();
|
|
|
|
|
|
app.MapGet("/buildCredentialOptions", ([FromQuery] string login, CredentialManager credMan) =>
|
|
|
credMan.BuildCredentialOptions(login))
|
|
|
@@ -51,4 +70,9 @@ app.MapPost("/verifyCredential", async ([FromBody] AuthenticatorAssertionRawResp
|
|
|
.WithName("VerifyCredential")
|
|
|
.WithOpenApi();
|
|
|
|
|
|
+app.MapGet("/protected", () => "Success!").WithName("Protected").RequireAuthorization(policy =>
|
|
|
+{
|
|
|
+ policy.RequireClaim("MagicClaim");
|
|
|
+});
|
|
|
+
|
|
|
app.Run();
|