patbef-ServiceInside/ServiceInside/Controllers/BaseController.cs

137 lines
4.4 KiB
C#
Raw Permalink Normal View History

2024-01-29 16:26:54 +01:00
using Microsoft.AspNetCore.Mvc;
using ServiceInside.Filter;
using ServiceShared;
using ServiceShared.Crypto;
using ServiceShared.Models.Response;
using System.Text.Json;
namespace ServiceInside.Controllers
{
[TrustedHeader]
[ServiceOutsideRequest]
public class BaseController : ControllerBase
{
/// <summary>
/// Current LogType from the configuration
/// </summary>
private static Log.Types _LogType = Log.Types.INFO;
/// <summary>
/// Configuration from appsettings.json
/// </summary>
private readonly IConfiguration _configuration = null;
/// <summary>
/// Server asymetric KeyPair(PrivateKey, PublicKey)
/// It changes evrytime when the server is restarting
/// </summary>
protected readonly KeyPair _KeyPair = null;
/// <summary>
/// Constructor of BaseController, that getting instance of configuration and KeyPair
/// </summary>
/// <param name="_configuration">Configuration from appsettings.json</param>
/// <param name="keyPair">Server Curve25519 KeyPair</param>
public BaseController(IConfiguration _configuration, KeyPair keyPair)
{
this._configuration = _configuration;
this._KeyPair = keyPair;
}
/// <summary>
/// Sets the log type
/// </summary>
/// <param name="Log.Types">Log.Types</param>
public static void SetLog(Log.Types logType)
{
_LogType = logType;
}
/// <summary>
/// Signature public key of server for the trusted response
/// </summary>
/// <returns>Returns server signature public key</returns>
public string GetServerSignatureKey()
{
return this._KeyPair.SigningPublicKey;
}
/// <summary>
/// Signature of server for the trusted response
/// <paramref name="response">Response text, that sould be signied by server</paramref>
/// </summary>
/// <returns>Returns signature of response</returns>
public string GetServerSignature(String response)
{
return this._KeyPair.GetSignature(response);
}
/// <summary>
/// Client public key
/// </summary>
/// <returns>Returns client public key from the header (base64)</returns>
protected string GetClientPublicKey()
{
return this.HttpContext.Request.Headers["Client-Key"].ToString();
}
/// <summary>
/// Client signature of trusted request
/// </summary>
/// <returns>Returns client signature of request from the header (base64)</returns>
protected string GetClientSignature()
{
return this.HttpContext.Request.Headers["Client-Signature"].ToString();
}
/// <summary>
/// Client public signature key for the trusted request
/// </summary>
/// <returns>Returns client signature public key of trusted request from the header (base64), that was used in the signature</returns>
protected string GetClientSignatureKey()
{
return this.HttpContext.Request.Headers["Client-Signature-Key"].ToString();
}
/// <summary>
/// Server public key for shared key(Encryption)
/// </summary>
/// <returns>Returns server public key for shared key</returns>
public string GetServerPublicKey()
{
return this._KeyPair.PublicKey;
}
/// <summary>
/// Returns client shared key
/// </summary>
/// <returns>HKDF.DeriveKey(SHA512)</returns>
protected byte[] GetClientSharedKey()
{
return this._KeyPair.GetSharedKey(this.GetClientPublicKey());
}
/// <summary>
/// Debugs object in debug mode
/// </summary>
/// <param name="obj"></param>
protected void Debug(object obj, string type)
{
if (_LogType == Log.Types.DEBUG)
{
if (obj != null)
{
try
{
Log.Debug("[" + type + "]" + JsonSerializer.Serialize(obj));
}
catch (Exception ex)
{
Log.Critical(ex, "ServiceInside.Controllers.BaseController", "Debug");
}
}
}
}
}
}