|
| 1 | +using Payjoin; |
| 2 | + |
| 3 | +namespace Payjoin.Http |
| 4 | +{ |
| 5 | + internal static class OhttpKeysClient |
| 6 | + { |
| 7 | + /// <summary> |
| 8 | + /// Fetches the OHTTP keys from the specified payjoin directory via proxy. |
| 9 | + /// </summary> |
| 10 | + /// <param name="ohttpRelayUrl"> |
| 11 | + /// The HTTP CONNECT method proxy to request the OHTTP keys from a payjoin directory. |
| 12 | + /// Proxying requests for OHTTP keys ensures a client IP address is never revealed to |
| 13 | + /// the payjoin directory. |
| 14 | + /// </param> |
| 15 | + /// <param name="directoryUrl"> |
| 16 | + /// The payjoin directory from which to fetch the OHTTP keys. This directory stores |
| 17 | + /// and forwards payjoin client payloads. |
| 18 | + /// </param> |
| 19 | + /// <param name="certificate">The DER-encoded certificate to use for local HTTPS connections.</param> |
| 20 | + /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> |
| 21 | + /// <returns>The decoded <see cref="OhttpKeys"/> from the payjoin directory.</returns> |
| 22 | + internal static async Task<OhttpKeys> GetOhttpKeysAsync(System.Uri ohttpRelayUrl, System.Uri directoryUrl, byte[] certificate, CancellationToken cancellationToken = default) |
| 23 | + { |
| 24 | + var keysUrl = new System.Uri(directoryUrl, "/.well-known/ohttp-gateway"); |
| 25 | + |
| 26 | + using var handler = new HttpClientHandler |
| 27 | + { |
| 28 | + Proxy = new System.Net.WebProxy(ohttpRelayUrl), |
| 29 | + UseProxy = true, |
| 30 | + ServerCertificateCustomValidationCallback = (_, serverCert, _, _) => serverCert != null && serverCert.GetRawCertData().SequenceEqual(certificate) |
| 31 | + }; |
| 32 | + |
| 33 | + using var client = new HttpClient(handler); |
| 34 | + using var request = new HttpRequestMessage(HttpMethod.Get, keysUrl); |
| 35 | + request.Headers.Accept.ParseAdd("application/ohttp-keys"); |
| 36 | + |
| 37 | + using var response = await client.SendAsync(request, cancellationToken); |
| 38 | + response.EnsureSuccessStatusCode(); |
| 39 | + |
| 40 | + var ohttpKeysBytes = await response.Content.ReadAsByteArrayAsync(cancellationToken); |
| 41 | + return OhttpKeys.Decode(ohttpKeysBytes); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments