/*
* 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 System.Buffers.Text;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace OpenIddict.Validation.SystemNetHttp;
///
/// Represents the context used by the System.Net.Http integration when creating a new HTTP client.
///
public sealed class OpenIddictValidationSystemNetHttpContext
{
private static readonly AsyncLocal _current = new();
///
/// Gets or sets the X.509 client certificate that will be used to authenticate
/// this peer when communicating with the external endpoint, if applicable.
///
public X509Certificate2? LocalCertificate { get; init; }
///
/// Gets or sets the ambient context for the current execution flow.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static OpenIddictValidationSystemNetHttpContext? Current
{
get => _current.Value;
set => _current.Value = value;
}
///
/// Computes a stable, unique identifier for the specified context using a cryptographic hash.
///
/// The client context for which to compute the stable identifier.
/// A string representing the stable identifier for the specified context.
[EditorBrowsable(EditorBrowsableState.Never)]
public static string ComputeStableId(OpenIddictValidationSystemNetHttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
using var algorithm = SHA256.Create();
if (context.LocalCertificate is X509Certificate2 certificate)
{
algorithm.TransformBlock(certificate.RawData, 0, certificate.RawData.Length, outputBuffer: null, outputOffset: 0);
}
algorithm.TransformFinalBlock([], 0, 0);
return Base64Url.EncodeToString(algorithm.Hash);
}
}