Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add http client handler with x509 (#1469)
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/Whipstaff.Runtime/Cryptography/X509/X509Certificate2Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Whipstaff.Runtime.Cryptography.X509 | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="X509Certificate2"/>. | ||
/// </summary> | ||
public static class X509Certificate2Extensions | ||
{ | ||
/// <summary> | ||
/// Check to ensure that the certificate has a private key. | ||
/// </summary> | ||
/// <param name="certificate">Certificate to check.</param> | ||
public static void EnsurePrivateKey(this X509Certificate2 certificate) | ||
{ | ||
try | ||
{ | ||
// we wrap in try block as HasPrivateKey throws an exception on certain runtimes. | ||
if (certificate.HasPrivateKey) | ||
{ | ||
return; | ||
} | ||
} | ||
catch | ||
{ | ||
// no op | ||
} | ||
|
||
throw new ArgumentException("Certificate does not have a private key", nameof(certificate)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Whipstaff.Runtime.Cryptography.X509; | ||
|
||
namespace Whipstaff.Runtime.Http | ||
{ | ||
/// <summary> | ||
/// Factory for creating <see cref="HttpClientHandler"/> instances. | ||
/// </summary> | ||
public static class HttpClientHandlerFactory | ||
{ | ||
/// <summary> | ||
/// Creates a Http Client Handler with a client certificate attached. | ||
/// </summary> | ||
/// <param name="certificate">Certificate to bind as client certificate.</param> | ||
/// <returns><see cref="HttpClientHandler"/> instance.</returns> | ||
public static HttpClientHandler GetHttpClientHandlerWithClientCertificate(X509Certificate2 certificate) | ||
{ | ||
if (certificate == null) | ||
{ | ||
throw new ArgumentNullException(nameof(certificate)); | ||
} | ||
|
||
certificate.EnsurePrivateKey(); | ||
|
||
var clientHandler = new HttpClientHandler(); | ||
_ = clientHandler.ClientCertificates.Add(certificate); | ||
|
||
return clientHandler; | ||
} | ||
} | ||
} |