Skip to content

Commit 6c09a53

Browse files
Erwinvandervalkmaartenba
authored andcommitted
BFF v4 updates
1 parent 8d88ee5 commit 6c09a53

File tree

16 files changed

+108
-39
lines changed

16 files changed

+108
-39
lines changed

src/content/docs/bff/architecture/index.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ functions:
2222
* Server-side Token Management
2323
* Blazor support with unified authentication state management across rendering modes.
2424

25+
## Authentiation flow
26+
27+
The following diagram shows how the BFF protects browser based applications.
28+
2529
![BFF Security Framework Architecture Overview](../images/bff_application_architecture.svg)
2630

31+
32+
* **Authentication flows**: The server handles the authentication flows. There are specific endpoints for login / logout. While the browser is involved with these authentication flows, because the user is redirected to and from the identity provider, the actual browser based application will never see the authentication tokens. These are exchanged for a code on the server only.
33+
* **Cookies**: After successful authentication, a cookie is added. This cookie protects all subsequent calls to the apis. When using this type of authentication, **CSRF protection** is very important.
34+
* **Access to apis**: The BFF can expose embedded apis (which are embedded in the BFF itself) or proxy calls to remote api's (which is more common in a microservice environment). While proxying, it will exchange the authentication cookie for an access token.
35+
* **Session Management**: The BFF can manage the users session. This can either be cookie based session management or storage based session management.
36+
37+
38+
## Internals
2739
Duende.BFF builds on widely used tools and frameworks, including ASP.NET Core's OpenID Connect and cookie authentication
2840
handlers, YARP, and Duende.AccessTokenManagement. Duende.BFF combines these tools and adds additional security and
2941
application features that are useful with a BFF architecture so that you can focus on providing application logic
3042
instead of security logic:
3143

3244
![Duende BFF Security Framework - components](../images/bff_blocs.svg)
3345

34-
## ASP.NET OpenID Connect Handler
46+
### ASP.NET OpenID Connect Handler
3547

3648
Duende.BFF uses ASP.NET's OpenID Connect handler for OIDC and OAuth protocol processing. As long-term users of and
3749
contributors to this library, we think it is a well implemented and flexible implementation of the protocols.
3850

39-
## ASP.NET Cookie Handler
51+
### ASP.NET Cookie Handler
4052

4153
Duende.BFF uses ASP.NET's Cookie handler for session management. The Cookie handler provides a claims-based identity to
4254
the application persisted in a digitally signed and encrypted cookie that is protected with modern cookie security
@@ -45,28 +57,28 @@ support, and has a flexible extensibility model, which Duende.BFF uses to
4557
implement [server-side session management](/bff/fundamentals/session/server-side-sessions/)
4658
and [back-channel logout support](/bff/fundamentals/session/management/back-channel-logout/).
4759

48-
## Duende.AccessTokenManagement
60+
### Duende.AccessTokenManagement
4961

5062
Duende.BFF uses the Duende.AccessTokenManagement library for access token management and storage. This includes storage
5163
and retrieval of tokens, refreshing tokens as needed, and revoking tokens on logout. The library provides integration
5264
with the ASP.NET HTTP client to automatically attach tokens to outgoing HTTP requests, and its underlying management
5365
actions can also be programmatically invoked through an imperative API.
5466

55-
## API Endpoints
67+
### API Endpoints
5668

5769
In the BFF architecture, the frontend makes API calls to backend services via the BFF host exclusively. Typically, the
5870
BFF acts as a reverse proxy to [remote APIs](/bff/fundamentals/apis/remote), providing session and token management.
5971
Implementing local APIs within the BFF host is also [possible](/bff/fundamentals/apis/local). Regardless, requests to
6072
APIs are authenticated with the session cookie and need to be secured with an anti-forgery protection header.
6173

62-
## YARP
74+
### YARP
6375

6476
Duende.BFF proxies requests to remote APIs using Microsoft's YARP (Yet Another Reverse Proxy). You can set up YARP using
6577
a simplified developer-centric configuration API provided by Duende.BFF, or if you have more complex requirements, you
6678
can use the full YARP configuration system directly. If you are using YARP directly, Duende.BFF
6779
provides [YARP integration](/bff/fundamentals/apis/yarp) to add BFF security and identity features.
6880

69-
## UI Assets
81+
### UI Assets
7082

7183
The BFF host typically serves at least some of the UI assets of the frontend, which can be HTML/JS/CSS, WASM, and/or
7284
server-rendered content. Serving the UI assets, or at least the index page of the UI from the same origin as the backend
@@ -78,7 +90,7 @@ SameSite cookie attribute prevents the frontend from sending the authentication
7890
It is also possible to separate the BFF and UI and host them separately. See [here](/bff/architecture/ui-hosting) for
7991
more discussion of UI hosting architecture.
8092

81-
## Blazor Support
93+
### Blazor Support
8294

8395
Blazor based applications have unique challenges when it comes to authentication state. It's possible to mix various
8496
rendering models in a single application. Auto mode even starts off server rendered, then transitions to WASM when the

src/content/docs/bff/architecture/ui-hosting.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ a number of [deployment options](https://angular.io/guide/deployment) that allow
102102
assets.
103103

104104
The added complexity of this technique is justified when there is a requirement to host the front end on a different
105-
site (typically a CDN) from the BFF.
105+
site (typically a CDN) from the BFF.
106+
107+
:::note
108+
BFF V4 has built-in support for proxying the index.html from a CDN.
109+
:::
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
label: "API Endpoints"
2-
collapsed: true
2+
collapsed: true
3+
order: 100

src/content/docs/bff/fundamentals/apis/index.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ redirect_from:
1515

1616
A frontend application using the BFF pattern can call two types of APIs:
1717

18-
#### Remote APIs
18+
#### Embedded (local) APIs
1919

20-
These APIs are deployed on a different host than the BFF, which allows them to be shared between multiple frontends or (more generally speaking) multiple clients. These APIs can only be called via the BFF host acting as a proxy.
20+
These APIs embedded inside the BFF and typically exist to support the BFF's frontend; they are not shared with other frontends or services.
21+
22+
See [Embedded apis](local.mdx) for more information.
2123

22-
#### Local APIs
24+
#### Proxying to Remote APIs
25+
26+
These APIs are deployed on a different host than the BFF, which allows them to be shared between multiple frontends or (more generally speaking) multiple clients. These APIs can only be called via the BFF host acting as a proxy.
2327

24-
These APIs only exist to support the specific frontend; they are not shared with other frontends or services. They are located in the BFF host and can be called directly by the frontend.
28+
You can use [Direct Forwarding](./remote.md) for most scenarios. If you have more complex requirements, you can also directly interact with [YARP](./yarp.md)

src/content/docs/bff/fundamentals/apis/local.mdx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Local APIs"
3-
description: Documentation about Local APIs in BFF, including self-contained APIs and those using managed access tokens, along with securing endpoints and configuration details.
2+
title: "Embedded (local) APIs"
3+
description: Documentation about Embedded (Embedded) APIs in BFF, including self-contained APIs and those using managed access tokens, along with securing endpoints and configuration details.
44
sidebar:
55
order: 10
66
redirect_from:
@@ -13,28 +13,28 @@ redirect_from:
1313

1414
import { Steps } from "@astrojs/starlight/components";
1515

16-
A _Local API_ is an API located within the BFF host. Local APIs are implemented with the familiar ASP.NET abstractions of API controllers or Minimal API endpoints.
16+
An _Embedded API_ (or local api) is an API located within the BFF host. Embedded APIs are implemented with the familiar ASP.NET abstractions of API controllers or Minimal API endpoints.
1717

18-
There are two styles of local APIs:
18+
There are two styles of Embedded APIs:
1919

20-
- Self-contained Local APIs
21-
- Local APIs that Make Requests using Managed Access Tokens
20+
- Self-contained Embedded APIs
21+
- Embedded APIs that Make Requests using Managed Access Tokens
2222

23-
#### Self-Contained Local APIs
23+
#### Self-Contained Embedded APIs
2424

2525
These APIs reside within the BFF and don't make HTTP requests to other APIs. They access data controlled by the BFF itself, which can simplify the architecture of the system by reducing the number of APIs that must be deployed and managed. They are suitable for scenarios where the BFF is the sole consumer of the data. If you require data accessibility from other applications or services, this approach is probably not suitable.
2626

27-
#### Local APIs That Make Requests Using Managed Access Tokens
27+
#### Embedded APIs That Make Requests Using Managed Access Tokens
2828

29-
Alternatively, you can make the data available as a service and make HTTP requests to that service from your BFF's local endpoints. The benefits of this style of Local Endpoint include:
29+
Alternatively, you can make the data available as a service and make HTTP requests to that service from your BFF's Embedded endpoints. The benefits of this style of Embedded Endpoint include:
3030

3131
- Your frontend's network access can be simplified into an aggregated call for the specific data that it needs, which reduces the amount of data that must be sent to the client.
3232
- Your BFF endpoint can expose a subset of your remote APIs so that they are called in a more controlled manner than if the BFF proxied all requests to the endpoint.
3333
- Your BFF endpoint can include business logic to call the appropriate endpoints, which simplifies your front end code.
3434

35-
Your local endpoints can leverage services like the HTTP client factory and Duende.BFF [token management](/bff/fundamentals/tokens) to make the outgoing calls.
35+
Your Embedded endpoints can leverage services like the HTTP client factory and Duende.BFF [token management](/bff/fundamentals/tokens) to make the outgoing calls.
3636

37-
The following is a simplified example showing how local endpoints can get managed access tokens and use them to make requests to remote APIs.
37+
The following is a simplified example showing how Embedded endpoints can get managed access tokens and use them to make requests to remote APIs.
3838

3939
```csharp
4040
// MyApiController.cs
@@ -66,11 +66,11 @@ public class MyApiController : ControllerBase
6666
}
6767
```
6868

69-
The example above is simplified to demonstrate the way that you might obtain a token. Real local endpoints will typically enforce constraints on the way the API is called, aggregate multiple calls, or perform other business logic. Local endpoints that merely forward requests from the frontend to the remote API may not be needed at all. Instead, you could proxy the requests through the BFF using either the [simple http forwarder](/bff/fundamentals/apis/remote/) or [YARP](/bff/fundamentals/apis/yarp/).
69+
The example above is simplified to demonstrate the way that you might obtain a token. Real Embedded endpoints will typically enforce constraints on the way the API is called, aggregate multiple calls, or perform other business logic. Embedded endpoints that merely forward requests from the frontend to the remote API may not be needed at all. Instead, you could proxy the requests through the BFF using either the [simple http forwarder](/bff/fundamentals/apis/remote/) or [YARP](/bff/fundamentals/apis/yarp/).
7070

71-
## Securing Local API Endpoints
71+
## Securing Embedded API Endpoints
7272

73-
Regardless of the style of data access used by a local API, it must be protected against threats such as [CSRF (Cross-Site Request Forgery)](https://developer.mozilla.org/en-US/docs/Glossary/CSRF) attacks. To defend against such attacks and ensure that only the frontend can access these endpoints, we recommend implementing two layers of protection.
73+
Regardless of the style of data access used by a Embedded API, it must be protected against threats such as [CSRF (Cross-Site Request Forgery)](https://developer.mozilla.org/en-US/docs/Glossary/CSRF) attacks. To defend against such attacks and ensure that only the frontend can access these endpoints, we recommend implementing two layers of protection.
7474

7575
#### SameSite Cookies
7676

@@ -221,3 +221,7 @@ MVC controllers and actions can use the `BffApiSkipAntiforgeryAttribute` (which
221221
public class SampleApiController : ControllerBase
222222
{ /* ... */ }
223223
```
224+
225+
:::note
226+
It's also possible to disable anti-forgery protection using _BffOptions.DisableAntiForgeryCheck()_
227+
:::

src/content/docs/bff/fundamentals/apis/remote.md renamed to src/content/docs/bff/fundamentals/apis/remote.mdx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Remote APIs"
2+
title: "Proxying to Remote APIs"
33
description: Learn how to configure and secure remote API access through BFF using HTTP forwarding and token management.
44
sidebar:
55
order: 20
@@ -10,19 +10,26 @@ redirect_from:
1010
- /identityserver/v6/bff/apis/remote/
1111
- /identityserver/v7/bff/apis/remote/
1212
---
13+
import { Badge } from "@astrojs/starlight/components";
14+
import { Code } from "@astrojs/starlight/components";
15+
import { Tabs, TabItem } from "@astrojs/starlight/components";
1316

1417
A _Remote API_ is an API that is deployed separately from the BFF host. Remote APIs use access tokens to authenticate and authorize requests, but the frontend does not possess an access token to make requests to remote APIs directly. Instead, all access to remote APIs is proxied through the BFF, which authenticates the frontend using its authentication cookie, gets the appropriate access token, and forwards the request to the Remote API with the token attached.
1518

1619
There are two different ways to set up Remote API proxying in Duende.BFF. This page describes the built-in simple HTTP forwarder. Alternatively, you can integrate Duende.BFF with Microsoft's [YARP](/bff/fundamentals/apis/yarp) reverse proxy, which allows for more complex reverse proxy features provided by YARP combined with the security and identity features of Duende.BFF.
1720

18-
## Simple HTTP forwarder
21+
## Direct HTTP forwarding
1922

20-
Duende.BFF's simple HTTP forwarder maps routes in the BFF to a remote API surface. It uses [Microsoft YARP](https://github.com/microsoft/reverse-proxy) internally, but is much simpler to configure than YARP. The intent is to provide a developer-centric and simplified way to proxy requests from the BFF to remote APIs when more complex reverse proxy features are not needed.
23+
Duende.BFF's direct HTTP forwarder maps routes in the BFF to a remote API surface. It uses [Microsoft YARP](https://github.com/microsoft/reverse-proxy) internally, but is much simpler to configure than YARP. The intent is to provide a developer-centric and simplified way to proxy requests from the BFF to remote APIs when more complex reverse proxy features are not needed.
2124

2225
These routes receive automatic anti-forgery protection and integrate with automatic token management.
2326

2427
To enable this feature, add a reference to the *Duende.BFF.Yarp* NuGet package, add the remote APIs service to the service provider, and then add the remote endpoint mappings.
2528

29+
:::note
30+
The BFF multi-frontend feature has built-in support for direct forwarding.
31+
:::
32+
2633
#### Add Remote API Service to Service Provider
2734

2835
To use the HTTP forwarder, register it in the service provider:
@@ -41,11 +48,24 @@ Use the `MapRemoteBffApiEndpoint` extension method to describe how to map reques
4148

4249
The `MapRemoteBffApiEndpoint` extension method maps a path and all sub-paths below it. The intent is to allow easy mapping of groups of URLs. For example, you can set up mappings for the `/users`, `/users/{userId}`, `/users/{userId}/books`, and `/users/{userId}/books/{bookId}` endpoints without having to explicitly include all of them:
4350

51+
{/* prettier-ignore */}
52+
<Tabs syncKey="bffVersion">
53+
{/* prettier-ignore */}
54+
<TabItem label="V4">
4455
```csharp
4556
// Program.cs
4657
app.MapRemoteBffApiEndpoint("/api/users", new Uri("https://remoteHost/users"))
4758
.WithAccessToken(RequiredTokenType.User);
4859
```
60+
</TabItem>
61+
<TabItem label="V3">
62+
```csharp
63+
// Program.cs
64+
app.MapRemoteBffApiEndpoint("/api/users", new Uri("https://remoteHost/users"))
65+
.WithAccessToken(TokenType.User);
66+
```
67+
</TabItem>
68+
</Tabs>
4969

5070
:::note
5171
This example opens up the complete */users* API namespace to the frontend, and thus, to the outside world. While it is convenient to register API paths this way, consider if you need to be more specific hen designing the forwarding paths to prevent accidentally exposing unintended endpoints.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
label: "Blazor"
2-
collapsed: true
2+
collapsed: true
3+
order: 140

src/content/docs/bff/fundamentals/blazor/rendering-modes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ It's important to understand that, if you use a rendering mode that uses WebAsse
2525

2626
If you have a component that's rendered both on the server AND on the client, then you effectively need to make sure that all the services it requires are available both on the server AND on the client.
2727

28-
## Fetching Data From Local APIs
28+
## Fetching Data From Embedded APIs
2929

3030
If your BFF application can directly access data (for example from a database), then you have to decide where this information is rendered.
3131

@@ -35,11 +35,11 @@ For web assembly rendering, you'll need to make the data available via a web ser
3535

3636
When using auto-rendering mode, you'll need to make sure that the component get's a different 'data access' component for server rendering vs client rendering. Consider the following diagram:
3737

38-
![local APIs](../../images/bff_blazor_local_api.svg)
38+
![Embedded APIs](../../images/bff_blazor_local_api.svg)
3939

4040
In this diagram, you'll see the example **IDataAccessor** that has two implementations. One that accesses the data via an HTTP client (for use in WASM) and one that directly accesses the data.
4141

42-
The data is also exposed (and secured by the BFF) via a local api.
42+
The data is also exposed (and secured by the BFF) via an embedded api.
4343

4444
Below is an example of registering an abstraction
4545

@@ -68,7 +68,7 @@ internal class ServerWeatherClient() : IDataAccessor
6868
```csharp
6969
// setup on the client
7070
71-
// Register a http client that can access the data via a local api.
71+
// Register a http client that can access the data via an embedded api.
7272
builder.Services.AddLocalApiHttpClient<DataAccessHttpClient>();
7373

7474
// Register an adapter that would abstract between the data accessor and the http client.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Overview
3+
sidebar:
4+
order: 0
5+
label: Overview
6+
description: "Overview of the BFF fundamentals"
7+
date: 2024-07-21T08:22:12+02:00
8+
---
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
label: "Multiple frontends"
2+
collapsed: true
3+
order: 120

0 commit comments

Comments
 (0)