diff --git a/GPW_Admin/WebUserControls/mod_adminDipendenti.ascx.cs b/GPW_Admin/WebUserControls/mod_adminDipendenti.ascx.cs index 0f0bad0..0410636 100644 --- a/GPW_Admin/WebUserControls/mod_adminDipendenti.ascx.cs +++ b/GPW_Admin/WebUserControls/mod_adminDipendenti.ascx.cs @@ -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() diff --git a/GPW_Barcode/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/GPW_Barcode/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 1867bc1..a1f2a11 100644 Binary files a/GPW_Barcode/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/GPW_Barcode/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/GPW_Data/ObjLicenze.cs b/GPW_Data/ObjLicenze.cs index 47ba6d0..9e65411 100644 --- a/GPW_Data/ObjLicenze.cs +++ b/GPW_Data/ObjLicenze.cs @@ -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 diff --git a/GPW_Data/licenzeGPW.cs b/GPW_Data/licenzeGPW.cs index 9b838a6..c0c9d8d 100644 --- a/GPW_Data/licenzeGPW.cs +++ b/GPW_Data/licenzeGPW.cs @@ -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 /// //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/"; /// /// 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 infoList = JsonConvert.DeserializeObject>(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 infoList = JsonConvert.DeserializeObject>(rawData); + if (infoList != null) + { + answ = infoList.OrderByDescending(x => x.Scadenza).FirstOrDefault(); + } + } + else + { + answ = JsonConvert.DeserializeObject(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 codiciImpiego = new List(); + // 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 + /// + /// Calcolo Hash data stringa input + /// + /// + /// + 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; + } + /// /// Verifica attivazione licenza dato codice impiego /// @@ -467,6 +542,43 @@ namespace GPW_data return await Task.FromResult(answ); } + /// + /// Effettua refresh Payload su server (Remoto) e recupera info licenza in locale (sovrascrivendo info locali Redis) + /// + /// + public static async Task 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); + } + /// /// Tenta attivazione licenza dato codice impiego ///