Skip to content

Commit 86da59b

Browse files
committed
Added AOT compatibility to HTTP Client authentication.
1 parent 1852cb7 commit 86da59b

18 files changed

+160
-52
lines changed

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from ASP.NET Core authentication schemes.
88
</Description>
99
<PackageTags>$(PackageTags);Security;Authentication;OAuth;OAuth2;OAuth 2.0;IdentityModel;ASP.NET Core</PackageTags>
10+
<!-- Without this, ILLink analyzers are not added, even though 'IsAotComptabile' is set in the parent 'Directory.Build.props' -->
11+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
1012
</PropertyGroup>
1113

1214
<ItemGroup>

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore/AuthenticationSchemeOAuthClientOptionsResolver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) The AppCore .NET project.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Threading.Tasks;
67
using AppCoreNet.Diagnostics;
78
using Microsoft.AspNetCore.Authentication;
@@ -16,7 +17,11 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore;
1617
/// <typeparam name="TClientOptions">The type of the <see cref="AuthenticationSchemeOptions"/>.</typeparam>
1718
/// <typeparam name="TOptions">The type of the <see cref="RemoteAuthenticationOptions"/>.</typeparam>
1819
/// <typeparam name="THandler">The type of the <see cref="IAuthenticationHandler"/>.</typeparam>
19-
public abstract class AuthenticationSchemeOAuthClientOptionsResolver<TClientOptions, TOptions, THandler> : IOAuthOptionsResolver
20+
public abstract class AuthenticationSchemeOAuthClientOptionsResolver<
21+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TClientOptions,
22+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions,
23+
THandler>
24+
: IOAuthOptionsResolver
2025
where TClientOptions : AuthenticationSchemeOptions
2126
where TOptions : RemoteAuthenticationOptions
2227
where THandler : IAuthenticationHandler

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore/AuthenticationSessionOAuthUserTokenStore.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) The AppCore .NET project.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Security.Authentication;
67
using System.Security.Claims;
78
using System.Threading;
@@ -20,7 +21,9 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore;
2021
/// session.
2122
/// </summary>
2223
/// <typeparam name="TOptions">The type of the <see cref="OAuthUserOptions"/>.</typeparam>
23-
public abstract class AuthenticationSessionOAuthUserTokenStore<TOptions> : IOAuthUserTokenStore
24+
public abstract class AuthenticationSessionOAuthUserTokenStore<
25+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>
26+
: IOAuthUserTokenStore
2427
where TOptions : OAuthUserOptions
2528
{
2629
private readonly IHttpContextAccessor _httpContextAccessor;

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore/OAuthUserHandler.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore;
1616
/// Provides a base class for a OAuth user authentication handler.
1717
/// </summary>
1818
/// <typeparam name="TParameters">The type of the <see cref="OAuthUserParameters"/>.</typeparam>
19-
public abstract class OAuthUserHandler<TParameters> : IAuthenticationSchemeHandler<TParameters>
19+
public abstract class OAuthUserHandler<TParameters> : IAuthenticationSchemeHandler
2020
where TParameters : OAuthUserParameters
2121
{
2222
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -42,7 +42,14 @@ protected OAuthUserHandler(IHttpContextAccessor httpContextAccessor, IOAuthUserT
4242
/// <param name="scheme">The <see cref="AuthenticationScheme"/>.</param>
4343
protected abstract void EnsureCompatibleScheme(AuthenticationScheme scheme);
4444

45-
/// <inheritdoc />
45+
/// <summary>
46+
/// Authenticates a <see cref="HttpRequestMessage"/> with the specified OAuth user scheme and parameters.
47+
/// </summary>
48+
/// <param name="scheme">The <see cref="AuthenticationScheme"/>.</param>
49+
/// <param name="request">The <see cref="HttpRequestMessage"/>.</param>
50+
/// <param name="parameters">The <see cref="OAuthUserParameters"/>.</param>
51+
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
52+
/// <returns>The asynchronous operation.</returns>
4653
public async Task AuthenticateAsync(
4754
AuthenticationScheme scheme,
4855
HttpRequestMessage request,
@@ -64,4 +71,18 @@ await _authTokenService.GetAccessTokenAsync(scheme, user, parameters, cancellati
6471

6572
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
6673
}
74+
75+
async Task IAuthenticationSchemeHandler.AuthenticateAsync(
76+
AuthenticationScheme scheme,
77+
HttpRequestMessage request,
78+
AuthenticationParameters? parameters,
79+
CancellationToken cancellationToken)
80+
{
81+
await AuthenticateAsync(
82+
scheme,
83+
request,
84+
(TParameters?)parameters,
85+
cancellationToken)
86+
.ConfigureAwait(false);
87+
}
6788
}

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore/OAuthUserTokenService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth.AspNetCore;
2020
/// Provides the base class for see <see cref="IOAuthUserTokenService"/>.
2121
/// </summary>
2222
/// <typeparam name="TOptions">The type of the <see cref="OAuthUserOptions"/>.</typeparam>
23-
public abstract class OAuthUserTokenService<TOptions> : IOAuthUserTokenService
23+
public abstract class OAuthUserTokenService<
24+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>
25+
: IOAuthUserTokenService
2426
where TOptions : OAuthUserOptions
2527
{
2628
private static readonly ConcurrentDictionary<string, Lazy<Task<OAuthUserToken>>> _sync = new();

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth.OpenIdConnect/AppCoreNet.Extensions.Http.Authentication.OAuth.OpenIdConnect.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from ASP.NET Core OpenID connect authentication schemes.
88
</Description>
99
<PackageTags>$(PackageTags);Security;Authentication;OAuth;OAuth2;OAuth 2.0;IdentityModel;ASP.NET Core;OpenID Connect</PackageTags>
10+
<!-- Without this, ILLink analyzers are not added, even though 'IsAotComptabile' is set in the parent 'Directory.Build.props' -->
11+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
1012
</PropertyGroup>
1113

1214
<ItemGroup>

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth/OAuthClientHandler.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth;
1212
/// <summary>
1313
/// Provides a OAuth client authentication handler.
1414
/// </summary>
15-
public class OAuthClientHandler : IAuthenticationSchemeHandler<OAuthParameters>
15+
public class OAuthClientHandler : IAuthenticationSchemeHandler
1616
{
1717
private readonly IOAuthTokenService _authTokenService;
1818

@@ -26,7 +26,14 @@ public OAuthClientHandler(IOAuthTokenService authTokenService)
2626
_authTokenService = authTokenService;
2727
}
2828

29-
/// <inheritdoc />
29+
/// <summary>
30+
/// Authenticates a <see cref="HttpRequestMessage"/> with the specified OAuth client scheme and parameters.
31+
/// </summary>
32+
/// <param name="scheme">The <see cref="AuthenticationScheme"/>.</param>
33+
/// <param name="request">The <see cref="HttpRequestMessage"/>.</param>
34+
/// <param name="parameters">The <see cref="OAuthParameters"/>.</param>
35+
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
36+
/// <returns>The asynchronous operation.</returns>
3037
public async Task AuthenticateAsync(
3138
AuthenticationScheme scheme,
3239
HttpRequestMessage request,
@@ -39,4 +46,18 @@ await _authTokenService.GetClientAccessTokenAsync(scheme, parameters, cancellati
3946

4047
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
4148
}
49+
50+
async Task IAuthenticationSchemeHandler.AuthenticateAsync(
51+
AuthenticationScheme scheme,
52+
HttpRequestMessage request,
53+
AuthenticationParameters? parameters,
54+
CancellationToken cancellationToken)
55+
{
56+
await AuthenticateAsync(
57+
scheme,
58+
request,
59+
(OAuthParameters?)parameters,
60+
cancellationToken)
61+
.ConfigureAwait(false);
62+
}
4263
}

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth/OAuthOptionsResolver.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
// Copyright (c) The AppCore .NET project.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Threading.Tasks;
67
using Microsoft.Extensions.Options;
78

89
namespace AppCoreNet.Extensions.Http.Authentication.OAuth;
910

10-
internal sealed class OAuthOptionsResolver<TOptions> : IOAuthOptionsResolver
11+
internal sealed class OAuthOptionsResolver<
12+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TOptions>
13+
: IOAuthOptionsResolver
1114
where TOptions : OAuthOptions
1215
{
1316
private readonly IOptionsMonitor<TOptions> _options;

Http/src/AppCoreNet.Extensions.Http.Authentication.OAuth/OAuthPasswordHandler.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Net.Http;
1+
// Licensed under the MIT license.
2+
// Copyright (c) The AppCore .NET project.
3+
4+
using System.Net.Http;
25
using System.Net.Http.Headers;
36
using System.Threading;
47
using System.Threading.Tasks;
@@ -9,7 +12,7 @@ namespace AppCoreNet.Extensions.Http.Authentication.OAuth;
912
/// <summary>
1013
/// Provides a OAuth password authentication handler.
1114
/// </summary>
12-
public class OAuthPasswordHandler : IAuthenticationSchemeHandler<OAuthParameters>
15+
public class OAuthPasswordHandler : IAuthenticationSchemeHandler
1316
{
1417
private readonly IOAuthTokenService _authTokenService;
1518

@@ -23,7 +26,14 @@ public OAuthPasswordHandler(IOAuthTokenService authTokenService)
2326
_authTokenService = authTokenService;
2427
}
2528

26-
/// <inheritdoc />
29+
/// <summary>
30+
/// Authenticates a <see cref="HttpRequestMessage"/> with the specified OAuth password scheme and parameters.
31+
/// </summary>
32+
/// <param name="scheme">The <see cref="AuthenticationScheme"/>.</param>
33+
/// <param name="request">The <see cref="HttpRequestMessage"/>.</param>
34+
/// <param name="parameters">The <see cref="OAuthParameters"/>.</param>
35+
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
36+
/// <returns>The asynchronous operation.</returns>
2737
public async Task AuthenticateAsync(
2838
AuthenticationScheme scheme,
2939
HttpRequestMessage request,
@@ -36,4 +46,18 @@ await _authTokenService.GetPasswordAccessTokenAsync(scheme, parameters, cancella
3646

3747
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
3848
}
49+
50+
async Task IAuthenticationSchemeHandler.AuthenticateAsync(
51+
AuthenticationScheme scheme,
52+
HttpRequestMessage request,
53+
AuthenticationParameters? parameters,
54+
CancellationToken cancellationToken)
55+
{
56+
await AuthenticateAsync(
57+
scheme,
58+
request,
59+
(OAuthParameters?)parameters,
60+
cancellationToken)
61+
.ConfigureAwait(false);
62+
}
3963
}

Http/src/AppCoreNet.Extensions.Http.Authentication/AuthenticationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ namespace AppCoreNet.Extensions.Http.Authentication;
1616
/// authentication to the request.
1717
/// </summary>
1818
/// <typeparam name="TParameters">The type of the <see cref="AuthenticationParameters"/>.</typeparam>
19-
/// <typeparam name="THandler">The type of the <see cref="IAuthenticationSchemeHandler{TParameters}"/>.</typeparam>
19+
/// <typeparam name="THandler">The type of the <see cref="IAuthenticationSchemeHandler"/>.</typeparam>
2020
public class AuthenticationHandler<TParameters, THandler> : DelegatingHandler
2121
where TParameters : AuthenticationParameters, new()
22-
where THandler : IAuthenticationSchemeHandler<TParameters>
22+
where THandler : IAuthenticationSchemeHandler
2323
{
2424
private readonly string _scheme;
2525
private readonly IAuthenticationSchemeProvider _schemes;
26-
private readonly IAuthenticationSchemeHandler<TParameters> _schemeHandler;
26+
private readonly IAuthenticationSchemeHandler _schemeHandler;
2727
private readonly TParameters? _parameters;
2828
private readonly ILogger _logger;
2929

0 commit comments

Comments
 (0)