diff --git a/DigitalData.Core.Security/Cryptographer/CryptographerExtensions.cs b/DigitalData.Core.Security/Cryptographer/CryptographerExtensions.cs new file mode 100644 index 0000000..4452007 --- /dev/null +++ b/DigitalData.Core.Security/Cryptographer/CryptographerExtensions.cs @@ -0,0 +1,29 @@ +namespace DigitalData.Core.Security.Cryptographer +{ + public static class CryptographerExtensions + { + public static IEnumerable GetByIssuer(this IEnumerable cryptographers, string issuer) where TRSACryptographer: RSACryptographer + => cryptographers.Where(c => c.Issuer == issuer); + + public static IEnumerable GetByAudience(this IEnumerable cryptographers, string audience) where TRSACryptographer : RSACryptographer + => cryptographers.Where(c => c.Audience == audience); + + public static TRSACryptographer Get(this IEnumerable cryptographers, string issuer, string audience) where TRSACryptographer : RSACryptographer + => cryptographers.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault() + ?? throw new InvalidOperationException($"No {cryptographers.GetTypeName()} found with Issuer: {issuer} and Audience: {audience}"); + + public static bool TryGet(this IEnumerable cryptographers, string issuer, string audience, out TRSACryptographer? cryptographer) where TRSACryptographer : RSACryptographer + { + cryptographer = cryptographers.Where(c => c.Issuer == issuer && c.Audience == audience).SingleOrDefault(); + return cryptographer is not null; + } + + public static RSACryptographerList ToCryptographerList(this IEnumerable cryptographers, Func keyGenerator) where TRSACryptographer : RSACryptographer + => new(keyGenerator: keyGenerator, cryptographers: cryptographers); + + private static string GetTypeName(this IEnumerable cryptographers) where TRSACryptographer : RSACryptographer + => typeof(TRSACryptographer).GetType().Name.TrimStart(InterfaceIdentifierChar); + + private static readonly char InterfaceIdentifierChar = 'I'; + } +} \ No newline at end of file