Skip to content

Commit 06af4e3

Browse files
authored
Merge pull request #294
* New BFF v4 sample for Vue
1 parent be8b390 commit 06af4e3

47 files changed

Lines changed: 7740 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BFF/v4/Vue/Vue.Api/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Vue.Api;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.Services.AddAuthentication("token")
6+
.AddJwtBearer("token", options =>
7+
{
8+
options.Authority = "https://demo.duendesoftware.com";
9+
options.Audience = "api";
10+
options.MapInboundClaims = false;
11+
});
12+
13+
builder.Services.AddAuthorization(options =>
14+
{
15+
options.AddPolicy("ApiCaller", policy =>
16+
{
17+
policy.RequireClaim("scope", "api");
18+
});
19+
20+
options.AddPolicy("InteractiveUser", policy =>
21+
{
22+
policy.RequireClaim("sub");
23+
});
24+
});
25+
26+
var app = builder.Build();
27+
28+
29+
app.UseHttpsRedirection();
30+
31+
32+
app.MapGroup("/todos")
33+
.ToDoGroup()
34+
.RequireAuthorization("ApiCaller", "InteractiveUser")
35+
.WithOpenApi();
36+
37+
app.Run();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "https://localhost:7001",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System.Security.Claims;
5+
using Microsoft.AspNetCore.Http.Extensions;
6+
7+
namespace Vue.Api;
8+
9+
public static class ToDoEndpointGroup
10+
{
11+
private static readonly List<ToDo> data = new List<ToDo>()
12+
{
13+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "2 (Bob Smith)" },
14+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "2 (Bob Smith)" },
15+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(4), Name = "Have Dinner", User = "1 (Alice Smith)" },
16+
};
17+
18+
public static RouteGroupBuilder ToDoGroup(this RouteGroupBuilder group)
19+
{
20+
// GET
21+
group.MapGet("/", () => data);
22+
group.MapGet("/{id}", (int id) =>
23+
{
24+
var item = data.FirstOrDefault(x => x.Id == id);
25+
}).WithName("todo#show");
26+
27+
// POST
28+
group.MapPost("/", (ToDo model, ClaimsPrincipal user, LinkGenerator links) =>
29+
{
30+
model.Id = ToDo.NewId();
31+
model.User = $"{user.FindFirst("sub")?.Value} ({user.FindFirst("name")?.Value})";
32+
33+
data.Add(model);
34+
35+
var url = links.GetPathByName("todo#show", new { id = model.Id });
36+
return Results.Created(url, model);
37+
});
38+
39+
// PUT
40+
group.MapPut("/{id}", (int id, ToDo model, ClaimsPrincipal User) =>
41+
{
42+
var item = data.FirstOrDefault(x => x.Id == id);
43+
if (item == null) return Results.NotFound();
44+
45+
item.Date = model.Date;
46+
item.Name = model.Name;
47+
48+
return Results.NoContent();
49+
});
50+
51+
// DELETE
52+
group.MapDelete("/{id}", (int id) =>
53+
{
54+
var item = data.FirstOrDefault(x => x.Id == id);
55+
if (item == null) return Results.NotFound();
56+
57+
data.Remove(item);
58+
59+
return Results.NoContent();
60+
});
61+
62+
return group;
63+
}
64+
}
65+
66+
public class ToDo
67+
{
68+
static int _nextId = 1;
69+
public static int NewId()
70+
{
71+
return _nextId++;
72+
}
73+
74+
public int Id { get; set; }
75+
public DateTimeOffset Date { get; set; }
76+
public string? Name { get; set; }
77+
public string? User { get; set; }
78+
}

BFF/v4/Vue/Vue.Api/Vue.Api.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

BFF/v4/Vue/Vue.Bff/Program.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Duende.Bff;
2+
using Duende.Bff.DynamicFrontends;
3+
using Duende.Bff.Yarp;
4+
using Vue.Api;
5+
6+
var builder = WebApplication.CreateBuilder(args);
7+
8+
builder.Services.AddBff()
9+
.AddRemoteApis()
10+
.ConfigureOpenIdConnect(options =>
11+
{
12+
options.Authority = "https://demo.duendesoftware.com";
13+
options.ClientId = "interactive.confidential";
14+
options.ClientSecret = "secret";
15+
options.ResponseType = "code";
16+
options.ResponseMode = "query";
17+
18+
options.GetClaimsFromUserInfoEndpoint = true;
19+
options.MapInboundClaims = false;
20+
options.SaveTokens = true;
21+
22+
options.Scope.Clear();
23+
options.Scope.Add("openid");
24+
options.Scope.Add("profile");
25+
options.Scope.Add("api");
26+
options.Scope.Add("offline_access");
27+
28+
options.TokenValidationParameters = new()
29+
{
30+
NameClaimType = "name",
31+
RoleClaimType = "role"
32+
};
33+
})
34+
.ConfigureCookies(options =>
35+
{
36+
options.Cookie.Name = "__Host-bff";
37+
options.Cookie.SameSite = SameSiteMode.Strict;
38+
});
39+
40+
builder.Services.AddAuthentication(options =>
41+
{
42+
options.DefaultScheme = BffAuthenticationSchemes.BffCookie;
43+
options.DefaultChallengeScheme = BffAuthenticationSchemes.BffOpenIdConnect;
44+
options.DefaultSignOutScheme = BffAuthenticationSchemes.BffOpenIdConnect;
45+
});
46+
47+
builder.Services.AddAuthorization();
48+
49+
var app = builder.Build();
50+
51+
app.UseDefaultFiles();
52+
app.UseStaticFiles();
53+
54+
app.UseHttpsRedirection();
55+
app.UseAuthentication();
56+
app.UseBff();
57+
app.UseAuthorization();
58+
app.MapBffManagementEndpoints();
59+
60+
app.MapGroup("/todos")
61+
.ToDoGroup()
62+
.RequireAuthorization()
63+
.AsBffApiEndpoint();
64+
65+
// Comment this in to use the external api
66+
// app.MapRemoteBffApiEndpoint("/todos", "https://localhost:7001/todos")
67+
// .RequireAccessToken(Duende.Bff.TokenType.User);
68+
69+
70+
app.Run();
71+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:6001",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
12+
}
13+
}
14+
}
15+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using System.Security.Claims;
5+
using Microsoft.AspNetCore.Http.Extensions;
6+
7+
namespace Vue.Api;
8+
9+
public static class ToDoEndpointGroup
10+
{
11+
private static readonly List<ToDo> data = new List<ToDo>()
12+
{
13+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow, Name = "Demo ToDo API", User = "2 (Bob Smith)" },
14+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(1), Name = "Stop Demo", User = "2 (Bob Smith)" },
15+
new ToDo { Id = ToDo.NewId(), Date = DateTimeOffset.UtcNow.AddHours(4), Name = "Have Dinner", User = "1 (Alice Smith)" },
16+
};
17+
18+
public static RouteGroupBuilder ToDoGroup(this RouteGroupBuilder group)
19+
{
20+
// GET
21+
group.MapGet("/", () => data);
22+
group.MapGet("/{id}", (int id) =>
23+
{
24+
var item = data.FirstOrDefault(x => x.Id == id);
25+
}).WithName("todo#show");
26+
27+
// POST
28+
group.MapPost("/", (ToDo model, ClaimsPrincipal user, LinkGenerator links) =>
29+
{
30+
model.Id = ToDo.NewId();
31+
model.User = $"{user.FindFirst("sub")?.Value} ({user.FindFirst("name")?.Value})";
32+
33+
data.Add(model);
34+
35+
var url = links.GetPathByName("todo#show", new { id = model.Id });
36+
return Results.Created(url, model);
37+
});
38+
39+
// PUT
40+
group.MapPut("/{id}", (int id, ToDo model, ClaimsPrincipal User) =>
41+
{
42+
var item = data.FirstOrDefault(x => x.Id == id);
43+
if (item == null) return Results.NotFound();
44+
45+
item.Date = model.Date;
46+
item.Name = model.Name;
47+
48+
return Results.NoContent();
49+
});
50+
51+
// DELETE
52+
group.MapDelete("/{id}", (int id) =>
53+
{
54+
var item = data.FirstOrDefault(x => x.Id == id);
55+
if (item == null) return Results.NotFound();
56+
57+
data.Remove(item);
58+
59+
return Results.NoContent();
60+
});
61+
62+
return group;
63+
}
64+
}
65+
66+
public class ToDo
67+
{
68+
static int _nextId = 1;
69+
public static int NewId()
70+
{
71+
return _nextId++;
72+
}
73+
74+
public int Id { get; set; }
75+
public DateTimeOffset Date { get; set; }
76+
public string? Name { get; set; }
77+
public string? User { get; set; }
78+
}

BFF/v4/Vue/Vue.Bff/Vue.Bff.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
<SpaRoot>../vue.client</SpaRoot>
9+
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
10+
<SpaProxyServerUrl>https://localhost:4200</SpaProxyServerUrl>
11+
<RootNamespace>Vue.Bff</RootNamespace>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Duende.BFF.Yarp" Version="4.0.2" />
16+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
17+
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="10.0.2" />
18+
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)