/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; namespace OpenIddict.Extensions; /// /// Exposes the helpers used to restrict the algorithms accepted when validating JSON Web Tokens. /// internal static class OpenIddictAlgorithmHelpers { /// /// Creates an that only restricts the JWS signing algorithms. /// /// /// Unlike , which IdentityModel also applies to /// the JWE key management and content encryption algorithms ("alg"/"enc" headers of encrypted tokens), /// the returned validator ignores the algorithms of encrypted tokens (which are implicitly restricted /// by the configured decryption keys) and only applies the list to the signature of the (inner) JWS. /// If already defines an algorithm validator or valid algorithms, /// they are also enforced. /// /// The allowed signing algorithms. /// The token validation parameters whose existing restrictions must be preserved. /// The algorithm validator. public static AlgorithmValidator CreateSigningAlgorithmValidator( IEnumerable algorithms, TokenValidationParameters? parameters = null) { ArgumentNullException.ThrowIfNull(algorithms); var allowed = new HashSet(algorithms, StringComparer.Ordinal); var validator = parameters?.AlgorithmValidator; var list = parameters?.ValidAlgorithms?.ToArray(); return (algorithm, key, token, parameters) => { if (validator is not null) { if (!validator(algorithm, key, token, parameters)) { return false; } } else if (list is { Length: > 0 } && !list.Contains(algorithm, StringComparer.Ordinal)) { return false; } // Note: the "alg" and "enc" headers of JWE tokens are not signing algorithms. if (token is JsonWebToken { IsEncrypted: true }) { return true; } return allowed.Contains(algorithm); }; } }