Skip to content
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
14 changes: 14 additions & 0 deletions BFF/v4/Angular/Angular.Api/Angular.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions BFF/v4/Angular/Angular.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Angular.Api;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication("token")
.AddJwtBearer("token", options =>
{
options.Authority = "https://demo.duendesoftware.com";
options.Audience = "api";

options.MapInboundClaims = false;
});

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

options.AddPolicy("InteractiveUser", policy =>
{
policy.RequireClaim("sub");
});
});

var app = builder.Build();

app.UseHttpsRedirection();

app.MapGroup("/todos")
.ToDoGroup()
.RequireAuthorization("ApiCaller", "InteractiveUser");


app.Run();

14 changes: 14 additions & 0 deletions BFF/v4/Angular/Angular.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
79 changes: 79 additions & 0 deletions BFF/v4/Angular/Angular.Api/ToDoEndpointGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Security.Claims;
using Microsoft.AspNetCore.Http.Extensions;

namespace Angular.Api;

public static class TodoEndpointGroup
{
private static readonly List<ToDo> data = new List<ToDo>()
{
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(4), Name = "Have Dinner", User = "1 (Alice Smith)" },
};

public static RouteGroupBuilder ToDoGroup(this RouteGroupBuilder group)
{
// GET
group.MapGet("/", () => data);
group.MapGet("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
});

// POST
group.MapPost("/", (ToDo model, ClaimsPrincipal user, HttpContext context) =>
{
model.Id = ToDo.NewId();
model.User = $"{user.FindFirst("sub")?.Value} ({user.FindFirst("name")?.Value})";

data.Add(model);

var url = new Uri($"{context.Request.GetEncodedUrl()}/{model.Id}");

return Results.Created(url, model);
});

// PUT
group.MapPut("/{id}", (int id, ToDo model, ClaimsPrincipal User) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

item.Date = model.Date;
item.Name = model.Name;

return Results.NoContent();
});

// DELETE
group.MapDelete("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

data.Remove(item);

return Results.NoContent();
});

return group;
}
}

public class ToDo
{
static int _nextId = 1;
public static int NewId()
{
return _nextId++;
}

public int Id { get; set; }
public DateTimeOffset Date { get; set; }
public string? Name { get; set; }
public string? User { get; set; }
}
8 changes: 8 additions & 0 deletions BFF/v4/Angular/Angular.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions BFF/v4/Angular/Angular.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
27 changes: 27 additions & 0 deletions BFF/v4/Angular/Angular.Bff/Angular.Bff.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<SpaRoot>..\angular.ssr</SpaRoot>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<SpaProxyServerUrl>https://localhost:4200</SpaProxyServerUrl>
<RootNamespace>Angular.Bff</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\angular.ssr\angular.ssr.esproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Duende.Bff.Yarp" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
<Version>10.0.2</Version>
</PackageReference>
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions BFF/v4/Angular/Angular.Bff/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Angular.Bff;
using Duende.Bff;
using Duende.Bff.DynamicFrontends;
using Duende.Bff.Yarp;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = BffAuthenticationSchemes.BffCookie;
options.DefaultChallengeScheme = BffAuthenticationSchemes.BffOpenIdConnect;
options.DefaultSignOutScheme = BffAuthenticationSchemes.BffOpenIdConnect;
});

builder.Services.AddBff()
.AddRemoteApis()
.ConfigureOpenIdConnect(options =>
{
options.Authority = "https://demo.duendesoftware.com";
options.ClientId = "interactive.confidential";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.ResponseMode = "query";

options.GetClaimsFromUserInfoEndpoint = true;
options.MapInboundClaims = false;
options.SaveTokens = true;

options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api");
options.Scope.Add("offline_access");

options.TokenValidationParameters = new()
{
NameClaimType = "name",
RoleClaimType = "role"
};
})
.ConfigureCookies(options =>
{
options.Cookie.Name = "__Host-bff";
options.Cookie.SameSite = SameSiteMode.Strict;
});

builder.Services.AddAuthorization();

var app = builder.Build();

app.UseDefaultFiles();
app.MapStaticAssets();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseBff();
app.UseAuthorization();
app.MapBffManagementEndpoints();

// Comment this out to use the external api
app.MapGroup("/todos")
.ToDoGroup()
.RequireAuthorization()
.AsBffApiEndpoint();

// Comment this in to use the external api
//app.MapRemoteBffApiEndpoint("/todos", "https://localhost:7001/todos")
// .RequireAccessToken(Duende.Bff.TokenType.User);

app.MapFallbackToFile("/index.html");

app.Run();

16 changes: 16 additions & 0 deletions BFF/v4/Angular/Angular.Bff/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:6001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
}
}

80 changes: 80 additions & 0 deletions BFF/v4/Angular/Angular.Bff/ToDoEndpointGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Security.Claims;
using Microsoft.AspNetCore.Http.Extensions;

namespace Angular.Bff;

public static class TodoEndpointGroup
{

private static readonly List<ToDo> data = new List<ToDo>()
{
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "2 (Bob Smith)" },
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(4), Name = "Have Dinner", User = "1 (Alice Smith)" },
};

public static RouteGroupBuilder ToDoGroup(this RouteGroupBuilder group)
{
// GET
group.MapGet("/", () => data);
group.MapGet("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
});

// POST
group.MapPost("/", (ToDo model, ClaimsPrincipal user, HttpContext context) =>
{
model.Id = ToDo.NewId();
model.User = $"{user.FindFirst("sub")?.Value} ({user.FindFirst("name")?.Value})";

data.Add(model);

var url = new Uri($"{context.Request.GetEncodedUrl()}/{model.Id}");

return Results.Created(url, model);
});

// PUT
group.MapPut("/{id}", (int id, ToDo model, ClaimsPrincipal User) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

item.Date = model.Date;
item.Name = model.Name;

return Results.NoContent();
});

// DELETE
group.MapDelete("/{id}", (int id) =>
{
var item = data.FirstOrDefault(x => x.Id == id);
if (item == null) return Results.NotFound();

data.Remove(item);

return Results.NoContent();
});

return group;
}
}

public class ToDo
{
static int _nextId = 1;
public static int NewId()
{
return _nextId++;
}

public int Id { get; set; }
public DateTimeOffset Date { get; set; }
public string? Name { get; set; }
public string? User { get; set; }
}
8 changes: 8 additions & 0 deletions BFF/v4/Angular/Angular.Bff/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions BFF/v4/Angular/Angular.Bff/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading
Loading