PalacePal/Pal.Client/Net/RemoteApi.Utils.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2023-02-11 20:10:45 +00:00
using System;
2022-12-21 19:23:48 +00:00
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
2023-03-26 13:47:18 +00:00
using Dalamud.Logging;
using Grpc.Core;
using Microsoft.Extensions.Logging;
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Net;
internal partial class RemoteApi
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
private Metadata UnauthorizedHeaders() => new()
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
{ "User-Agent", _userAgent },
};
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
private Metadata AuthorizedHeaders() => new()
{
{ "Authorization", $"Bearer {_loginInfo.AuthToken}" },
{ "User-Agent", _userAgent },
};
2022-12-21 19:23:48 +00:00
2023-03-30 20:01:43 +00:00
private SslClientAuthenticationOptions? GetSslClientAuthenticationOptions()
{
2022-12-21 19:23:48 +00:00
#if !DEBUG
2023-03-30 20:01:43 +00:00
var secrets = typeof(RemoteApi).Assembly.GetType("Pal.Client.Secrets");
if (secrets == null)
return null;
var pass = secrets.GetProperty("CertPassword")?.GetValue(null) as string;
if (pass == null)
return null;
var manifestResourceStream = typeof(RemoteApi).Assembly.GetManifestResourceStream("Pal.Client.Certificate.pfx");
if (manifestResourceStream == null)
return null;
var bytes = new byte[manifestResourceStream.Length];
int read = manifestResourceStream.Read(bytes, 0, bytes.Length);
if (read != bytes.Length)
throw new InvalidOperationException();
var certificate = new X509Certificate2(bytes, pass, X509KeyStorageFlags.DefaultKeySet);
_logger.LogDebug("Using client certificate {CertificateHash}", certificate.GetCertHashString());
return new SslClientAuthenticationOptions
{
ClientCertificates = new X509CertificateCollection()
2022-12-21 19:23:48 +00:00
{
2023-03-30 20:01:43 +00:00
certificate,
},
};
2022-12-21 19:23:48 +00:00
#else
2023-03-30 20:01:43 +00:00
_logger.LogDebug("Not using client certificate");
return null;
2022-12-21 19:23:48 +00:00
#endif
}
}