-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathTodoApi.cs
29 lines (23 loc) · 938 Bytes
/
TodoApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using Microsoft.AspNetCore.Authentication;
using Yarp.ReverseProxy.Forwarder;
using Yarp.ReverseProxy.Transforms;
namespace Todo.Web.Server;
public static class TodoApi
{
public static RouteGroupBuilder MapTodos(this IEndpointRouteBuilder routes)
{
// The todo API translates the authentication cookie between the browser the BFF into an
// access token that is sent to the todo API. We're using YARP to forward the request.
var group = routes.MapGroup("/todos");
group.RequireAuthorization();
group.MapForwarder("{*path}", "http://todoapi", new ForwarderRequestConfig(), b =>
{
b.AddRequestTransform(async c =>
{
var accessToken = await c.HttpContext.GetTokenAsync(TokenNames.AccessToken);
c.ProxyRequest.Headers.Authorization = new("Bearer", accessToken);
});
});
return group;
}
}