BotSharp/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs
2024-02-14 17:45:28 -06:00

29 lines
817 B
C#

using Microsoft.AspNetCore.Http;
namespace BotSharp.Plugin.ChatHub;
public class WebSocketsMiddleware
{
private readonly RequestDelegate _next;
public WebSocketsMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
var request = httpContext.Request;
// web sockets cannot pass headers so we must take the access token from query param and
// add it to the header before authentication middleware runs
if (request.Path.StartsWithSegments("/chatHub", StringComparison.OrdinalIgnoreCase) &&
request.Query.TryGetValue("access_token", out var accessToken))
{
request.Headers["Authorization"] = $"Bearer {accessToken}";
}
await _next(httpContext);
}
}