Пример #1
0
        public static DomainLicense Parse(string licenseText)
        {
            var result = new DomainLicense();

            string[] lines = licenseText.Split('\n');
            foreach (string l in lines)
            {
                int colon = l.IndexOf(':');
                if (colon < 1)
                {
                    continue;
                }
                string key   = l.Substring(0, colon).Trim().ToLowerInvariant();
                string value = l.Substring(colon + 1).Trim();

                switch (key)
                {
                case "domain": result.Domain = value; break;

                case "owner": result.OwnerName = value; break;

                case "issued": result.Issued = DateTime.Parse(value); break;

                case "expires":

                    if (value.Trim().Length == 0)
                    {
                        result.Expires = null;
                    }
                    else
                    {
                        result.Expires = DateTime.Parse(value);
                    }

                    break;

                case "features":
                    List <Guid> ids   = new List <Guid>();
                    string[]    parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string p in parts)
                    {
                        ids.Add(new Guid(p));
                    }
                    result.Features = ids;
                    break;
                }
            }

            return(result);
        }
Пример #2
0
        public static DomainLicense Parse(string licenseText)
        {
            var result = new DomainLicense();

            string[] lines = licenseText.Split('\n');
            foreach (string l in lines)
            {
                int colon = l.IndexOf(':');
                if (colon < 1) continue;
                string key = l.Substring(0, colon).Trim().ToLowerInvariant();
                string value = l.Substring(colon + 1).Trim();

                switch (key)
                {
                    case "domain": result.Domain = value; break;
                    case "owner": result.OwnerName = value; break;
                    case "issued": result.Issued = DateTime.Parse(value); break;
                    case "expires":

                        if (value.Trim().Length == 0)
                        {
                            result.Expires = null;
                        }
                        else
                        {
                            result.Expires = DateTime.Parse(value);
                        }

                        break;
                    case "features":
                        List<Guid> ids = new List<Guid>();
                        string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string p in parts)
                        {
                            ids.Add(new Guid(p));
                        }
                        result.Features = ids;
                        break;
                }
            }

            return result;
        }
Пример #3
0
        /// <summary>
        /// Returns a dictionary that associates domains with licenses.
        /// To retrieve licenses for domain aaa.bbb.ccc, use the results for all domaincs "aaa.bbb.ccc", "bbb.ccc" and "ccc".
        /// </summary>
        /// <param name="publicKeyXml"></param>
        /// <param name="licensesAndSignatures"></param>
        /// <returns></returns>
        public Dictionary <string, List <DomainLicense> > DeserializeAll(string publicKeyXml, ICollection <KeyValuePair <string, string> > licensesAndSignatures)
        {
            var licenses = new Dictionary <string, List <DomainLicense> >(StringComparer.OrdinalIgnoreCase);

            using (var r = new RSACryptoServiceProvider(2048))
            {
                try
                {
                    r.FromXmlString(publicKeyXml);

                    foreach (var licenseAndSignature in licensesAndSignatures)
                    {
                        var licenseBytes  = Convert.FromBase64String(licenseAndSignature.Key);
                        var domainLicense = DomainLicense.Parse(Encoding.UTF8.GetString(licenseBytes));

                        if (!r.VerifyData(licenseBytes, new SHA256Managed(),
                                          Convert.FromBase64String(licenseAndSignature.Value)))
                        {
                            throw new Exception("Signature failed for license of domain " + domainLicense.Domain);
                        }

                        string domain = domainLicense.Domain;
                        List <DomainLicense> forDomain;
                        if (!licenses.TryGetValue(domain, out forDomain))
                        {
                            forDomain        = new List <DomainLicense>();
                            licenses[domain] = forDomain;
                        }
                        forDomain.Add(domainLicense);
                    }
                }
                finally
                {
                    r.PersistKeyInCsp = false;
                }
            }

            return(licenses);
        }
Пример #4
0
        public static void AssertRemoteValidationCheckPasses(KeyHubWebDriver site,
                                                             string domain,
                                                             Guid appKey,
                                                             Guid featureCode,
                                                             string vendorPublicKey)
        {
            var licensingUrl = site.UrlFor("/api/LicenseValidation");

            var licensesAndSignature = new LicenseDownloader().RequestLicenses(licensingUrl, appKey,
                                                                               new Dictionary <string, List <Guid> >()
            {
                { domain, new List <Guid>()
                  {
                      featureCode
                  } }
            });

            var newLicenses = new LicenseDeserializer().DeserializeAll(vendorPublicKey, licensesAndSignature);

            DomainLicense license = newLicenses[domain].Single();

            Assert.Equal(license.Domain, domain);
            Assert.Contains(featureCode, license.Features);
        }