forked from davidfowl/TodoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoApi.cs
34 lines (26 loc) · 1.12 KB
/
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
30
31
32
33
34
using Microsoft.AspNetCore.Authentication;
using Yarp.ReverseProxy.Forwarder;
using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms.Builder;
namespace Todo.Web.Server;
public static class TodoApi
{
public static RouteGroupBuilder MapTodos(this IEndpointRouteBuilder routes, string todoUrl)
{
// 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();
var transformBuilder = routes.ServiceProvider.GetRequiredService<ITransformBuilder>();
var transform = transformBuilder.Create(b =>
{
b.AddRequestTransform(async c =>
{
var accessToken = await c.HttpContext.GetTokenAsync(TokenNames.AccessToken);
c.ProxyRequest.Headers.Authorization = new("Bearer", accessToken);
});
});
group.MapForwarder("{*path}", todoUrl, new ForwarderRequestConfig(), transform);
return group;
}
}