| | | 1 | | |
| | | 2 | | using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Authentication; |
| | | 5 | | /// <summary> |
| | | 6 | | /// OpenID Connect events to handle client assertion injection. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Initializes a new instance of the <see cref="OidcEvents"/> class. |
| | | 10 | | /// </remarks> |
| | | 11 | | /// <param name="assertionService">The assertion service used to create client assertions. </param> |
| | 2 | 12 | | public class OidcEvents(AssertionService assertionService) : OpenIdConnectEvents |
| | | 13 | | { |
| | 2 | 14 | | private readonly AssertionService _assertionService = assertionService; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Handles the AuthorizationCodeReceived event to inject client assertions. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="context">The context for the AuthorizationCodeReceived event.</param> |
| | | 20 | | /// <returns>A completed task.</returns> |
| | | 21 | | public override Task AuthorizationCodeReceived(AuthorizationCodeReceivedContext context) |
| | | 22 | | { |
| | 2 | 23 | | var tokenEndpoint = |
| | 2 | 24 | | context.Options.Configuration?.TokenEndpoint |
| | 2 | 25 | | ?? (context.Options.Authority?.TrimEnd('/') + "/connect/token"); |
| | 2 | 26 | | if (context.TokenEndpointRequest is not null) |
| | | 27 | | { |
| | 1 | 28 | | context.TokenEndpointRequest.ClientAssertionType = |
| | 1 | 29 | | "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"; |
| | | 30 | | |
| | 1 | 31 | | context.TokenEndpointRequest.ClientAssertion = |
| | 1 | 32 | | _assertionService.CreateClientAssertion(tokenEndpoint); |
| | | 33 | | } |
| | 2 | 34 | | return Task.CompletedTask; |
| | | 35 | | } |
| | | 36 | | } |