Skip to content

Commit 5b56edd

Browse files
committed
Remove all the store resolvers and mark all the assemblies as trimming/Native AOT-compatible
1 parent 342b765 commit 5b56edd

File tree

112 files changed

+2312
-4928
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+2312
-4928
lines changed

Directory.Build.targets

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
<PublicSign>false</PublicSign>
1616
</PropertyGroup>
1717

18+
<!--
19+
Note: the trimming and Native AOT analyzers are only supported on .NET.
20+
-->
21+
22+
<PropertyGroup
23+
Condition=" $(RepoRelativeProjectDir.Contains('src')) And '$(TargetFrameworkIdentifier)' == '.NETCoreApp' ">
24+
<IsAotCompatible>true</IsAotCompatible>
25+
</PropertyGroup>
26+
1827
<!--
1928
Note: .NET Native, .NET Framework and .NET Standard assemblies are not annotated with
2029
nullable references annotations. To avoid errors on these target frameworks, related
@@ -55,6 +64,7 @@
5564
<DefineConstants>$(DefineConstants);SUPPORTS_INT32_RANDOM_NUMBER_GENERATOR_METHODS</DefineConstants>
5665
<DefineConstants>$(DefineConstants);SUPPORTS_MULTIPLE_VALUES_IN_QUERYHELPERS</DefineConstants>
5766
<DefineConstants>$(DefineConstants);SUPPORTS_NAMED_PIPE_STATIC_FACTORY_WITH_ACL</DefineConstants>
67+
<DefineConstants>$(DefineConstants);SUPPORTS_NATIVE_AOT</DefineConstants>
5868
<DefineConstants>$(DefineConstants);SUPPORTS_ONE_SHOT_HASHING_METHODS</DefineConstants>
5969
<DefineConstants>$(DefineConstants);SUPPORTS_ONE_SHOT_RANDOM_NUMBER_GENERATOR_METHODS</DefineConstants>
6070
<DefineConstants>$(DefineConstants);SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON</DefineConstants>

shared/OpenIddict.Extensions/OpenIddictHelpers.cs

Lines changed: 53 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -52,71 +52,6 @@ public static IEnumerable<List<TSource>> Buffer<TSource>(this IEnumerable<TSourc
5252
}
5353
}
5454

55-
/// <summary>
56-
/// Finds the first base type that matches the specified generic type definition.
57-
/// </summary>
58-
/// <param name="type">The type to introspect.</param>
59-
/// <param name="definition">The generic type definition.</param>
60-
/// <returns>A <see cref="Type"/> instance if the base type was found, <see langword="null"/> otherwise.</returns>
61-
public static Type? FindGenericBaseType(Type type, Type definition)
62-
=> FindGenericBaseTypes(type, definition).FirstOrDefault();
63-
64-
/// <summary>
65-
/// Finds all the base types that matches the specified generic type definition.
66-
/// </summary>
67-
/// <param name="type">The type to introspect.</param>
68-
/// <param name="definition">The generic type definition.</param>
69-
/// <returns>A <see cref="Type"/> instance if the base type was found, <see langword="null"/> otherwise.</returns>
70-
public static IEnumerable<Type> FindGenericBaseTypes(Type type, Type definition)
71-
{
72-
if (type is null)
73-
{
74-
throw new ArgumentNullException(nameof(type));
75-
}
76-
77-
if (definition is null)
78-
{
79-
throw new ArgumentNullException(nameof(definition));
80-
}
81-
82-
if (!definition.IsGenericTypeDefinition)
83-
{
84-
throw new ArgumentException(SR.GetResourceString(SR.ID0263), nameof(definition));
85-
}
86-
87-
if (definition.IsInterface)
88-
{
89-
foreach (var contract in type.GetInterfaces())
90-
{
91-
if (!contract.IsGenericType && !contract.IsConstructedGenericType)
92-
{
93-
continue;
94-
}
95-
96-
if (contract.GetGenericTypeDefinition() == definition)
97-
{
98-
yield return contract;
99-
}
100-
}
101-
}
102-
103-
else
104-
{
105-
for (var candidate = type; candidate is not null; candidate = candidate.BaseType)
106-
{
107-
if (!candidate.IsGenericType && !candidate.IsConstructedGenericType)
108-
{
109-
continue;
110-
}
111-
112-
if (candidate.GetGenericTypeDefinition() == definition)
113-
{
114-
yield return candidate;
115-
}
116-
}
117-
}
118-
}
119-
12055
#if !SUPPORTS_TASK_WAIT_ASYNC
12156
/// <summary>
12257
/// Waits until the specified task returns a result or the cancellation token is signaled.
@@ -502,13 +437,19 @@ public static async ValueTask<IReadOnlyDictionary<string, StringValues>> ParseFo
502437
/// The implementation resolved from <see cref="CryptoConfig.CreateFromName(string)"/> is not valid.
503438
/// </exception>
504439
public static ECDsa CreateEcdsaKey()
505-
=> CryptoConfig.CreateFromName("OpenIddict ECDSA Cryptographic Provider") switch
440+
{
441+
return GetAlgorithmFromConfig() switch
506442
{
507443
ECDsa result => result,
508444
null => ECDsa.Create(),
509445
var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName))
510446
};
511447

448+
[UnconditionalSuppressMessage("Trimming", "IL2026",
449+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
450+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict ECDSA Cryptographic Provider");
451+
}
452+
512453
/// <summary>
513454
/// Creates a new <see cref="ECDsa"/> key.
514455
/// </summary>
@@ -519,7 +460,7 @@ public static ECDsa CreateEcdsaKey()
519460
/// </exception>
520461
public static ECDsa CreateEcdsaKey(ECCurve curve)
521462
{
522-
var algorithm = CryptoConfig.CreateFromName("OpenIddict ECDSA Cryptographic Provider") switch
463+
var algorithm = GetAlgorithmFromConfig() switch
523464
{
524465
ECDsa result => result,
525466
null => null,
@@ -546,6 +487,10 @@ public static ECDsa CreateEcdsaKey(ECCurve curve)
546487
}
547488

548489
return algorithm;
490+
491+
[UnconditionalSuppressMessage("Trimming", "IL2026",
492+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
493+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict ECDSA Cryptographic Provider");
549494
}
550495
#endif
551496

@@ -559,7 +504,7 @@ public static ECDsa CreateEcdsaKey(ECCurve curve)
559504
/// </exception>
560505
public static RSA CreateRsaKey(int size)
561506
{
562-
var algorithm = CryptoConfig.CreateFromName("OpenIddict RSA Cryptographic Provider") switch
507+
var algorithm = GetAlgorithmFromConfig() switch
563508
{
564509
RSA result => result,
565510

@@ -619,6 +564,10 @@ public static RSA CreateRsaKey(int size)
619564
}
620565

621566
return algorithm;
567+
568+
[UnconditionalSuppressMessage("Trimming", "IL2026",
569+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
570+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict RSA Cryptographic Provider");
622571
}
623572

624573
/// <summary>
@@ -632,7 +581,7 @@ public static RSA CreateRsaKey(int size)
632581
/// </exception>
633582
public static byte[] ComputeSha256MessageAuthenticationCode(byte[] key, byte[] data)
634583
{
635-
var algorithm = CryptoConfig.CreateFromName("OpenIddict HMAC SHA-256 Cryptographic Provider", [key]) switch
584+
var algorithm = GetAlgorithmFromConfig(key) switch
636585
{
637586
HMACSHA256 result => result,
638587
null => null,
@@ -659,6 +608,10 @@ public static byte[] ComputeSha256MessageAuthenticationCode(byte[] key, byte[] d
659608
{
660609
algorithm.Dispose();
661610
}
611+
612+
[UnconditionalSuppressMessage("Trimming", "IL2026",
613+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
614+
static object? GetAlgorithmFromConfig(byte[] key) => CryptoConfig.CreateFromName("OpenIddict HMAC SHA-256 Cryptographic Provider", [key]);
662615
}
663616

664617
/// <summary>
@@ -671,7 +624,7 @@ public static byte[] ComputeSha256MessageAuthenticationCode(byte[] key, byte[] d
671624
/// </exception>
672625
public static byte[] ComputeSha256Hash(byte[] data)
673626
{
674-
var algorithm = CryptoConfig.CreateFromName("OpenIddict SHA-256 Cryptographic Provider") switch
627+
var algorithm = GetAlgorithmFromConfig() switch
675628
{
676629
SHA256 result => result,
677630
null => null,
@@ -698,6 +651,10 @@ public static byte[] ComputeSha256Hash(byte[] data)
698651
{
699652
algorithm.Dispose();
700653
}
654+
655+
[UnconditionalSuppressMessage("Trimming", "IL2026",
656+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
657+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict SHA-256 Cryptographic Provider");
701658
}
702659

703660
/// <summary>
@@ -710,7 +667,7 @@ public static byte[] ComputeSha256Hash(byte[] data)
710667
/// </exception>
711668
public static byte[] ComputeSha384Hash(byte[] data)
712669
{
713-
var algorithm = CryptoConfig.CreateFromName("OpenIddict SHA-384 Cryptographic Provider") switch
670+
var algorithm = GetAlgorithmFromConfig() switch
714671
{
715672
SHA384 result => result,
716673
null => null,
@@ -737,6 +694,10 @@ public static byte[] ComputeSha384Hash(byte[] data)
737694
{
738695
algorithm.Dispose();
739696
}
697+
698+
[UnconditionalSuppressMessage("Trimming", "IL2026",
699+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
700+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict SHA-384 Cryptographic Provider");
740701
}
741702

742703
/// <summary>
@@ -749,7 +710,7 @@ public static byte[] ComputeSha384Hash(byte[] data)
749710
/// </exception>
750711
public static byte[] ComputeSha512Hash(byte[] data)
751712
{
752-
var algorithm = CryptoConfig.CreateFromName("OpenIddict SHA-512 Cryptographic Provider") switch
713+
var algorithm = GetAlgorithmFromConfig() switch
753714
{
754715
SHA512 result => result,
755716
null => null,
@@ -776,6 +737,10 @@ public static byte[] ComputeSha512Hash(byte[] data)
776737
{
777738
algorithm.Dispose();
778739
}
740+
741+
[UnconditionalSuppressMessage("Trimming", "IL2026",
742+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
743+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict SHA-512 Cryptographic Provider");
779744
}
780745

781746
/// <summary>
@@ -788,7 +753,7 @@ public static byte[] ComputeSha512Hash(byte[] data)
788753
/// </exception>
789754
public static byte[] CreateRandomArray(int size)
790755
{
791-
var algorithm = CryptoConfig.CreateFromName("OpenIddict RNG Cryptographic Provider") switch
756+
var algorithm = GetAlgorithmFromConfig() switch
792757
{
793758
RandomNumberGenerator result => result,
794759
null => null,
@@ -825,6 +790,10 @@ public static byte[] CreateRandomArray(int size)
825790
}
826791

827792
return array;
793+
794+
[UnconditionalSuppressMessage("Trimming", "IL2026",
795+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
796+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict RNG Cryptographic Provider");
828797
}
829798

830799
/// <summary>
@@ -839,7 +808,7 @@ public static byte[] CreateRandomArray(int size)
839808
/// </exception>
840809
public static string CreateRandomString(ReadOnlySpan<string> charset, int count)
841810
{
842-
var algorithm = CryptoConfig.CreateFromName("OpenIddict RNG Cryptographic Provider") switch
811+
var algorithm = GetAlgorithmFromConfig() switch
843812
{
844813
RandomNumberGenerator result => result,
845814
null => null,
@@ -908,6 +877,10 @@ static int GetInt32(RandomNumberGenerator algorithm, Range range)
908877

909878
return (int) value + range.Start.Value;
910879
}
880+
881+
[UnconditionalSuppressMessage("Trimming", "IL2026",
882+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
883+
static object? GetAlgorithmFromConfig() => CryptoConfig.CreateFromName("OpenIddict RNG Cryptographic Provider");
911884
}
912885

913886
/// <summary>
@@ -1036,8 +1009,7 @@ public static byte[] DeriveKey(string secret, byte[] salt, HashAlgorithmName alg
10361009
{
10371010
// Warning: the type and order of the arguments specified here MUST exactly match the parameters used with
10381011
// Rfc2898DeriveBytes(string password, byte[] salt, int iterations, HashAlgorithmName hashAlgorithm).
1039-
using var generator = CryptoConfig.CreateFromName("OpenIddict PBKDF2 Cryptographic Provider",
1040-
args: [secret, salt, iterations, algorithm]) switch
1012+
using var generator = GetAlgorithmFromConfig(secret, salt, iterations, algorithm) switch
10411013
{
10421014
Rfc2898DeriveBytes result => result,
10431015

@@ -1049,6 +1021,11 @@ public static byte[] DeriveKey(string secret, byte[] salt, HashAlgorithmName alg
10491021
};
10501022

10511023
return generator.GetBytes(length);
1024+
1025+
[UnconditionalSuppressMessage("Trimming", "IL2026",
1026+
Justification = "The default implementation is always used when no custom algorithm was registered.")]
1027+
static object? GetAlgorithmFromConfig(string secret, byte[] salt, int iterations, HashAlgorithmName algorithm)
1028+
=> CryptoConfig.CreateFromName("OpenIddict PBKDF2 Cryptographic Provider", [secret, salt, iterations, algorithm]);
10521029
}
10531030
#endif
10541031

0 commit comments

Comments
 (0)