/// <summary> /// Provides templating variable replacement for Config values /// </summary> /// <param name="template"></param> /// <param name="config"></param> /// <param name="forSuccess"></param> /// <returns></returns> private static string ParseValues(string template, CertRequestConfig config, bool forSuccess, bool url_encode) { // add all config properties to template vars var vars = new Dictionary <string, string>(); foreach (var prop in config.GetType().GetProperties()) { string value = prop.GetValue(config)?.ToString() ?? ""; if (url_encode) { value = WebUtility.UrlEncode(value); } else { value = value.Replace(@"\", @"\\"); } vars[prop.Name.ToLower()] = value; } // add special processing for these values vars["success"] = forSuccess ? "true" : "false"; vars["subjectalternativenames"] = string.Join(",", config.SubjectAlternativeNames ?? new string[] { config.PrimaryDomain }); // process the template and replace values return(Regex.Replace(template, @"\$(\w+)(?=[\W$])", m => { // replace var if it can be found, otherwise don't string key = m.Groups[1].Value.ToLower(); return vars.ContainsKey(key) ? vars[key] : "$" + key; }, RegexOptions.IgnoreCase)); }
/// <summary> /// Sends an HTTP Request with the requested parameters /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="contentType"></param> /// <param name="body"></param> /// <returns>A named Tuple with Success boolean and int StatusCode of the HTTP Request</returns> public async static Task <(bool Success, int StatusCode)> SendRequest(CertRequestConfig config, bool forSuccess) { using (var client = new HttpClient()) { HttpRequestMessage message; string Url = ParseValues(config.WebhookUrl, config, forSuccess, true); switch (config.WebhookMethod) { case METHOD_GET: message = new HttpRequestMessage(HttpMethod.Get, Url); break; case METHOD_POST: message = new HttpRequestMessage(HttpMethod.Post, Url) { Content = new StringContent( ParseValues(!string.IsNullOrEmpty(config.WebhookContentBody) ? config.WebhookContentBody : DEFAULT_BODY, config, forSuccess, false), Encoding.UTF8, string.IsNullOrEmpty(config.WebhookContentType) ? "application/json" : config.WebhookContentType) }; break; default: throw new ArgumentException("Method must be GET or POST", "method"); } var resp = await client.SendAsync(message); return(resp.IsSuccessStatusCode, (int)resp.StatusCode); } }
public ManagedCertificate() { Name = "New Managed Certificate"; IncludeInAutoRenew = true; DomainOptions = new ObservableCollection <DomainOption>(); RequestConfig = new CertRequestConfig(); IncludeInAutoRenew = true; }
public ManagedCertificate() { Name = "New Managed Certificate"; IncludeInAutoRenew = true; DomainOptions = new ObservableCollection <DomainOption>(); RequestConfig = new CertRequestConfig(); IncludeInAutoRenew = true; CertificateAuthorityId = "letsencrypt.org"; }
public ManagedCertificate() { Name = "New Managed Certificate"; IncludeInAutoRenew = true; DomainOptions = new ObservableCollection <DomainOption>(); RequestConfig = new CertRequestConfig(); IncludeInAutoRenew = true; #if DEBUG UseStagingMode = true; #else UseStagingMode = false; #endif }