|
1 | 1 | using Microsoft.AspNetCore.Authentication; |
| 2 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
2 | 3 | using Microsoft.AspNetCore.Http; |
3 | 4 | using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using System.Net.Http.Headers; |
| 7 | +using System.Text.Json; |
4 | 8 |
|
5 | 9 | namespace MrWho.ClientAuth.M2M; |
6 | 10 |
|
7 | 11 | /// <summary> |
8 | 12 | /// Delegating handler that forwards the current authenticated user's access token (server-side scenarios). |
| 13 | +/// Supports optional automatic refresh (refresh_token) and automatic OIDC challenge on downstream 401. |
9 | 14 | /// </summary> |
10 | 15 | internal sealed class MrWhoUserAccessTokenHandler : DelegatingHandler |
11 | 16 | { |
12 | 17 | private readonly IHttpContextAccessor _httpContextAccessor; |
13 | 18 | private readonly ILogger<MrWhoUserAccessTokenHandler> _logger; |
| 19 | + private readonly IAuthenticationSchemeProvider _schemeProvider; |
| 20 | + private readonly IOptionsMonitor<OpenIdConnectOptions> _oidcOptions; |
| 21 | + private readonly IHttpClientFactory _httpClientFactory; |
| 22 | + private readonly MrWhoUserAccessTokenHandlerOptions _options; |
14 | 23 |
|
15 | | - public MrWhoUserAccessTokenHandler(IHttpContextAccessor httpContextAccessor, ILogger<MrWhoUserAccessTokenHandler> logger) |
| 24 | + public MrWhoUserAccessTokenHandler( |
| 25 | + IHttpContextAccessor httpContextAccessor, |
| 26 | + ILogger<MrWhoUserAccessTokenHandler> logger, |
| 27 | + IAuthenticationSchemeProvider schemeProvider, |
| 28 | + IOptionsMonitor<OpenIdConnectOptions> oidcOptions, |
| 29 | + IOptions<MrWhoUserAccessTokenHandlerOptions> options, |
| 30 | + IHttpClientFactory httpClientFactory) |
16 | 31 | { |
17 | 32 | _httpContextAccessor = httpContextAccessor; |
18 | 33 | _logger = logger; |
| 34 | + _schemeProvider = schemeProvider; |
| 35 | + _oidcOptions = oidcOptions; |
| 36 | + _httpClientFactory = httpClientFactory; |
| 37 | + _options = options.Value; |
19 | 38 | } |
20 | 39 |
|
21 | 40 | protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
22 | 41 | { |
23 | 42 | var ctx = _httpContextAccessor.HttpContext; |
24 | 43 | if (ctx?.User?.Identity?.IsAuthenticated == true) |
25 | 44 | { |
26 | | - var accessToken = await ctx.GetTokenAsync("access_token"); |
| 45 | + string? accessToken; |
| 46 | + if (_options.EnableAutomaticRefresh) |
| 47 | + { |
| 48 | + accessToken = await EnsureFreshAccessTokenAsync(ctx, cancellationToken); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + accessToken = await ctx.GetTokenAsync("access_token"); |
| 53 | + } |
| 54 | + |
27 | 55 | if (!string.IsNullOrWhiteSpace(accessToken)) |
28 | 56 | { |
29 | | - request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); |
| 57 | + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); |
30 | 58 | } |
31 | 59 | else |
32 | 60 | { |
33 | 61 | _logger.LogDebug("User authenticated but no access token present in session."); |
34 | 62 | } |
35 | 63 | } |
36 | | - return await base.SendAsync(request, cancellationToken); |
| 64 | + |
| 65 | + var response = await base.SendAsync(request, cancellationToken); |
| 66 | + |
| 67 | + if (_options.ChallengeOnUnauthorized && response.StatusCode == System.Net.HttpStatusCode.Unauthorized && ctx?.User?.Identity?.IsAuthenticated == true) |
| 68 | + { |
| 69 | + if (!ctx.Response.HasStarted) |
| 70 | + { |
| 71 | + _logger.LogInformation("Downstream API returned 401 for user {Sub}. Triggering OIDC challenge.", ctx.User.FindFirst("sub")?.Value); |
| 72 | + await ctx.ChallengeAsync(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return response; |
| 77 | + } |
| 78 | + |
| 79 | + private async Task<string?> EnsureFreshAccessTokenAsync(HttpContext ctx, CancellationToken ct) |
| 80 | + { |
| 81 | + var accessToken = await ctx.GetTokenAsync("access_token"); |
| 82 | + var expiresAtStr = await ctx.GetTokenAsync("expires_at"); |
| 83 | + if (!DateTimeOffset.TryParse(expiresAtStr, out var expiresAtUtc)) |
| 84 | + { |
| 85 | + return accessToken; |
| 86 | + } |
| 87 | + |
| 88 | + if (DateTimeOffset.UtcNow.Add(_options.RefreshSkew) < expiresAtUtc) |
| 89 | + { |
| 90 | + return accessToken; |
| 91 | + } |
| 92 | + |
| 93 | + var refreshToken = await ctx.GetTokenAsync("refresh_token"); |
| 94 | + if (string.IsNullOrWhiteSpace(refreshToken)) |
| 95 | + { |
| 96 | + _logger.LogDebug("Access token near/at expiry but no refresh_token available."); |
| 97 | + return accessToken; |
| 98 | + } |
| 99 | + |
| 100 | + try |
| 101 | + { |
| 102 | + var (cookieScheme, oidcScheme) = await ResolveSchemesAsync(); |
| 103 | + var oidc = _oidcOptions.Get(oidcScheme); |
| 104 | + var authority = oidc.Authority?.TrimEnd('/') ?? string.Empty; |
| 105 | + if (string.IsNullOrEmpty(authority)) |
| 106 | + { |
| 107 | + _logger.LogWarning("Cannot refresh token: OIDC authority missing."); |
| 108 | + return accessToken; |
| 109 | + } |
| 110 | + |
| 111 | + var tokenEndpoint = $"{authority}/connect/token"; // standard endpoint |
| 112 | + var client = oidc.BackchannelHttpHandler != null |
| 113 | + ? new HttpClient(oidc.BackchannelHttpHandler, disposeHandler: false) |
| 114 | + : _httpClientFactory.CreateClient(); |
| 115 | + |
| 116 | + var form = new Dictionary<string, string> |
| 117 | + { |
| 118 | + ["grant_type"] = "refresh_token", |
| 119 | + ["refresh_token"] = refreshToken |
| 120 | + }; |
| 121 | + if (!string.IsNullOrEmpty(oidc.ClientId)) form["client_id"] = oidc.ClientId; |
| 122 | + if (!string.IsNullOrEmpty(oidc.ClientSecret)) form["client_secret"] = oidc.ClientSecret; |
| 123 | + |
| 124 | + using var req = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint) { Content = new FormUrlEncodedContent(form) }; |
| 125 | + using var resp = await client.SendAsync(req, ct); |
| 126 | + if (!resp.IsSuccessStatusCode) |
| 127 | + { |
| 128 | + var body = await resp.Content.ReadAsStringAsync(ct); |
| 129 | + _logger.LogWarning("Refresh token request failed: {Status} {Body}", (int)resp.StatusCode, body); |
| 130 | + return accessToken; |
| 131 | + } |
| 132 | + |
| 133 | + var json = await resp.Content.ReadAsStringAsync(ct); |
| 134 | + using var doc = JsonDocument.Parse(json); |
| 135 | + var newAccessToken = doc.RootElement.TryGetProperty("access_token", out var atEl) ? atEl.GetString() : null; |
| 136 | + var newRefreshToken = doc.RootElement.TryGetProperty("refresh_token", out var rtEl) ? rtEl.GetString() : refreshToken; |
| 137 | + var expiresIn = doc.RootElement.TryGetProperty("expires_in", out var expEl) ? expEl.GetInt32() : 3600; |
| 138 | + if (string.IsNullOrEmpty(newAccessToken)) |
| 139 | + { |
| 140 | + _logger.LogWarning("Refresh response missing access_token."); |
| 141 | + return accessToken; |
| 142 | + } |
| 143 | + |
| 144 | + var newExpiresAt = DateTimeOffset.UtcNow.AddSeconds(expiresIn); |
| 145 | + |
| 146 | + var authResult = await ctx.AuthenticateAsync(cookieScheme); |
| 147 | + if (authResult.Succeeded && authResult.Properties != null) |
| 148 | + { |
| 149 | + authResult.Properties.UpdateTokenValue("access_token", newAccessToken); |
| 150 | + authResult.Properties.UpdateTokenValue("refresh_token", newRefreshToken); |
| 151 | + authResult.Properties.UpdateTokenValue("expires_at", newExpiresAt.ToString("o")); |
| 152 | + await ctx.SignInAsync(cookieScheme, authResult.Principal!, authResult.Properties); |
| 153 | + _logger.LogDebug("Refreshed user access token; new expiry {Expiry}", newExpiresAt); |
| 154 | + return newAccessToken; |
| 155 | + } |
| 156 | + _logger.LogWarning("Failed to persist refreshed token (scheme {Scheme}).", cookieScheme); |
| 157 | + return newAccessToken; |
| 158 | + } |
| 159 | + catch (Exception ex) |
| 160 | + { |
| 161 | + _logger.LogError(ex, "Error refreshing user access token"); |
| 162 | + return accessToken; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private async Task<(string cookieScheme, string oidcScheme)> ResolveSchemesAsync() |
| 167 | + { |
| 168 | + var defaultAuthenticate = await _schemeProvider.GetDefaultAuthenticateSchemeAsync(); |
| 169 | + var defaultChallenge = await _schemeProvider.GetDefaultChallengeSchemeAsync(); |
| 170 | + var cookie = defaultAuthenticate?.Name ?? MrWhoClientAuthDefaults.CookieScheme; |
| 171 | + var oidc = defaultChallenge?.Name ?? MrWhoClientAuthDefaults.OpenIdConnectScheme; |
| 172 | + return (cookie, oidc); |
37 | 173 | } |
38 | 174 | } |
0 commit comments