Skip to content

Commit 507a7d1

Browse files
committed
Fetch access token from query path parameter on web socket connections
1 parent 25e316e commit 507a7d1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

backend/api/Program.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@
148148
.AddDownstreamApi(InspectionService.ServiceName, builder.Configuration.GetSection("SARA"))
149149
.AddDownstreamApi(IsarService.ServiceName, builder.Configuration.GetSection("Isar"));
150150

151+
builder.Services.Configure<JwtBearerOptions>(
152+
JwtBearerDefaults.AuthenticationScheme,
153+
options =>
154+
{
155+
options.Events ??= new JwtBearerEvents();
156+
options.Events.OnMessageReceived = context =>
157+
{
158+
if (
159+
context.HttpContext.Request.Path.StartsWithSegments("/hub")
160+
&& context.Request.Query.TryGetValue("access_token", out var token)
161+
)
162+
{
163+
context.Token = token;
164+
}
165+
return Task.CompletedTask;
166+
};
167+
}
168+
);
169+
151170
builder
152171
.Services.AddAuthorizationBuilder()
153172
.AddFallbackPolicy("RequireAuthenticatedUser", policy => policy.RequireAuthenticatedUser());

0 commit comments

Comments
 (0)