Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bff add aspire to samples #1710

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion bff/Duende.Bff.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# 17
# Visual Studio Version 17
VisualStudioVersion = 17.9.34414.90
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Duende.Bff", "src\Duende.Bff\Duende.Bff.csproj", "{63FD9C99-C538-44BF-9AD4-D892775E336B}"
Expand Down Expand Up @@ -61,6 +61,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Duende.Bff.Blazor.Client.Un
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Duende.Bff.Blazor.UnitTests", "test\Duende.Bff.Blazor.UnitTests\Duende.Bff.Blazor.UnitTests.csproj", "{2A04808A-A06C-4F10-87B9-2D12E065F729}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.AppHost", "samples\Hosts.AppHost\Hosts.AppHost.csproj", "{8B943A54-F50C-4946-8D0E-DA1B886F13D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.ServiceDefaults", "samples\Hosts.ServiceDefaults\Hosts.ServiceDefaults.csproj", "{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -323,6 +327,30 @@ Global
{2A04808A-A06C-4F10-87B9-2D12E065F729}.Release|x64.Build.0 = Release|Any CPU
{2A04808A-A06C-4F10-87B9-2D12E065F729}.Release|x86.ActiveCfg = Release|Any CPU
{2A04808A-A06C-4F10-87B9-2D12E065F729}.Release|x86.Build.0 = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|x64.ActiveCfg = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|x64.Build.0 = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|x86.ActiveCfg = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Debug|x86.Build.0 = Debug|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|Any CPU.Build.0 = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|x64.ActiveCfg = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|x64.Build.0 = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|x86.ActiveCfg = Release|Any CPU
{8B943A54-F50C-4946-8D0E-DA1B886F13D6}.Release|x86.Build.0 = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|x64.ActiveCfg = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|x64.Build.0 = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|x86.ActiveCfg = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Debug|x86.Build.0 = Debug|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|Any CPU.Build.0 = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x64.ActiveCfg = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x64.Build.0 = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x86.ActiveCfg = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -353,6 +381,8 @@ Global
{40EDC041-C262-414C-B374-631BF2D1BD97} = {8A0FCD30-A6D9-4622-B4D5-90010DE0795E}
{001840D4-8B83-4A8C-AF2C-5429D4F9A370} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
{2A04808A-A06C-4F10-87B9-2D12E065F729} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
{8B943A54-F50C-4946-8D0E-DA1B886F13D6} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DAD5980-4688-4794-9CF0-6F3CB67194E7}
Expand Down
3 changes: 2 additions & 1 deletion bff/Duende.Bff.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.</s:String></wpf:ResourceDictionary>
// See LICENSE in the project root for license information.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion bff/migrations/UserSessionDb/UserSessionDb.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion bff/samples/Apis/Api.DPoP/Api.DPoP.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Duende.IdentityModel" />
<PackageReference Include="Duende.AspNetCore.Authentication.JwtBearer" Version="0.1.3" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Hosts.ServiceDefaults\Hosts.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
86 changes: 86 additions & 0 deletions bff/samples/Apis/Api.DPoP/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Duende.AspNetCore.Authentication.JwtBearer.DPoP;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;

internal static class Extensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
{
var services = builder.Services;

services.AddControllers();

services.AddAuthentication("token")
.AddJwtBearer("token", options =>
{
options.Authority = "https://localhost:5001";
options.MapInboundClaims = false;

options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = false,
ValidTypes = new[] { "at+jwt" },

NameClaimType = "name",
RoleClaimType = "role"
};
});

// layers DPoP onto the "token" scheme above
services.ConfigureDPoPTokensForScheme("token");

services.AddAuthorization(options =>
{
options.AddPolicy("ApiCaller", policy =>
{
policy.RequireClaim("scope", "api");
});

options.AddPolicy("RequireInteractiveUser", policy =>
{
policy.RequireClaim("sub");
});
});
return builder.Build();

}

public static WebApplication ConfigurePipeline(this WebApplication app)
{
// The BFF sets the X-Forwarded-* headers to reflect that it
// forwarded the request here. Using the forwarded headers
// middleware here would therefore change the request's host to be
// the bff instead of this API, which is not what the DPoP
// validation code expects when it checks the htu value. If this API
// were hosted behind a load balancer, you might need to add back
// the forwarded headers middleware, or consider changing the DPoP
// proof validation.

// app.UseForwardedHeaders(new ForwardedHeadersOptions
// {
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,
// });

app.UseSerilogRequestLogging();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers()
.RequireAuthorization("ApiCaller");

return app;
}
}
82 changes: 34 additions & 48 deletions bff/samples/Apis/Api.DPoP/Program.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,44 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace Api.DPoP
Console.Title = "DPoP Api";

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();

Log.Information("Starting up");

try
{
public class Program
{
public static int Main(string[] args)
{
Console.Title = "DPoP API";
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
.CreateLogger();
builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

try
{
Log.Information("Starting host...");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly.");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
var app = builder
.ConfigureServices()
.ConfigurePipeline();

public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Unhandled exception");
}
finally
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
88 changes: 0 additions & 88 deletions bff/samples/Apis/Api.DPoP/Startup.cs

This file was deleted.

6 changes: 5 additions & 1 deletion bff/samples/Apis/Api.Isolated/Api.Isolated.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Duende.IdentityModel" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Hosts.ServiceDefaults\Hosts.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
Loading
Loading