using ServiceShared;
using ServiceShared.Crypto;
using ServiceShared.Models.Request;
using ServiceShared.Models.Response;
namespace ServiceInside.Service
{
public static class ServiceOutside
{
///
/// URL of ServiceOutside
///
private static string ServiceOutsideURL;
///
/// Sets the URL of ServiceOutside
///
/// Url of OutsideService
public static void SetServiceOutsideURL(string serviceOutsideURL)
{
ServiceOutsideURL = serviceOutsideURL;
}
///
/// Sends notification to the ServiceOutside
///
/// notification that should be sent to the outside service
public static void Notify(Notification notification)
{
try
{
KeyPair keyPair = Curve25519.GenerateKeyPair();
EncryptedRequest encryptedRequest = new EncryptedRequest("Notification", notification, ServiceOutsideDeriveKey(keyPair));
ServiceShared.Https.Request.PostWithoutResponse(ServiceOutsideURL, "serviceinside", "notification", encryptedRequest, keyPair);
}
catch (Exception ex)
{
Log.Critical(ex, "ServiceInside.Service.ServiceOutside", "Notify");
}
}
///
/// Shared derive key from outside service over (exchange controller)
///
/// KeyPair from the InsideService
///
private static byte[] ServiceOutsideDeriveKey(KeyPair keyPair)
{
byte[] result = null;
try
{
PublicKey publicKey = ServiceShared.Https.Request.GetPublicKey(ServiceOutsideURL);
if(publicKey != null && !string.IsNullOrEmpty(publicKey.key))
{
byte[] base64Encoded = Convert.FromBase64String(publicKey.key);
result = keyPair.GetSharedKey(publicKey.key);
}
else
{
Log.Critical(new Exception("Could not get public key from the outside service"), "ServiceInside.Service.ServiceOutside", "ServiceOutsideDeriveKey");
}
}
catch (Exception ex)
{
Log.Critical(ex, "ServiceInside.Service.ServiceOutside", "ServiceOutsideDeriveKey(KeyPair)");
}
return result;
}
}
}