OidcDeskCredential authenticates a bearer through the validation scheme only (never the default scheme; the cookie authenticates nothing on /api/desk and a bearer authenticates no cookie page) and returns sub, audience and scopes. The desk keeps judging audience, scope and per-request Active membership. The issuer registers desk.read/desk.answer and stamps audience bifrost-desk on tokens that carry one; client permissions are what limit scopes, so the MAS client can neither ask for them nor use its chat token on the desk. A Desk section without Bifrost:Oidc now fails startup in StartupConfigurationValidator. Packages: OpenIddict.Validation.AspNetCore and .ServerIntegration 7.7.1. No production Ratatoskr client is seeded. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
28 lines
1.5 KiB
C#
28 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using OpenIddict.Abstractions;
|
|
using OpenIddict.Validation.AspNetCore;
|
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
|
|
|
namespace Bifrost.Web.Features.Desk;
|
|
|
|
// The production IDeskCredential: OpenIddict's validation handler against the local server, asked for by its own
|
|
// scheme name and no other. It authenticates only. The default scheme stays the cookie, so a bearer token reaches no
|
|
// cookie surface, and this credential never reads the cookie, so the session cookie authenticates nothing on the
|
|
// desk. Audience, scope and Active membership are the desk's judgments (DeskEndpoints), not this port's.
|
|
public sealed class OidcDeskCredential : IDeskCredential
|
|
{
|
|
public async ValueTask<DeskToken?> AuthenticateAsync(HttpContext context, CancellationToken cancellationToken)
|
|
{
|
|
var result = await context.AuthenticateAsync(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
|
|
if (!result.Succeeded || result.Principal is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
// A token may name several audiences; the desk's own is reported when present so the desk judges it, else the
|
|
// first (or none).
|
|
var audiences = result.Principal.GetAudiences();
|
|
var audience = audiences.Contains(DeskEndpoints.Audience) ? DeskEndpoints.Audience : audiences.FirstOrDefault();
|
|
return new DeskToken(result.Principal.GetClaim(Claims.Subject), audience, [.. result.Principal.GetScopes()]);
|
|
}
|
|
}
|