Add Azure Functions authentication boundary - #822
Conversation
Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an authentication boundary for generated Azure Functions endpoints (isolated worker) by invoking ASP.NET Core authentication (IAuthenticationService) inside the generated function body, plus accompanying configuration hooks, tests, and design/decision documentation.
Changes:
- Added
ArkAzureFunctionsInvocation.AuthenticateAsync(...)and generator emission so each generated function authenticates (and challenges) before binding/dispatch. - Introduced DI registration helper for Azure Functions authentication options/scheme and added snapshot/runtime tests around anonymous metadata + challenge behavior.
- Documented the design decision to keep authentication at the generated function boundary rather than worker middleware.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Ark.Tools.MediatorFramework.Tests/GeneratorSnapshotTests.cs | Adds generator/runtime tests for anonymous metadata and challenge behavior. |
| src/mediator-framework/Ark.Tools.MediatorFramework.AzureFunctions/ArkAzureFunctionsServiceCollectionExtensions.cs | Adds AddArkAzureFunctionsAuthentication(...) registration helper. |
| src/mediator-framework/Ark.Tools.MediatorFramework.AzureFunctions/ArkAzureFunctionsInvocation.cs | Adds the runtime authentication helper used by generated functions. |
| src/mediator-framework/Ark.Tools.MediatorFramework.AzureFunctions/ArkAzureFunctionsAuthenticationOptions.cs | Introduces authentication options and a profile enum (DirectBearer/EasyAuth). |
| src/mediator-framework/Ark.Tools.MediatorFramework.AzureFunctions.Generators/AzureFunctionsEndpointGenerator.cs | Emits authentication checks and propagates AllowAnonymous metadata into generation. |
| docs/mediator-framework/progress/tasks/README.md | Updates progress checklist. |
| docs/mediator-framework/progress/azure-functions-decision-log.md | Records the auth-boundary decision (AZD-12). |
| docs/mediator-framework/azure-functions-design.md | Updates design notes about worker middleware vs generated auth boundary. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (allowAnonymous) | ||
| { | ||
| context.User ??= new System.Security.Claims.ClaimsPrincipal(new System.Security.Claims.ClaimsIdentity()); | ||
| return null; | ||
| } | ||
|
|
||
| var authentication = context.RequestServices.GetService<IAuthenticationService>() | ||
| ?? throw new InvalidOperationException( | ||
| "The Azure Functions authentication service is not registered. Configure ASP.NET Core authentication."); | ||
| var options = context.RequestServices.GetService<IOptions<ArkAzureFunctionsAuthenticationOptions>>()?.Value; | ||
| var scheme = options?.Scheme; | ||
| var result = await authentication.AuthenticateAsync(context, scheme).ConfigureAwait(false); | ||
| if (!result.Succeeded || result.Principal is null) | ||
| return Results.Challenge( | ||
| authenticationSchemes: scheme is null ? null : new[] { scheme }); | ||
|
|
||
| context.User = result.Principal; | ||
| return null; | ||
| } |
| /// <summary>Gets or sets the authentication profile.</summary> | ||
| public ArkAzureFunctionsAuthenticationProfile Profile { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the authentication scheme. <see langword="null"/> uses the host default scheme. |
|
@copilot Fix the code for all comments in this review thread. When a review comment includes a suggested change, apply the suggestion exactly. Do not make changes beyond what is described in the linked review thread. |
…mplemented Profile enum Co-authored-by: AndreaCuneo <5227688+AndreaCuneo@users.noreply.github.com>
Fixed in the latest commit:
|
Azure Functions needed authentication parity with the existing HTTP transport while ASP.NET Core middleware is unavailable in isolated-worker hosting.
Authentication
IAuthenticationService.AllowAnonymousand returned standard challenge results.HttpContext.User.Generator
FunctionContextmiddleware as suitable for worker-level concerns, not ASP.NET authentication.