Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor reverse proxy for accessing WebAPIs #11

Closed
Tracked by #6
YuriyDurov opened this issue May 27, 2024 · 3 comments
Closed
Tracked by #6

Blazor reverse proxy for accessing WebAPIs #11

YuriyDurov opened this issue May 27, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@YuriyDurov
Copy link
Member

YuriyDurov commented May 27, 2024

  • Add an ability to reverse proxy requests made to Blazor Server's {host}/api to arbitrary WebAPIs.
  • Expose configuration func to allow intercept and update outbound requests to said WebAPI (attach bearer tokens, implement custom logic, etc.)
@YuriyDurov YuriyDurov mentioned this issue May 27, 2024
4 tasks
@YuriyDurov YuriyDurov added this to the Blazor.Auth - v1 milestone May 27, 2024
@Hantse
Copy link

Hantse commented May 28, 2024

Hello,

Probably link to this, it's possible to attach bearer in HTTP Client ? Custom handler ?

Kr,

@YuriyDurov
Copy link
Member Author

@Hantse Hello

Currently (package version 0.*) you can access your user's bearer token as one of the claims provided in the user's AuthenticationState. In other words, if you inject the AuthenticationStateProvider somewhere in your app and then call it's GetAuthenticationStateAsync method, you should be able to get the user's bearer token from the AuthenticationState object it returns.

But we are planning on changing this in version 1.*. We are planning on removing the ability of client-side code to access the JwtPair in any way. The Jwt data should be stored as an HttpOnly (and, optionally, Secure) Cookie in the user's browser. This is necessary in order to counteract the potential for XSS vulnerabilities. This will be covered in more detail here: #6.

Blazor already provides some protection from cross-site scripting, but allowing the client part of your app unrestricted access to the user's JwtPair can still be dangerous.

This issue here is about allowing authorized http requests without accessing the JwtPair on the client device. The idea is that the Server part of your Blazor app should provide a configurable reverse proxy for accessing necessary WebAPIs. In other words, it should act as a configurable middleware between the user and an external WebAPI that the user needs to access. It should also be doing necessary manipulations on the users' requests, like appending bearer tokens to the outbound part of the request.

I suggest you to keep an eye on upcoming updates, especially regarding changes between 0.* and 1.*, as this migration will likely require some adjustments to your solution's code.

We are doing this to ensure the extra safety of your users' data.

Hope this brings some additional clarity on this aspect of the package.

@Hantse
Copy link

Hantse commented May 28, 2024

Hello !

Thx for feedback !

I've do that's as "workaround" :

 public class ApplicationAuthorizationMessageHandler(ICookieService cookieService)
     : DelegatingHandler
 {
     protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     {
         var token = await cookieService.GetAsync("AccessToken");
         if (token != null)
         {
             request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {token.Value}");
         }

         return await base.SendAsync(request, cancellationToken);
     }
 }

But it's possible better :

app.MapForwarder("/weather-forecast", "https://weatherapi", transformBuilder =>
{
    transformBuilder.AddRequestTransform(async transformContext =>
    {
        var accessToken = await transformContext.HttpContext.GetTokenAsync("access_token");
        transformContext.ProxyRequest.Headers.Authorization = new("Bearer", accessToken);
    });
}).RequireAuthorization();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants