Completata gestione payload iniziale

This commit is contained in:
Samuele Locatelli
2021-11-09 12:38:57 +01:00
parent c7b3bc7a2e
commit cb6cc17020
4 changed files with 149 additions and 24 deletions
@@ -64,8 +64,9 @@ namespace GPW_Admin.WebUserControls
{
// eseguo call di recupero da online
bool refreshApp = licenzeGPW.RefreshAppInfo().Result;
bool refreshLic = licenzeGPW.RefreshLicInfo().Result;
bool refreshAct = licenzeGPW.RefreshActInfo().Result;
bool refreshPayload = licenzeGPW.RefreshPayload().Result;
//bool refreshLic = licenzeGPW.RefreshLicInfo().Result;
}
private void fixVisibility()
+12
View File
@@ -83,6 +83,18 @@ namespace GPW_data
#endregion Public Properties
}
public class LicenseCoord
{
#region Public Properties
public string CodApp { get; set; } = "";
public string CodInst { get; set; } = "";
public string Enigma { get; set; } = "";
public string MasterKey { get; set; } = "";
#endregion Public Properties
}
public class UserLicenseRequest
{
#region Public Properties
+135 -23
View File
@@ -8,6 +8,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Web;
using static GPW_data.LiManObj;
using System.Text;
using System.Security.Cryptography;
namespace GPW_data
{
@@ -19,7 +21,7 @@ namespace GPW_data
/// Url di base chiamate API gestione licenze
/// </summary>
//protected static string apiUrl = "https://localhost:44351/";
protected static string apiUrl = "https://iis01.egalware.com/ELM.API/";
protected static string apiUrl = "https://liman.egalware.com/ELM.API/";
/// <summary>
/// Durata cache statica locale dati licenza
@@ -30,6 +32,7 @@ namespace GPW_data
protected static string rkeyActInfo = memLayer.ML.redHash($"LiMan:ActInfo");
protected static string rkeyAppInfo = memLayer.ML.redHash($"LiMan:AppInfo");
protected static string rkeyLicInfo = memLayer.ML.redHash($"LiMan:LicInfo");
protected static string rkeyPayload = memLayer.ML.redHash($"LiMan:Payload");
#endregion Protected Fields
@@ -87,6 +90,8 @@ namespace GPW_data
bool answ = false;
try
{
// effettua refresh attivazioni...
var fatto = RefreshActInfo().Result;
answ = utentiAttivi <= licenzeAttive;
if (!answ && memLayer.ML.confReadInt("_logLevel") > 5)
{
@@ -106,21 +111,24 @@ namespace GPW_data
{
get
{
bool answ = true;
// FIXME TODO !!!
//bool answ = false;
//try
//{
// answ = utentiAttivi <= licenzeAttive;
// if (!answ && memLayer.ML.confReadInt("_logLevel") > 5)
// {
// logger.lg.scriviLog(string.Format("WARNING licenze: utenti attivi {0} / licenziati {1}", utentiAttivi, licenzeAttive), tipoLog.WARNING);
// }
//}
//catch
//{ }
bool answ = false;
string currPayload = "";
try
{
// cerco payload licenze locale...
currPayload = memLayer.ML.getRSV(rkeyPayload);
// se no ci fosse --> refresh online!
if (string.IsNullOrEmpty(currPayload))
{
currPayload = calcPayload();
memLayer.ML.setRSV(rkeyPayload, currPayload, 3600);
}
// confronto
answ = InfoApplicativo.Payload == currPayload;
}
catch
{ }
// confronto payload calc con quello della licenza
return answ;
}
}
@@ -134,24 +142,37 @@ namespace GPW_data
{
LiManObj.ApplicativoDTO answ = new LiManObj.ApplicativoDTO();
// cerco da cache
string rawData = memLayer.ML.getRSV(rkeyAppInfo);
string rawData = memLayer.ML.getRSV(rkeyLicInfo);
// se no ci fosse --> refresh online!
if (string.IsNullOrEmpty(rawData))
{
var fatto = RefreshAppInfo().Result;
if (fatto)
{
rawData = memLayer.ML.getRSV(rkeyAppInfo);
rawData = memLayer.ML.getRSV(rkeyLicInfo);
}
}
if (!string.IsNullOrEmpty(rawData))
{
// POTREBBE darmi + di 1 risultato, prendo + recente...
List<LiManObj.ApplicativoDTO> infoList = JsonConvert.DeserializeObject<List<LiManObj.ApplicativoDTO>>(rawData);
if (infoList != null)
try
{
answ = infoList.OrderByDescending(x => x.Scadenza).FirstOrDefault();
// verifico se ho 1 risultato o molti...se inizia per "[" è molti
if (rawData.StartsWith("["))
{
// POTREBBE darmi + di 1 risultato, prendo + recente...
List<LiManObj.ApplicativoDTO> infoList = JsonConvert.DeserializeObject<List<LiManObj.ApplicativoDTO>>(rawData);
if (infoList != null)
{
answ = infoList.OrderByDescending(x => x.Scadenza).FirstOrDefault();
}
}
else
{
answ = JsonConvert.DeserializeObject<LiManObj.ApplicativoDTO>(rawData);
}
}
catch
{ }
}
return answ;
}
@@ -186,7 +207,7 @@ namespace GPW_data
{
int answ = 0;
int licenzeMax = licenseMan.getLicenseNum(installazione, applicazione);
// verifico dato su DB sia valido... expiry date con quel numero sia > della data odierna!!!
// verifico dato su DB sia valido... expiry date con quel numero sia > della data odierna
if (scadenzaLicenza >= DateTime.Today)
{
answ = licenzeDb;
@@ -322,8 +343,62 @@ namespace GPW_data
#endregion Public Properties
#region Protected Methods
protected static string calcPayload()
{
string answ = "";
// costruisco stringa da elenco impieghi...
StringBuilder sb = new StringBuilder();
// ciclo tutti gli utenti attivi
var localUserList = DataProxy.DP.taDipendenti.getAttivi(false);
List<string> codiciImpiego = new List<string>();
// ciclo x ogni dip attivo...
foreach (var item in localUserList)
{
codiciImpiego.Add(DataProxy.DP.hashCodImpiego(item));
}
// riordino
var ordList = codiciImpiego.OrderBy(x => x);
foreach (var item in ordList)
{
sb.AppendLine(item);
}
string rawData = sb.ToString();
// hashing!
using (var md5 = MD5.Create())
{
byte[] InputBytes = Encoding.UTF8.GetBytes(rawData);
var byteHash = md5.ComputeHash(InputBytes);
answ = BitConverter.ToString(byteHash).Replace("-", "").ToLowerInvariant();
}
return answ;
}
#endregion Protected Methods
#region Public Methods
/// <summary>
/// Calcolo Hash data stringa input
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
public static string calcHash(string rawData)
{
string hash = "";
// hashing!
using (var md5 = MD5.Create())
{
byte[] InputBytes = Encoding.UTF8.GetBytes(rawData);
var byteHash = md5.ComputeHash(InputBytes);
hash = BitConverter.ToString(byteHash).Replace("-", "").ToLowerInvariant();
}
return hash;
}
/// <summary>
/// Verifica attivazione licenza dato codice impiego
/// </summary>
@@ -467,6 +542,43 @@ namespace GPW_data
return await Task.FromResult(answ);
}
/// <summary>
/// Effettua refresh Payload su server (Remoto) e recupera info licenza in locale (sovrascrivendo info locali Redis)
/// </summary>
/// <returns></returns>
public static async Task<bool> RefreshPayload()
{
bool answ = false;
// svuoto payload calcolato in primis...
memLayer.ML.setRSV(rkeyPayload, "", 1);
// cerco online
RestClient client = new RestClient(apiUrl);
//client.Authenticator = new HttpBasicAuthenticator("username", "password");
string MKeyEnc = HttpUtility.UrlEncode(MasterKey);
var request = new RestRequest($"/api/licenza/refreshPayload", DataFormat.Json);
string newEnigma = calcHash($"{DateTime.Now:yyyyMMddHHmmss}");
LicenseCoord newBody = new LicenseCoord()
{
MasterKey = MasterKey,
CodInst = installazione,
CodApp = applicazione,
Enigma = newEnigma
};
request.AddJsonBody(newBody);
var response = client.Post(request);
// controllo risposta
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
answ = true;
// salvo in redis contenuto serializzato
string rawData = response.Content;
// salvo in redis per 7 gg
memLayer.ML.setRSV(rkeyLicInfo, rawData, LicCacheTTL);
}
return await Task.FromResult(answ);
}
/// <summary>
/// Tenta attivazione licenza dato codice impiego
/// </summary>