using Agger.Registro; using Assinador.Model.Domain; using Newtonsoft.Json; using Sign.Modelos; using System; using System.Diagnostics; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace Gestor.Application.Helpers { public static class AssinadorHelper { private static ParametrosAssinaturaAssinador _parametros; public const string Authorization = "Authorization"; public const string Server = "Server"; public const string Contrato = "Purchase"; public const string Adquirir = "Purchase/BuyPackage"; public const string Disponiveis = "Sign/Availble"; public const string Contratadas = "Sign/Purchased"; private static string ApiKey { get; set; } public static ParametrosAssinaturaAssinador Parametros { get { return AssinadorHelper._parametros; } set { AssinadorHelper._parametros = value; if (value == null) { return; } Sign.Modelos.Remetente remetente = new Sign.Modelos.Remetente(); remetente.set_Id(ApplicationHelper.IdFornecedor); remetente.set_Documento(value.get_Documento()); remetente.set_Email(value.get_Email()); remetente.set_Nome(value.get_Nome()); remetente.set_Serial(ApplicationHelper.NumeroSerial); AssinadorHelper.Remetente = remetente; } } public static Sign.Modelos.Remetente Remetente { get; set; } private static string Base64EncodeBasic(string plainText) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText)); } public static string BasicKey(this string serial) { DateTime universalTime = Funcoes.GetNetworkTime().ToUniversalTime(); return AssinadorHelper.Base64EncodeBasic(string.Format("{0}:{1}", serial, universalTime.Ticks)); } public static async Task Contratado(long id) { bool flag = await AssinadorHelper.Get(string.Format("{0}{1}/{2}/86", Address.GestorApi(), "Purchase", id), ApplicationHelper.NumeroSerial.BasicKey(), true) == "true"; return flag; } public static async Task Get(string command, string token = null, bool basic = false) { HttpStatusCode statusCode; HttpResponseMessage httpResponseMessage; string str = (basic ? "Basic" : "Token"); Uri uri = new Uri(Address.GestorApi(), command); HttpClient httpClient = new HttpClient(); if (!string.IsNullOrWhiteSpace(token)) { httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue(str, token)); } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; try { ConfiguredTaskAwaitable configuredTaskAwaitable = httpClient.GetAsync(uri).ConfigureAwait(false); httpResponseMessage = await configuredTaskAwaitable; } catch (Exception exception) { statusCode = HttpStatusCode.InternalServerError; return statusCode; } statusCode = httpResponseMessage.get_StatusCode(); return statusCode; } internal static async Task Get(string command, string token = null, bool basic = false) where T : class { T t; HttpResponseMessage httpResponseMessage; string str = (basic ? "Basic" : "Token"); Uri uri = new Uri(command); HttpClient httpClient = new HttpClient(); if (!string.IsNullOrWhiteSpace(token)) { httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue(str, token)); } ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; try { ConfiguredTaskAwaitable configuredTaskAwaitable = httpClient.GetAsync(uri).ConfigureAwait(false); httpResponseMessage = await configuredTaskAwaitable; } catch (Exception exception) { t = default(T); return t; } if (httpResponseMessage.get_StatusCode() == HttpStatusCode.OK) { string result = httpResponseMessage.get_Content().ReadAsStringAsync().Result; t = JsonConvert.DeserializeObject(httpResponseMessage.get_Content().ReadAsStringAsync().Result); } else { t = default(T); } return t; } public static async Task Key() { string str = null; try { str = await AssinadorHelper.Get(string.Format("{0}{1}/86", Address.AssinadorApi(), "Authorization"), ApplicationHelper.NumeroSerial.BasicKey(), true); } catch (Exception exception) { } string str1 = str; str = null; return str1; } public static async Task Licencas(string key) { int num; if (key != null) { int num1 = 0; try { string str = await AssinadorHelper.Get(string.Format("{0}{1}/{2}", Address.AssinadorApi(), "Sign/Availble", ApplicationHelper.IdFornecedor), key, false); num1 = int.Parse(str); } catch (Exception exception) { } num = num1; } else { num = 0; } return num; } internal static async Task Put(string command, T keyValues, string token = null, bool basic = false) where T : class { T t; string str = (basic ? "Basic" : "Token"); Uri uri = new Uri(command); object obj = keyValues; JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings(); jsonSerializerSetting.set_ReferenceLoopHandling(1); StringContent stringContent = new StringContent(JsonConvert.SerializeObject(obj, 1, jsonSerializerSetting), Encoding.UTF8, "application/json"); HttpClient httpClient = new HttpClient(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; if (!string.IsNullOrWhiteSpace(token)) { httpClient.get_DefaultRequestHeaders().set_Authorization(new AuthenticationHeaderValue(str, token)); } HttpResponseMessage httpResponseMessage = await httpClient.PutAsync(uri, stringContent); if (httpResponseMessage.get_StatusCode() == HttpStatusCode.OK || httpResponseMessage.get_StatusCode() == HttpStatusCode.NoContent) { t = (httpResponseMessage.get_StatusCode() != HttpStatusCode.NoContent ? JsonConvert.DeserializeObject(httpResponseMessage.get_Content().ReadAsStringAsync().Result) : default(T)); } else { t = default(T); } return t; } } }