Skip to content

Commit d2038bd

Browse files
committed
Improve wording on BFF remote page / access token requirements
1 parent 38ad050 commit d2038bd

File tree

1 file changed

+28
-18
lines changed
  • src/content/docs/bff/fundamentals/apis

1 file changed

+28
-18
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,65 @@ redirect_from:
1111
- /identityserver/v7/bff/apis/remote/
1212
---
1313

14-
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, obtains the appropriate access token, and forwards the request to the Remote API with the token attached.
14+
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.
1515

16-
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 reverse proxy [YARP](/bff/fundamentals/apis/yarp), which allows for more complex reverse proxy features provided by YARP combined with the security and identity features of Duende.BFF.
16+
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.
1717

1818
## Simple HTTP forwarder
1919

2020
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.
2121

2222
These routes receive automatic anti-forgery protection and integrate with automatic token management.
2323

24-
To enable this feature, add a reference to the *Duende.BFF.Yarp* NuGet package, add the remote APIs service to DI, and call the *MapRemoteBFFApiEndpoint* method to create the mappings.
24+
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.
2525

26-
#### Add Remote API Service to DI
26+
#### Add Remote API Service to Service Provider
2727

28-
```cs
28+
To use the HTTP forwarder, register it in the service provider:
29+
30+
```csharp {3}
31+
// Program.cs
2932
builder.Services.AddBff()
3033
.AddRemoteApis();
3134
```
3235

33-
3436
#### Map Remote APIs
35-
Use the *MapRemoteBffApiEndpoint* extension method to describe how to map requests coming into the BFF out to remote APIs and the *RequireAccessToken* method to specify token requirements. *MapRemoteBffApiEndpoint* takes two parameters: the base path of requests that will be mapped externally, and the address to the external API where the requests will be mapped. *MapRemoteBffApiEndpoint* 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 like this:
3637

37-
```cs
38+
Use the `MapRemoteBffApiEndpoint` extension method to describe how to map requests coming into the BFF to remote APIs.
39+
40+
`MapRemoteBffApiEndpoint` takes two parameters: the base path of requests that will be mapped externally, and the address to the external API where the requests will be mapped.
41+
42+
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:
43+
44+
```csharp
45+
// Program.cs
3846
app.MapRemoteBffApiEndpoint("/API/users", "https://remoteHost/users")
3947
.RequireAccessToken(TokenType.User);
4048
```
4149

4250
:::note
43-
This example opens up the complete */users* API namespace to the frontend and thus to the outside world. Try to be as specific as possible when designing the forwarding paths.
51+
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.
4452
:::
4553

54+
The `RequireAccessToken` method can be added to [specify token requirements](#access-token-requirements) for the remote API. The BFF will automatically forward the correct access token to the remote API, which will be scoped to the client application, the user, or either.
55+
4656
## Securing Remote APIs
57+
4758
Remote APIs typically require access control and must be protected against threats such as [CSRF (Cross-Site Request Forgery)](https://developer.mozilla.org/en-US/docs/Glossary/CSRF) attacks.
4859

4960
To provide access control, you can specify authorization policies on the mapped routes, and configure them with access token requirements.
5061

5162
To defend against CSRF attacks, you should use SameSite cookies to authenticate calls from the frontend to the BFF. As an additional layer of defense, APIs mapped with *MapRemoteBffApiEndpoint* are automatically protected with an anti-forgery header.
5263

53-
5464
#### SameSite cookies
65+
5566
[The SameSite cookie attribute](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value) is a feature of modern browsers that restricts cookies so that they are only sent to pages originating from the [site](https://developer.mozilla.org/en-US/docs/Glossary/Site) where the cookie was originally issued. This prevents CSRF attacks, because cross site requests will no longer implicitly include the user's credentials.
5667

5768
This is a good first layer of defense, but makes the assumption that you can trust all subdomains of your site. All subdomains within a registrable domain are considered the same site for purposes of SameSite cookies. Thus, if another application hosted on a subdomain within your site is infected with malware, it can make CSRF attacks against your application.
5869

59-
6070
#### Anti-forgery header
6171

62-
For this reason, remote APIs automatically require an additional custom header on API endpoints. For example:
72+
Remote APIs mapped in the BFF always require an additional custom header on API endpoints. For example:
6373

6474
```text
6575
GET /endpoint
@@ -71,25 +81,25 @@ The value of the header is not important, but its presence, combined with the co
7181

7282
#### Require authorization
7383

74-
The *MapRemoteBffApiEndpoint* method returns the appropriate type to integrate with the ASP.NET Core authorization system. You can attach authorization policies to remote endpoints using **RequireAuthorization**, just as you would for a standard ASP.NET core endpoint created with *MapGet*, and the authorization middleware will then enforce that policy before forwarding requests on that route to the remote endpoint.
84+
The `MapRemoteBffApiEndpoint` method returns the appropriate type to integrate with the ASP.NET Core authorization system. You can attach authorization policies to remote endpoints using `RequireAuthorization` extension method, just as you would for a standard ASP.NET core endpoint created with `MapGet`. The authorization middleware will then enforce that policy before forwarding requests on that route to the remote endpoint.
7585

7686
#### Access token requirements
7787

78-
Remote APIs sometimes allow anonymous access, but usually require an access token, and the type of access token (user or client) will vary as well. You can specify access token requirements via the **RequireAccessToken** extension method. Its **TokenType** parameter has three options:
88+
Remote APIs sometimes allow anonymous access, but usually require an access token, and the type of access token (user or client) will vary as well. You can specify access token requirements via the `RequireAccessToken` extension method. Its `TokenType` parameter has three options:
7989

80-
* ***User***
90+
* `User`
8191

8292
A valid user access token is required and will be forwarded to the remote API. A user access token is an access token obtained during an OIDC flow (or subsequent refresh), and is associated with a particular user. User tokens are obtained when the user initially logs in, and will be automatically refreshed using a refresh token when they expire.
8393

84-
* ***Client***
94+
* `Client`
8595

8696
A valid client access token is required and will be forwarded to the remote API. A client access token is an access token obtained through the client credentials flow, and is associated with the client application, not any particular user. Client tokens can be obtained even if the user is not logged in.
8797

88-
* ***UserOrClient***
98+
* `UserOrClient`
8999

90100
Either a valid user access token or a valid client access token (as fallback) is required and will be forwarded to the remote API.
91101

92-
You can also use the *WithOptionalUserAccessToken* extension method to specify that the API should be called with a user access token if one is available and anonymously if not.
102+
You can also use the `WithOptionalUserAccessToken` extension method to specify that the API should be called with a user access token if one is available and anonymously if not.
93103

94104
:::note
95105
These settings only specify the logic that is applied before the API call gets proxied. The remote APIs you are calling should always specify their own authorization and token requirements.

0 commit comments

Comments
 (0)