コード例 #1
0
        public void Should_be_able_to_encrypt_and_decrypt()
        {
            var keyPair = Helpers.GenerateKeyPairXml();

            byte[] encrypted = domainLicense.SerializeAndEncrypt(keyPair);

            var encryptedDomainLicense = new DomainLicense(encrypted, keyPair);
            var serializedDomainLicense = encryptedDomainLicense.SerializeUnencrypted();

            serializedDomainLicense.ShouldEqual(domainLicense.SerializeUnencrypted());
        }
コード例 #2
0
        public void Should_be_able_to_encrypt_and_decrypt()
        {
            var keyPair = Helpers.GenerateKeyPairXml();

            byte[] encrypted = domainLicense.SerializeAndEncrypt(keyPair);

            var encryptedDomainLicense  = new DomainLicense(encrypted, keyPair);
            var serializedDomainLicense = encryptedDomainLicense.SerializeUnencrypted();

            serializedDomainLicense.ShouldEqual(domainLicense.SerializeUnencrypted());
        }
コード例 #3
0
ファイル: LicenseVerifier.cs プロジェクト: zhaoyingju/resizer
        private Dictionary <string, List <DomainLicense> > DecryptAll(ICollection <byte[]> encrypted)
        {
            var licenses = new Dictionary <string, List <DomainLicense> >(StringComparer.OrdinalIgnoreCase);

            foreach (byte[] data in encrypted)
            {
                var    d      = new DomainLicense(data, this);
                string domain = NormalizeDomain(d.Domain);
                List <DomainLicense> forDomain;
                if (!licenses.TryGetValue(domain, out forDomain))
                {
                    forDomain        = new List <DomainLicense>();
                    licenses[domain] = forDomain;
                }
                forDomain.Add(d);
            }
            return(licenses);
        }
コード例 #4
0
        public DomainLicenseTests()
        {
            domain = "domain.com";
            ownerName = "John Doe";
            issued = DateTime.UtcNow;
            expires = DateTime.UtcNow.AddHours(24);

            featureOne = Guid.NewGuid();
            featureTwo = Guid.NewGuid();
            featureThree = Guid.NewGuid();
            features = new List<Guid> {
                featureOne,
                featureTwo,
                featureThree
            };

            domainLicense = new DomainLicense(
                domain,
                ownerName,
                issued,
                expires,
                features
            );
        }
コード例 #5
0
        public DomainLicenseTests()
        {
            domain    = "domain.com";
            ownerName = "John Doe";
            issued    = DateTime.UtcNow;
            expires   = DateTime.UtcNow.AddHours(24);

            featureOne   = Guid.NewGuid();
            featureTwo   = Guid.NewGuid();
            featureThree = Guid.NewGuid();
            features     = new List <Guid> {
                featureOne,
                featureTwo,
                featureThree
            };

            domainLicense = new DomainLicense(
                domain,
                ownerName,
                issued,
                expires,
                features
                );
        }
コード例 #6
0
            public WhenLocalCacheContainsLicenseInformationTests()
            {
                featureOne   = Guid.NewGuid();
                featureTwo   = Guid.NewGuid();
                featureThree = Guid.NewGuid();
                featureFour  = Guid.NewGuid();
                featureFive  = Guid.NewGuid();
                featureSix   = Guid.NewGuid();
                featureSeven = Guid.NewGuid();

                var httpClient = Substitute.For <IHttpClient>();

                string keyPair = Helpers.GenerateKeyPairXml();

                // featureOne and featureThree belong to an expired license
                var expiredDomainLicense = new DomainLicense(
                    "expired.com",
                    "John Doe",
                    DateTime.UtcNow.AddYears(-1),
                    DateTime.UtcNow.AddHours(24).AddYears(-1),
                    new List <Guid> {
                    featureOne,
                    featureThree
                }
                    );

                var issued  = DateTime.UtcNow;
                var expires = DateTime.UtcNow.AddHours(24);
                // featureTwo and featureFive have already been verified
                var cachedDomainLicense = new DomainLicense(
                    "domain.com",
                    "John Doe",
                    issued,
                    expires,
                    new List <Guid> {
                    featureTwo,
                    featureFive
                }
                    );

                // featureSix and featureSeven are Pending, but only featureSix will be enabled
                var keyHubDomainLicense = new DomainLicense(
                    "domain.com",
                    "John Doe",
                    issued,
                    expires,
                    new List <Guid> {
                    featureTwo,
                    featureFour,   // Rejected in the past is now enabled
                    featureFive,
                    featureSix     // Pending new feature
                }
                    );

                httpClient.Send(Arg.Any <HttpRequest>()).Returns(new HttpResponse {
                    Content = Helpers.GenerateValidXmlResponseFromKeyHub(keyPair, new List <DomainLicense> {
                        keyHubDomainLicense
                    })
                });

                licenseStore = Substitute.For <ILicenseStore>();
                licenseStore.GetLicenses().Returns(new List <byte[]> {
                    expiredDomainLicense.SerializeAndEncrypt(keyPair),
                    cachedDomainLicense.SerializeAndEncrypt(keyPair)
                });

                var featureStates = new Dictionary <string, Dictionary <Guid, FeatureState> > {
                    {
                        "expired.com",
                        new Dictionary <Guid, FeatureState> {
                            { featureOne, FeatureState.Enabled },
                            { featureThree, FeatureState.Enabled }
                        }
                    },
                    {
                        "domain.com",
                        new Dictionary <Guid, FeatureState> {
                            { featureTwo, FeatureState.Enabled },
                            { featureFour, FeatureState.Rejected },
                            { featureFive, FeatureState.Enabled },
                            { featureSix, FeatureState.Pending },
                            { featureSeven, FeatureState.Pending }
                        }
                    }
                };

                var pendingFeatures = new Dictionary <string, List <Guid> > {
                    {
                        "domain.com",
                        new List <Guid> {
                            featureSix,
                            featureSeven
                        }
                    }
                };

                verifyAction           = new VerifyAction(httpClient, keyPair, Guid.NewGuid(), licenseStore, featureStates, pendingFeatures);
                verifyAction.Finished += verifyAction_Finished;
            }
コード例 #7
0
        /// <summary>
        /// Vaidate DomainLicenses
        /// </summary>
        /// <param name="appKey">Customer App key</param>
        /// <param name="domainValidations">List of domains to validate</param>
        /// <returns>List of DomainValidationResult</returns>
        public IEnumerable <DomainValidationResult> Validate(Guid appKey, IEnumerable <DomainValidation> domainValidations)
        {
            using (var context = dataContextFactory.Create())
            {
                DeleteExpiredDomainLicenses();

                customerApp = context.CustomerAppKeys.Where(x => x.AppKey == appKey)
                              .Select(x => x.CustomerApp)
                              .Include(x => x.LicenseCustomerApps)
                              .FirstOrDefault();

                if (customerApp == null)
                {
                    // need to notify, no customerApp
                    throw new Exception(string.Format("CustomerApp with appKey={0} has not found", appKey));
                }

                IEnumerable <Guid> matchedLicenseIds = GetValidLicences(customerApp).ToList();

                var domainLicenses = new List <DomainLicense>();

                var alreadyFailedDomainLicenses = new List <DomainLicense>();

                IEqualityComparer <DomainLicense> equalityComparer = new DomainLicenseEqualityComparer();

                foreach (DomainValidation domainValidation in domainValidations)
                {
                    string domainName  = domainValidation.DomainName;
                    Guid   featureCode = domainValidation.FeatureCode;

                    var domainLicense = context.DomainLicenses
                                        .Include(x => x.License.Sku.SkuFeatures.Select(s => s.Feature))
                                        .FirstOrDefault(x => x.DomainName == domainName &&
                                                        matchedLicenseIds.Contains(x.LicenseId)
                                                        &&
                                                        x.License.Sku.SkuFeatures.Select(
                                                            s => s.Feature.FeatureCode)
                                                        .Contains(featureCode));

                    if (domainLicense == null)
                    {
                        var featureLicense = (from x in context.Licenses where matchedLicenseIds.Contains(x.ObjectId) select x)
                                             .Include(x => x.Sku.SkuFeatures.Select(s => s.Feature))
                                             .FirstOrDefault(
                            x => x.Sku.SkuFeatures.Select(s => s.Feature.FeatureCode).Any(y => y == featureCode));

                        if (featureLicense == null)
                        {
                            // we do not have license on a feature or we just do not have this one
                            // need to notify
                            continue;
                        }

                        domainLicense = new DomainLicense
                        {
                            DomainName           = domainName,
                            AutomaticallyCreated = true,
                            DomainLicenseIssued  = featureLicense.Sku.CalculateDomainIssueDate(),
                            DomainLicenseExpires = featureLicense.Sku.CalculateAutoDomainExpiration(),
                            KeyBytes             = featureLicense.Sku.PrivateKey.KeyBytes,
                            License   = featureLicense,
                            LicenseId = featureLicense.ObjectId
                        };

                        if (!context.DomainLicenses.Any(x => x.DomainLicenseId == domainLicense.DomainLicenseId) &&
                            !alreadyFailedDomainLicenses.Contains(domainLicense, equalityComparer))
                        {
                            context.DomainLicenses.Add(domainLicense);
                            if (!context.SaveChanges(OnValidationFailed))
                            {
                                alreadyFailedDomainLicenses.Add(domainLicense);
                            }
                        }
                    }

                    if (!domainLicenses.Contains(domainLicense, equalityComparer) &&
                        !alreadyFailedDomainLicenses.Contains(domainLicense, equalityComparer))
                    {
                        domainLicenses.Add(domainLicense);
                    }
                }
                return(ConvertToDomainValidationResults(domainLicenses));
            }
        }
コード例 #8
0
 public void Init()
 {
     instance = new DomainLicense();
 }
コード例 #9
0
ファイル: LicenseVerifier.cs プロジェクト: eakova/resizer
        private Dictionary<string, List<DomainLicense>> DecryptAll(ICollection<byte[]> encrypted)
        {
            var licenses = new Dictionary<string, List<DomainLicense>>(StringComparer.OrdinalIgnoreCase);

                foreach (byte[] data in encrypted) {
                    var d= new DomainLicense(data,this);
                    string domain = NormalizeDomain(d.Domain);
                    List<DomainLicense> forDomain;
                    if (!licenses.TryGetValue(domain, out forDomain)) {
                        forDomain = new List<DomainLicense>();
                        licenses[domain] = forDomain;
                    }
                    forDomain.Add(d);
                }
            return licenses;
        }
コード例 #10
0
 public static bool AnyEquals(this DbSet <DomainLicense> domainLicenseSet, DomainLicense domainLicense)
 {
     return(domainLicenseSet.Any(x => x.DomainName == domainLicense.DomainName && x.LicenseId == domainLicense.LicenseId));
 }
コード例 #11
0
        private static ActionResult LoadDomainLicenseForRemoval(IDataContextByUser context, Guid key, out DomainLicense domainLicense)
        {
            domainLicense = context.DomainLicenses
                            .Include(dl => dl.License)
                            .Include(dl => dl.License.Sku)
                            .SingleOrDefault(x => x.DomainLicenseId == key);

            if (domainLicense == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            if (!domainLicense.CanBeManuallyDeleted)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }

            return(null);
        }