| | | 1 | | using System.Net.Http.Headers; |
| | | 2 | | using Microsoft.AspNetCore.Components.WebAssembly.Authentication; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// DelegatingHandler that automatically attaches the Auth0 bearer token to all outgoing requests. |
| | | 8 | | /// Uses standard Authorization header now that API is hosted on separate App Service. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ChronicisAuthHandler : DelegatingHandler |
| | | 11 | | { |
| | | 12 | | private readonly IAccessTokenProvider _tokenProvider; |
| | | 13 | | |
| | 0 | 14 | | public ChronicisAuthHandler(IAccessTokenProvider tokenProvider) |
| | | 15 | | { |
| | 0 | 16 | | _tokenProvider = tokenProvider; |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | protected override async Task<HttpResponseMessage> SendAsync( |
| | | 20 | | HttpRequestMessage request, |
| | | 21 | | CancellationToken cancellationToken) |
| | | 22 | | { |
| | 0 | 23 | | var tokenResult = await _tokenProvider.RequestAccessToken( |
| | 0 | 24 | | new AccessTokenRequestOptions |
| | 0 | 25 | | { |
| | 0 | 26 | | Scopes = new[] { "openid", "profile", "email" } |
| | 0 | 27 | | }); |
| | | 28 | | |
| | 0 | 29 | | if (tokenResult.TryGetToken(out var token)) |
| | | 30 | | { |
| | | 31 | | // Use standard Authorization header |
| | | 32 | | // Now that API is on separate App Service, Azure SWA no longer intercepts this |
| | 0 | 33 | | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Value); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return await base.SendAsync(request, cancellationToken); |
| | 0 | 37 | | } |
| | | 38 | | } |