From a7492242046a381e71930738bcc676e04a6cd374 Mon Sep 17 00:00:00 2001 From: Joe DeCock Date: Mon, 3 Jul 2023 20:42:41 -0500 Subject: [PATCH 1/5] Allow optional user access tokens from yarp --- samples/JS.Yarp/Startup.cs | 12 ++++- samples/JS.Yarp/wwwroot/app.js | 15 +++++++ samples/JS.Yarp/wwwroot/index.html | 2 +- .../AccessTokenTransformProvider.cs | 44 ++++++++++++++----- src/Duende.Bff.Yarp/ProxyConfigExtensions.cs | 5 +++ src/Duende.Bff/Constants.cs | 2 + 6 files changed, 68 insertions(+), 12 deletions(-) diff --git a/samples/JS.Yarp/Startup.cs b/samples/JS.Yarp/Startup.cs index ac9ba711..8ca4bcc1 100644 --- a/samples/JS.Yarp/Startup.cs +++ b/samples/JS.Yarp/Startup.cs @@ -52,7 +52,17 @@ public void ConfigureServices(IServiceCollection services) { Path = "/anon_api/{**catch-all}" } - }.WithAntiforgeryCheck() + }.WithAntiforgeryCheck(), + new RouteConfig() + { + RouteId = "api_optional_user", + ClusterId = "cluster1", + + Match = new() + { + Path = "/optional_user_api/{**catch-all}" + } + }.WithOptionalUserAccessToken().WithAntiforgeryCheck() }, new[] { diff --git a/samples/JS.Yarp/wwwroot/app.js b/samples/JS.Yarp/wwwroot/app.js index 345db46f..e6d16062 100644 --- a/samples/JS.Yarp/wwwroot/app.js +++ b/samples/JS.Yarp/wwwroot/app.js @@ -54,6 +54,20 @@ async function callUserToken() { } } +async function callOptionalUserToken() { + var req = new Request("/optional_user_api", { + headers: new Headers({ + 'X-CSRF': '1' + }) + }) + var resp = await fetch(req); + + log("API Result: " + resp.status); + if (resp.ok) { + showApi(await resp.json()); + } +} + async function callClientToken() { var req = new Request("/client_api", { headers: new Headers({ @@ -88,6 +102,7 @@ document.querySelector(".login").addEventListener("click", login, false); document.querySelector(".logout").addEventListener("click", logout, false); document.querySelector(".call_user").addEventListener("click", callUserToken, false); +document.querySelector(".call_optional_user").addEventListener("click", callOptionalUserToken, false); document.querySelector(".call_client").addEventListener("click", callClientToken, false); document.querySelector(".call_anon").addEventListener("click", callNoToken, false); diff --git a/samples/JS.Yarp/wwwroot/index.html b/samples/JS.Yarp/wwwroot/index.html index 4a4c1be6..9d9b2057 100644 --- a/samples/JS.Yarp/wwwroot/index.html +++ b/samples/JS.Yarp/wwwroot/index.html @@ -14,9 +14,9 @@

YARP-first client