151 lines
5.0 KiB
C#
151 lines
5.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using ServiceShared.Database;
|
|
using ServiceOutside.Filter;
|
|
using ServiceShared;
|
|
using ServiceShared.Crypto;
|
|
using System.Text.Json;
|
|
using ServiceShared.Models.Response;
|
|
|
|
namespace ServiceOutside.Controllers
|
|
{
|
|
[TrustedHeader]
|
|
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>
|
|
/// dbContext
|
|
/// </summary>
|
|
private readonly DbContext _dbContext = null;
|
|
|
|
/// <summary>
|
|
/// Server asymetric KeyPair(PrivateKey, PublicKey)
|
|
/// It changes evrytime when the server is restarting
|
|
/// </summary>
|
|
protected readonly KeyPair _KeyPair = null;
|
|
|
|
/// <summary>
|
|
/// Maintenance Flag
|
|
/// </summary>
|
|
protected readonly bool Maintenance = false;
|
|
|
|
/// <summary>
|
|
/// Constructor of BaseController, that getting instance of configuration, dbcontext and KeyPair
|
|
/// </summary>
|
|
/// <param name="_configuration">Configuration from appsettings.json</param>
|
|
/// <param name="dbContext">DbContext</param>
|
|
/// <param name="keyPair">Server Curve25519 KeyPair</param>
|
|
public BaseController(IConfiguration _configuration, DbContext dbContext, KeyPair keyPair)
|
|
{
|
|
this._configuration = _configuration;
|
|
this._dbContext = dbContext;
|
|
this._KeyPair = keyPair;
|
|
this.Maintenance = this._dbContext.GetMaintenance();
|
|
}
|
|
|
|
/// <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>
|
|
/// Logs object with type in debug mode
|
|
/// </summary>
|
|
/// <param name="obj">Logging object</param>
|
|
/// <param name="type">Type of logging object</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, "ServiceOutside.Controllers.BaseController", "Debug");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|