public void CleanUp(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            var httpChallenge = (HttpChallenge)ctx.Challenge;

            EditFile(httpChallenge, true, ctx.Out);
        }
예제 #2
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            DnsChallenge challenge = (DnsChallenge)ctx.Challenge;
            var          helper    = new OvhHelper(Endpoint, ApplicationKey, ApplicationSecret, ConsumerKey);

            helper.AddOrUpdateDnsRecord(challenge.RecordName, GetCleanedRecordValue(challenge.RecordValue));
        }
예제 #3
0
        public void CleanUp(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            DnsChallenge challenge = (DnsChallenge)ctx.Challenge;
            var          helper    = new OvhHelper(Endpoint, ApplicationKey, ApplicationSecret, ConsumerKey);

            helper.DeleteDnsRecord(challenge.RecordName);
        }
        public void CleanUp(ChallengeHandlingContext ctx)
        {
            var dnsChallenge  = (DnsChallenge)ctx.Challenge;
            var domainDetails = GetDomainId(dnsChallenge);

            var records = managedPath + domainDetails.DomainId + "/records";

            CleanUp(dnsChallenge, domainDetails, records);
        }
예제 #5
0
        public void CleanUp(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            DnsChallenge challenge = (DnsChallenge)ctx.Challenge;
            var          helper    = new CloudFlareHelper(AuthKey, EmailAddress, DomainName);

            helper.DeleteDnsRecord(challenge.RecordName);

            ctx.Out.WriteLine("DNS record deleted of type [TXT] with name [{0}]", challenge.RecordName);
        }
예제 #6
0
        public void CleanUp(ChallengeHandlingContext ctx)
        {
            var dnsChallenge = (DnsChallenge)ctx.Challenge;
            var domain       = GetDomainId(dnsChallenge);

            var wr = CreateRequest(Token, domain, "");

            using (var response = wr.GetResponse())
            { }
        }
예제 #7
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            DnsChallenge challenge = (DnsChallenge)ctx.Challenge;
            var          helper    = new CloudFlareHelper(AuthKey, EmailAddress, DomainName);

            helper.AddOrUpdateDnsRecord(challenge.RecordName, GetCleanedRecordValue(challenge.RecordValue));

            ctx.Out.WriteLine("DNS record created of type [TXT] with name [{0}]", challenge.RecordName);
        }
예제 #8
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            DnsChallenge dnsChallenge = ctx.Challenge as DnsChallenge;

            ManagementScope            mgmtScope      = new ManagementScope(@"\\.\Root\MicrosoftDNS");
            ManagementClass            mgmtClass      = null;
            ManagementBaseObject       mgmtParams     = null;
            ManagementObjectSearcher   mgmtSearch     = null;
            ManagementObjectCollection mgmtDNSRecords = null;
            string strQuery;

            strQuery = string.Format("SELECT * FROM MicrosoftDNS_TXTType WHERE OwnerName = '{0}'", dnsChallenge.RecordName);

            mgmtScope.Connect();

            mgmtSearch = new ManagementObjectSearcher(mgmtScope, new ObjectQuery(strQuery));

            mgmtDNSRecords = mgmtSearch.Get();

            if (mgmtDNSRecords.Count == 1)
            {
                foreach (ManagementObject mgmtDNSRecord in mgmtDNSRecords)
                {
                    mgmtParams = mgmtDNSRecord.GetMethodParameters("modify");
                    mgmtParams["DescriptiveText"] = dnsChallenge.RecordValue;

                    mgmtDNSRecord.InvokeMethod("modify", mgmtParams, null);

                    break;
                }

                ctx.Out.WriteLine("Updated DNS record of type [TXT] with name [{0}]",
                                  dnsChallenge.RecordName);
            }
            else if (mgmtDNSRecords.Count == 0)
            {
                mgmtClass = new ManagementClass(mgmtScope, new ManagementPath("MicrosoftDNS_TXTType"), null);

                mgmtParams = mgmtClass.GetMethodParameters("CreateInstanceFromPropertyData");
                mgmtParams["DnsServerName"]   = Environment.MachineName;
                mgmtParams["ContainerName"]   = dnsChallenge.RecordName.Split('.')[dnsChallenge.RecordName.Split('.').Count() - 2] + "." + dnsChallenge.RecordName.Split('.')[dnsChallenge.RecordName.Split('.').Count() - 1];
                mgmtParams["OwnerName"]       = dnsChallenge.RecordName;
                mgmtParams["DescriptiveText"] = dnsChallenge.RecordValue;

                mgmtClass.InvokeMethod("CreateInstanceFromPropertyData", mgmtParams, null);

                ctx.Out.WriteLine("Created DNS record of type [TXT] with name [{0}]",
                                  dnsChallenge.RecordName);
            }
            else
            {
                throw new InvalidOperationException("There should not be more than one DNS txt record for the name.");
            }
        }
예제 #9
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            var dnsChallenge = (DnsChallenge)ctx.Challenge;
            var domain       = GetDomainId(dnsChallenge);

            var wr = CreateRequest(Token, domain, dnsChallenge.RecordValue);

            using (var response = wr.GetResponse())
            { }
        }
        public void Handle(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();
            var dnsChallenge  = (DnsChallenge)ctx.Challenge;
            var domainDetails = GetDomainId(dnsChallenge);

            var records = managedPath + domainDetails.DomainId + "/records";

            CleanUp(dnsChallenge, domainDetails, records);

            var recordNameToAdd = dnsChallenge.RecordName.Replace("." + domainDetails.DomainName, string.Empty);

            var wr = CreateRequest(records);

            wr.Method = "POST";

            using (var request = new StreamWriter(wr.GetRequestStream()))
            {
                var requestObject = new DomainRequest()
                {
                    name  = recordNameToAdd,
                    value = dnsChallenge.RecordValue,
                    ttl   = 600,
                    type  = "TXT"
                };

                var json = JsonConvert.SerializeObject(requestObject);

                request.Write(json);
            }

            using (var response = wr.GetResponse())
                using (var content = new StreamReader(response.GetResponseStream()))
                {
                    var resp       = content.ReadToEnd();
                    var respObject = JsonConvert.DeserializeObject <DomainRequest>(resp);
                    if (string.IsNullOrEmpty(respObject.id))
                    {
                        //Failed
                    }
                }
        }
예제 #11
0
        public void CleanUp(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();

            var dnsChallenge    = ctx.Challenge as DnsChallenge;
            var httpChallenge   = ctx.Challenge as HttpChallenge;
            var tlsSniChallenge = ctx.Challenge as TlsSniChallenge;

            if (dnsChallenge != null)
            {
                var json = new
                {
                    Label          = "Manual Challenge Handler - DNS",
                    CleanUpTime    = DateTime.Now,
                    ChallengeToken = dnsChallenge.Token,
                    ChallengeType  = "DNS",
                    Description    = new[]
                    {
                        "The Challenge has been completed -- you can now remove the",
                        "Resource Record created previously with the following",
                        "characteristics:",
                    },
                    DnsDetails = new
                    {
                        RRType  = "TXT",
                        RRName  = dnsChallenge.RecordName,
                        RRValue = dnsChallenge.RecordValue,
                    }
                };

                // Compute the output message
                var message = OutputJson
                                        ? JsonConvert.SerializeObject(json, Formatting.Indented)
                                        : $@"== {json.Label} ==
  * CleanUp Time:     [{json.CleanUpTime}]
  * Challenge Token:  [{json.ChallengeToken}]

{string.Join(Environment.NewLine, json.Description)}
  * RR Type:  [{json.DnsDetails.RRType}]
  * RR Name:  [{json.DnsDetails.RRName}]
  * RR Value: [{json.DnsDetails.RRValue}]
------------------------------------
";

                ctx.Out.WriteLine(message);
                _writer?.WriteLine(message);
                _writer?.Flush();
            }
            else if (httpChallenge != null)
            {
                var json = new
                {
                    Label          = "Manual Challenge Handler - HTTP",
                    CleanUpTime    = DateTime.Now,
                    ChallengeToken = httpChallenge.Token,
                    ChallengeType  = "HTTP",
                    Description    = new[]
                    {
                        "The Challenge has been completed -- you can now remove the",
                        "file created previously with the following characteristics:",
                    },
                    HttpDetails = new
                    {
                        HttpUrl     = httpChallenge.FileUrl,
                        FilePath    = httpChallenge.FilePath,
                        FileContent = httpChallenge.FileContent,
                        MimeType    = "text/plain",
                    }
                };

                // Compute the output message
                var message = OutputJson
                                        ? JsonConvert.SerializeObject(json, Formatting.Indented)
                                        : $@"== {json.Label} ==
  * CleanUp Time:     [{json.CleanUpTime}]
  * Challenge Token:  [{json.ChallengeToken}]

{string.Join(Environment.NewLine, json.Description)}
  * HTTP URL:     [{httpChallenge.FileUrl}]
  * File Path:    [{httpChallenge.FilePath}]
  * File Content: [{httpChallenge.FileContent}]
  * MIME Type:    [text/plain]
------------------------------------
";
                ctx.Out.WriteLine(message);
                _writer?.WriteLine(message);
                _writer?.Flush();
            }
            else if (tlsSniChallenge != null)
            {
                var json = new
                {
                    Label          = "Manual Challenge Handler - TLS-SNI",
                    CleanUpTime    = DateTime.Now,
                    ChallengeToken = httpChallenge.Token,
                    ChallengeType  = "TLS-SNI",
                    Description    = new[]
                    {
                        "The Challenge has been completed -- you can now remove the",
                        "self-signed certificates and SNI configuration on your",
                        "web server for the following token:",
                    },
                    TlsSniDetails = new
                    {
                        Token          = tlsSniChallenge.Token,
                        IterationCount = tlsSniChallenge.IterationCount,
                    }
                };

                // Compute the output message
                var message = OutputJson
                    ? JsonConvert.SerializeObject(json, Formatting.Indented)
                    : $@"== {json.Label} ==
  * CleanUp Time:     [{json.CleanUpTime}]
  * Challenge Token:  [{json.ChallengeToken}]

{string.Join(Environment.NewLine, json.Description)}
  * Token:        [{json.TlsSniDetails.Token}]
  * Iterations:   [{json.TlsSniDetails.IterationCount}]
------------------------------------
";
                ctx.Out.WriteLine(message);
                _writer?.WriteLine(message);
                _writer?.Flush();
            }
            else
            {
                var ex = new InvalidOperationException("unsupported Challenge type");
                ex.Data["challengeType"] = ctx.GetType();
                throw ex;
            }
        }
 public void CleanUp(ChallengeHandlingContext ctx)
 {
     AssertNotDisposed();
     EditDns((DnsChallenge)ctx.Challenge, true, ctx.Out);
 }
 public void Handle(ChallengeHandlingContext ctx)
 {
     AssertNotDisposed();
     EditDns((DnsChallenge)ctx.Challenge, false, ctx.Out);
 }
예제 #14
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            if (string.IsNullOrEmpty(SecretId))
            {
                throw new ArgumentNullException(nameof(SecretId));
            }
            if (string.IsNullOrEmpty(SecretKey))
            {
                throw new ArgumentNullException(nameof(SecretKey));
            }
            if (string.IsNullOrEmpty(Line))
            {
                throw new ArgumentNullException(nameof(Line));
            }

            DnsChallenge dnsChallenge = (DnsChallenge)ctx.Challenge;

            var cns = new QCloudAPI_SDK.Module.Cns();

            cns.setConfig(new SortedDictionary <string, object>(StringComparer.Ordinal)
            {
                { "SecretId", SecretId }, { "SecretKey", SecretKey }, { "RequestMethod", "GET" }
            });

            var  recordName = dnsChallenge.RecordName;
            var  topAndSecondLevelNameBuilder = new StringBuilder();
            var  subDomainNameBuilder         = new StringBuilder();
            byte level = 0;

            for (int i = recordName.Length - 1; i >= 0; i--)
            {
                if (recordName[i] == '.' && level < 2)
                {
                    level++;
                }
                if (level < 2)
                {
                    topAndSecondLevelNameBuilder.Insert(0, recordName[i]);
                }
                else
                {
                    subDomainNameBuilder.Insert(0, recordName[i]);
                }
            }
            var topAndSecondLevelName = topAndSecondLevelNameBuilder.ToString();
            var subDomainName         = subDomainNameBuilder.ToString();

            subDomainName = subDomainName.Substring(0, subDomainName.Length - 1);
            ctx.Out.WriteLine("Getting domain information for " + recordName + '.');
            var recordListResponse = (JObject)JsonConvert.DeserializeObject(cns.Call("RecordList", new SortedDictionary <string, object>(StringComparer.Ordinal)
            {
                { "domain", topAndSecondLevelName },
                { "offset", 0 },
                { "length", 100 },
                { "subDomain", subDomainName },
                { "recordType", "TXT" },
            }));

            ThrowQCloudError(recordListResponse);

            var record = (JArray)recordListResponse["data"]["records"];

            ctx.Out.WriteLine(record.Count + " existing record for " + recordName + " is found.");

            if (record.Count == 1)
            {
                //If record already exist.
                ctx.Out.WriteLine("Adding new record " + subDomainName + " in domain " + topAndSecondLevelName + ".");
                var domainListResponse = (JObject)JsonConvert.DeserializeObject(cns.Call("RecordModify", new SortedDictionary <string, object>(StringComparer.Ordinal)
                {
                    { "domain", topAndSecondLevelName },
                    { "recordId", (int)record[0]["id"] },
                    { "subDomain", subDomainName },
                    { "recordType", "TXT" },
                    { "recordLine", (string)record[0]["line"] },
                    { "value", dnsChallenge.RecordValue },
                }));
                ThrowQCloudError(domainListResponse);
                ctx.Out.WriteLine("Updated DNS record of type [TXT] with name [{0}]",
                                  dnsChallenge.RecordName);
            }
            else if (record.Count == 0)
            {
                //If record does not exist.
                ctx.Out.WriteLine("Updating record " + subDomainName + " in domain " + topAndSecondLevelName + ".");
                var domainListResponse = (JObject)JsonConvert.DeserializeObject(cns.Call("RecordCreate", new SortedDictionary <string, object>(StringComparer.Ordinal)
                {
                    { "domain", topAndSecondLevelName },
                    { "subDomain", subDomainName },
                    { "recordType", "TXT" },
                    { "recordLine", Line },
                    { "value", dnsChallenge.RecordValue },
                }));
                ThrowQCloudError(domainListResponse);
                ctx.Out.WriteLine("Created DNS record of type [TXT] with name [{0}]",
                                  dnsChallenge.RecordName);
            }
            else
            {
                throw new InvalidOperationException("There should not be more than one DNS txt record for the name.");
            }
        }
예제 #15
0
 public void CleanUp(ChallengeHandlingContext ctx)
 {
     throw new NotSupportedException("provider does not support clean up");
 }
예제 #16
0
        public AuthorizeChallenge HandleChallenge(AuthorizationState authzState,
                                                  string challengeType,
                                                  string handlerName, IReadOnlyDictionary <string, object> handlerParams,
                                                  bool cleanUp = false)
        {
            var provider = ChallengeHandlerExtManager.GetProvider(handlerName);

            if (provider == null)
            {
                throw new InvalidOperationException("unable to resolve Challenge Handler provider")
                      .With("handlerName", handlerName);
            }

            var authzChallenge = authzState.Challenges.FirstOrDefault(x => x.Type == challengeType);

            if (authzChallenge == null)
            {
                throw new ArgumentOutOfRangeException(nameof(challengeType),
                                                      "no challenge found matching requested type")
                      .With("challengeType", challengeType);
            }

            if (!provider.IsSupported(authzChallenge.Challenge))
            {
                throw new InvalidOperationException("Challenge Handler does not support given Challenge")
                      .With("handlerName", handlerName)
                      .With("challengeType", authzChallenge.Challenge.Type);
            }

            var handler = provider.GetHandler(authzChallenge.Challenge, handlerParams);

            if (handler == null)
            {
                throw new InvalidOperationException("no Challenge Handler provided for given Challenge")
                      .With("handlerName", handlerName)
                      .With("challengeType", authzChallenge.Challenge.Type);
            }

            authzChallenge.HandlerName = handlerName;

            using (var outWriter = new StringWriter())
            {
                var ctx = new ChallengeHandlingContext(authzChallenge.Challenge, outWriter);
                if (cleanUp)
                {
                    handler.CleanUp(ctx);
                    authzChallenge.HandlerCleanUpDate    = DateTime.Now;
                    authzChallenge.HandlerCleanUpMessage = outWriter.ToString();
                }
                else
                {
                    handler.Handle(ctx);
                    authzChallenge.HandlerHandleDate    = DateTime.Now;
                    authzChallenge.HandlerHandleMessage = outWriter.ToString();
                }
            }

            handler.Dispose();

            return(authzChallenge);
        }
예제 #17
0
        public void Handle(ChallengeHandlingContext ctx)
        {
            AssertNotDisposed();

            var dnsChallenge  = ctx.Challenge as DnsChallenge;
            var httpChallenge = ctx.Challenge as HttpChallenge;

            if (dnsChallenge != null)
            {
                var json = new
                {
                    Label          = "Manual Challenge Handler - DNS",
                    HandleTime     = DateTime.Now,
                    ChallengeToken = dnsChallenge.Token,
                    ChallengeType  = "DNS",
                    Description    = new[]
                    {
                        "To complete this Challenge please create a new Resource",
                        "Record (RR) with the following characteristics:",
                    },
                    DnsDetails = new
                    {
                        RRType  = "TXT",
                        RRName  = dnsChallenge.RecordName,
                        RRValue = dnsChallenge.RecordValue,
                    }
                };

                // Compute the output message
                var message = OutputJson
                                        ? JsonConvert.SerializeObject(json, Formatting.Indented)
                                        : $@"== {json.Label} ==
  * Handle Time:      [{json.HandleTime}]
  * Challenge Token:  [{json.ChallengeToken}]

{string.Join(Environment.NewLine, json.Description)}
  * RR Type:  [{json.DnsDetails.RRType}]
  * RR Name:  [{json.DnsDetails.RRName}]
  * RR Value: [{json.DnsDetails.RRValue}]
------------------------------------
";

                ctx.Out.WriteLine(message);
                _writer?.WriteLine(message);
                _writer?.Flush();
            }
            else if (httpChallenge != null)
            {
                var json = new
                {
                    Label          = "Manual Challenge Handler - HTTP",
                    HandleTime     = DateTime.Now,
                    ChallengeToken = httpChallenge.Token,
                    ChallengeType  = "HTTP",
                    Description    = new[]
                    {
                        "To complete this Challenge please create a new file",
                        "under the server that is responding to the hostname",
                        "and path given with the following characteristics:",
                    },
                    HttpDetails = new
                    {
                        HttpUrl     = httpChallenge.FileUrl,
                        FilePath    = httpChallenge.FilePath,
                        FileContent = httpChallenge.FileContent,
                        MimeType    = "text/plain",
                    }
                };

                // Compute the output message
                var message = OutputJson
                                        ? JsonConvert.SerializeObject(json, Formatting.Indented)
                                        : $@"== {json.Label} ==
  * Handle Time:      [{json.HandleTime}]
  * Challenge Token:  [{json.ChallengeToken}]

{string.Join(Environment.NewLine, json.Description)}
  * HTTP URL:     [{json.HttpDetails.HttpUrl}]
  * File Path:    [{json.HttpDetails.FilePath}]
  * File Content: [{json.HttpDetails.FileContent}]
  * MIME Type:    [{json.HttpDetails.MimeType}]
------------------------------------										
";

                ctx.Out.WriteLine(message);
                _writer?.WriteLine(message);
                _writer?.Flush();
            }
            else
            {
                var ex = new InvalidOperationException("unsupported Challenge type");
                ex.Data["challengeType"] = ctx.GetType();
                throw ex;
            }
        }