Пример #1
0
        public async Task <User> CreateUser(string _userName, string _password, string _email)
        {
            User current = new User();

            current.Username = _userName;

            byte[] salt = _cryptographyService.CreateSalt();
            current.Password = _cryptographyService.GenerateHash(_password, salt);
            current.Email    = _email;
            current.Salt     = Convert.ToBase64String(salt);

            current.Id = await _userRepository.AddUser(current);

            return(current);
        }
Пример #2
0
        Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, IPackage nugetPackage)
        {
            var package = packageRegistration.Packages
                          .Where(pv => pv.Version == nugetPackage.Version.ToString())
                          .SingleOrDefault();

            if (package != null)
            {
                throw new EntityException("A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            // TODO: add flattened authors, and other properties
            // TODO: add package size
            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            package = new Package
            {
                Version      = nugetPackage.Version.ToString(),
                Description  = nugetPackage.Description,
                ReleaseNotes = nugetPackage.ReleaseNotes,
                RequiresLicenseAcceptance = nugetPackage.RequireLicenseAcceptance,
                HashAlgorithm             = Const.Sha512HashAlgorithmId,
                Hash            = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes()),
                PackageFileSize = packageFileStream.Length,
                Created         = now,
                LastUpdated     = now,
                Published       = DateTime.UtcNow,
                Copyright       = nugetPackage.Copyright,
                IsPrerelease    = !nugetPackage.IsReleaseVersion(),
                Listed          = true
            };

            if (nugetPackage.IconUrl != null)
            {
                package.IconUrl = nugetPackage.IconUrl.ToString();
            }
            if (nugetPackage.LicenseUrl != null)
            {
                package.LicenseUrl = nugetPackage.LicenseUrl.ToString();
            }
            if (nugetPackage.ProjectUrl != null)
            {
                package.ProjectUrl = nugetPackage.ProjectUrl.ToString();
            }
            if (nugetPackage.Summary != null)
            {
                package.Summary = nugetPackage.Summary;
            }
            if (nugetPackage.Tags != null)
            {
                package.Tags = nugetPackage.Tags;
            }
            if (nugetPackage.Title != null)
            {
                package.Title = nugetPackage.Title;
            }

            foreach (var author in nugetPackage.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }

            foreach (var dependency in nugetPackage.Dependencies)
            {
                package.Dependencies.Add(new PackageDependency {
                    Id = dependency.Id, VersionSpec = dependency.VersionSpec.ToStringSafe()
                });
            }

            package.FlattenedAuthors      = package.Authors.Flatten();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }
Пример #3
0
        private Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, INupkg nugetPackage)
        {
            var package = packageRegistration.Packages.SingleOrDefault(pv => pv.Version == nugetPackage.Metadata.Version.ToString());

            if (package != null)
            {
                throw new EntityException(
                          "A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            package = new Package
            {
                Version      = nugetPackage.Metadata.Version.ToString(),
                Description  = nugetPackage.Metadata.Description,
                ReleaseNotes = nugetPackage.Metadata.ReleaseNotes,
                RequiresLicenseAcceptance = nugetPackage.Metadata.RequireLicenseAcceptance,
                HashAlgorithm             = Constants.Sha512HashAlgorithmId,
                Hash                = _cryptoService.GenerateHash(packageFileStream.ReadAllBytes()),
                PackageFileSize     = packageFileStream.Length,
                Created             = now,
                Language            = nugetPackage.Metadata.Language,
                LastUpdated         = now,
                Published           = now,
                Copyright           = nugetPackage.Metadata.Copyright,
                IsPrerelease        = !nugetPackage.Metadata.IsReleaseVersion(),
                Listed              = true,
                PackageRegistration = packageRegistration,
                Summary             = nugetPackage.Metadata.Summary,
                Tags                = nugetPackage.Metadata.Tags,
                Title               = nugetPackage.Metadata.Title,
            };

            package.IconUrl          = nugetPackage.Metadata.IconUrl.ToStringOrNull();
            package.LicenseUrl       = nugetPackage.Metadata.LicenseUrl.ToStringOrNull();
            package.ProjectUrl       = nugetPackage.Metadata.ProjectUrl.ToStringOrNull();
            package.MinClientVersion = nugetPackage.Metadata.MinClientVersion.ToStringOrNull();

            foreach (var author in nugetPackage.Metadata.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }

            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();

            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {
                        TargetFramework = supportedFramework
                    });
                }
            }

            foreach (var dependencySet in nugetPackage.Metadata.DependencySets)
            {
                if (dependencySet.Dependencies.Count == 0)
                {
                    package.Dependencies.Add(
                        new PackageDependency
                    {
                        Id              = null,
                        VersionSpec     = null,
                        TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull()
                    });
                }
                else
                {
                    foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
                    {
                        package.Dependencies.Add(
                            new PackageDependency
                        {
                            Id              = dependency.Id,
                            VersionSpec     = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
                            TargetFramework = dependency.TargetFramework.ToShortNameOrNull()
                        });
                    }
                }
            }

            package.FlattenedAuthors      = package.Authors.Flatten();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }
Пример #4
0
        Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, IPackage nugetPackage)
        {
            var package = packageRegistration.Packages
                          .Where(pv => pv.Version == nugetPackage.Version.ToString())
                          .SingleOrDefault();

            if (package != null)
            {
                throw new EntityException("A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
            }

            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            package = new Package
            {
                Version      = nugetPackage.Version.ToString(),
                Description  = nugetPackage.Description,
                ReleaseNotes = nugetPackage.ReleaseNotes,
                RequiresLicenseAcceptance = nugetPackage.RequireLicenseAcceptance,
                HashAlgorithm             = Constants.Sha512HashAlgorithmId,
                Hash            = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes()),
                PackageFileSize = packageFileStream.Length,
                Created         = now,
                Language        = nugetPackage.Language,
                LastUpdated     = now,
                Published       = now,
                Copyright       = nugetPackage.Copyright,
                IsPrerelease    = !nugetPackage.IsReleaseVersion(),
                Listed          = true,
            };

            if (nugetPackage.IconUrl != null)
            {
                package.IconUrl = nugetPackage.IconUrl.ToString();
            }
            if (nugetPackage.LicenseUrl != null)
            {
                package.LicenseUrl = nugetPackage.LicenseUrl.ToString();
            }
            if (nugetPackage.ProjectUrl != null)
            {
                package.ProjectUrl = nugetPackage.ProjectUrl.ToString();
            }
            if (nugetPackage.Summary != null)
            {
                package.Summary = nugetPackage.Summary;
            }
            if (nugetPackage.Tags != null)
            {
                package.Tags = nugetPackage.Tags;
            }
            if (nugetPackage.Title != null)
            {
                package.Title = nugetPackage.Title;
            }

            foreach (var author in nugetPackage.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }

            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();

            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {
                        TargetFramework = supportedFramework
                    });
                }
            }

            foreach (var dependencySet in nugetPackage.DependencySets)
            {
                if (dependencySet.Dependencies.Count == 0)
                {
                    package.Dependencies.Add(new PackageDependency
                    {
                        Id              = null,
                        VersionSpec     = null,
                        TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull()
                    });
                }
                else
                {
                    foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
                    {
                        package.Dependencies.Add(new PackageDependency
                        {
                            Id              = dependency.Id,
                            VersionSpec     = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
                            TargetFramework = dependency.TargetFramework.ToShortNameOrNull()
                        });
                    }
                }
            }

            foreach (var packageFile in nugetPackage.GetFiles().OrEmptyListIfNull())
            {
                var filePath    = packageFile.Path;
                var fileContent = " ";

                IList <string> extensions         = new List <string>();
                var            approvedExtensions = Configuration.ReadAppSettings("PackageFileTextExtensions");
                if (!string.IsNullOrWhiteSpace(approvedExtensions))
                {
                    foreach (var extension in approvedExtensions.Split(',', ';'))
                    {
                        extensions.Add("." + extension);
                    }
                }

                try
                {
                    if (extensions.Contains(Path.GetExtension(filePath)))
                    {
                        fileContent = packageFile.GetStream().ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    // Log but swallow the exception
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }

                package.Files.Add(new PackageFile
                {
                    FilePath    = filePath,
                    FileContent = fileContent,
                });
            }

            package.FlattenedAuthors      = package.Authors.Flatten();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }
Пример #5
0
        Package CreatePackageFromNuGetPackage(PackageRegistration packageRegistration, IPackage nugetPackage)
        {
            var package = packageRegistration.Packages
                          .Where(pv => pv.Version == nugetPackage.Version.ToString())
                          .SingleOrDefault();

            if (package != null)
            {
                switch (package.Status)
                {
                case PackageStatusType.Rejected:
                    throw new EntityException(string.Format("This package has been {0} and can no longer be submitted.", package.Status.GetDescriptionOrValue().ToLower()));

                case PackageStatusType.Submitted:
                    //continue on
                    break;

                default:
                    throw new EntityException("A package with identifier '{0}' and version '{1}' already exists.", packageRegistration.Id, package.Version);
                }
            }

            var now = DateTime.UtcNow;
            var packageFileStream = nugetPackage.GetStream();

            //if new package versus updating an existing package.
            if (package == null)
            {
                package = new Package();
            }

            package.Version      = nugetPackage.Version.ToString();
            package.Description  = nugetPackage.Description;
            package.ReleaseNotes = nugetPackage.ReleaseNotes;
            package.RequiresLicenseAcceptance = nugetPackage.RequireLicenseAcceptance;
            package.HashAlgorithm             = Constants.Sha512HashAlgorithmId;
            package.Hash            = cryptoSvc.GenerateHash(packageFileStream.ReadAllBytes());
            package.PackageFileSize = packageFileStream.Length;
            package.Created         = now;
            package.Language        = nugetPackage.Language;
            package.LastUpdated     = now;
            package.Published       = now;
            package.Copyright       = nugetPackage.Copyright;
            package.IsPrerelease    = !nugetPackage.IsReleaseVersion();
            package.Listed          = false;
            package.Status          = PackageStatusType.Submitted;
            package.SubmittedStatus = PackageSubmittedStatusType.Ready;
            package.ApprovedDate    = null;

            if (package.ReviewedDate.HasValue)
            {
                package.SubmittedStatus = PackageSubmittedStatusType.Updated;
            }

            //we don't moderate prereleases
            if (package.IsPrerelease)
            {
                package.Listed = true;
                package.Status = PackageStatusType.Exempted;
            }
            if (packageRegistration.IsTrusted)
            {
                package.Listed       = true;
                package.Status       = PackageStatusType.Approved;
                package.ReviewedDate = now;
                package.ApprovedDate = now;
            }

            package.IconUrl    = nugetPackage.IconUrl == null ? string.Empty : nugetPackage.IconUrl.ToString();
            package.LicenseUrl = nugetPackage.LicenseUrl == null ? string.Empty : nugetPackage.LicenseUrl.ToString();
            package.ProjectUrl = nugetPackage.ProjectUrl == null ? string.Empty : nugetPackage.ProjectUrl.ToString();
            package.Summary    = nugetPackage.Summary ?? string.Empty;
            package.Tags       = nugetPackage.Tags ?? string.Empty;
            package.Title      = nugetPackage.Title ?? string.Empty;

            foreach (var item in package.Authors.OrEmptyListIfNull().ToList())
            {
                packageAuthorRepo.DeleteOnCommit(item);
            }
            packageAuthorRepo.CommitChanges();
            foreach (var author in nugetPackage.Authors)
            {
                package.Authors.Add(new PackageAuthor {
                    Name = author
                });
            }

            foreach (var item in package.SupportedFrameworks.OrEmptyListIfNull().ToList())
            {
                packageFrameworksRepo.DeleteOnCommit(item);
            }
            packageFrameworksRepo.CommitChanges();
            var supportedFrameworks = GetSupportedFrameworks(nugetPackage).Select(fn => fn.ToShortNameOrNull()).ToArray();

            if (!supportedFrameworks.AnySafe(sf => sf == null))
            {
                foreach (var supportedFramework in supportedFrameworks)
                {
                    package.SupportedFrameworks.Add(new PackageFramework {
                        TargetFramework = supportedFramework
                    });
                }
            }

            foreach (var item in package.Dependencies.OrEmptyListIfNull().ToList())
            {
                packageDependenciesRepo.DeleteOnCommit(item);
            }
            packageDependenciesRepo.CommitChanges();
            foreach (var dependencySet in nugetPackage.DependencySets)
            {
                if (dependencySet.Dependencies.Count == 0)
                {
                    package.Dependencies.Add(new PackageDependency
                    {
                        Id              = null,
                        VersionSpec     = null,
                        TargetFramework = dependencySet.TargetFramework.ToShortNameOrNull()
                    });
                }
                else
                {
                    foreach (var dependency in dependencySet.Dependencies.Select(d => new { d.Id, d.VersionSpec, dependencySet.TargetFramework }))
                    {
                        package.Dependencies.Add(new PackageDependency
                        {
                            Id              = dependency.Id,
                            VersionSpec     = dependency.VersionSpec == null ? null : dependency.VersionSpec.ToString(),
                            TargetFramework = dependency.TargetFramework.ToShortNameOrNull()
                        });
                    }
                }
            }

            foreach (var item in package.Files.OrEmptyListIfNull().ToList())
            {
                packageFilesRepo.DeleteOnCommit(item);
            }
            packageFilesRepo.CommitChanges();
            foreach (var packageFile in nugetPackage.GetFiles().OrEmptyListIfNull())
            {
                var filePath    = packageFile.Path;
                var fileContent = " ";

                IList <string> extensions         = new List <string>();
                var            approvedExtensions = Configuration.ReadAppSettings("PackageFileTextExtensions");
                if (!string.IsNullOrWhiteSpace(approvedExtensions))
                {
                    foreach (var extension in approvedExtensions.Split(',', ';'))
                    {
                        extensions.Add("." + extension);
                    }
                }

                try
                {
                    var extension = Path.GetExtension(filePath);

                    if (extension != null)
                    {
                        if (extensions.Contains(extension))
                        {
                            fileContent = packageFile.GetStream().ReadToEnd();
                        }
                        else if (extension.Equals(".exe", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var bytes    = packageFile.GetStream().ReadAllBytes();
                            var md5Hash  = BitConverter.ToString(Convert.FromBase64String(cryptoSvc.GenerateHash(bytes, "MD5"))).Replace("-", string.Empty);
                            var sha1Hash = BitConverter.ToString(Convert.FromBase64String(cryptoSvc.GenerateHash(bytes, "SHA1"))).Replace("-", string.Empty);

                            fileContent = string.Format("md5: {0} | sha1: {1}", md5Hash, sha1Hash);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log but swallow the exception
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }

                package.Files.Add(new PackageFile
                {
                    FilePath    = filePath,
                    FileContent = fileContent,
                });
            }

            package.FlattenedAuthors      = package.Authors.Flatten();
            package.FlattenedDependencies = package.Dependencies.Flatten();

            return(package);
        }