Ejemplo n.º 1
0
        private Response GetMostRecentMajorVersion(INapackStorageManager napackManager, string name, int major)
        {
            int minorVersion = 0;

            return(this.GetPackage(napackManager, name, major, (majorVersion) =>
            {
                minorVersion = majorVersion.Versions.Max(version => version.Key);
                return minorVersion;
            },
                                   (majorVersion) => majorVersion.Versions[minorVersion].Max()));
        }
Ejemplo n.º 2
0
        private static void ValidateDependentPackages(INapackStorageManager napackManager, NewNapackVersion newNapack)
        {
            // Validate dependent packages exist, aren't recalled, and have valid licenses.
            foreach (NapackMajorVersion napackVersion in newNapack.Dependencies)
            {
                NapackMetadata             package = napackManager.GetPackageMetadata(napackVersion.Name, false);
                NapackMajorVersionMetadata majorVersionMetadata = package.GetMajorVersion(napackVersion.Major);
                if (majorVersionMetadata.Recalled)
                {
                    // Users creating a new napack cannot use recalled packages as they have no reasonable chance of retrieving them.
                    throw new NapackRecalledException(napackVersion.Name, napackVersion.Major);
                }

                newNapack.License.VerifyCompatibility(napackVersion.Name, napackVersion.Major, majorVersionMetadata.License);
            }
        }
Ejemplo n.º 3
0
        private Response GetPackage(INapackStorageManager napackManager, string name, int major,
                                    Func <NapackMajorVersionMetadata, int> minorVersionComputer, Func <NapackMajorVersionMetadata, int> patchVersionComputer)
        {
            NapackMetadata             package      = napackManager.GetPackageMetadata(name, false);
            NapackMajorVersionMetadata majorVersion = package.GetMajorVersion(major);

            if (majorVersion.Recalled)
            {
                throw new NapackRecalledException(name, major);
            }

            int minor = minorVersionComputer(majorVersion);
            int patch = patchVersionComputer(majorVersion);

            NapackVersion specifiedVersion = napackManager.GetPackageVersion(new NapackVersionIdentifier(name, major, minor, patch));

            Common.NapackVersion downloadableVersion = new Common.NapackVersion(major, minor, patch,
                                                                                specifiedVersion.Authors, specifiedVersion.Files, majorVersion.License, specifiedVersion.Dependencies);
            return(this.Response.AsJson(downloadableVersion, HttpStatusCode.OK));
        }
Ejemplo n.º 4
0
 private Response GetSpecificPackage(INapackStorageManager napackManager, NapackVersionIdentifier napackId)
 {
     return(this.GetPackage(napackManager, napackId.NapackName, napackId.Major, (unused) => napackId.Minor, (unused) => napackId.Patch));
 }
Ejemplo n.º 5
0
        public static void VerifyAuthorization(Dictionary <string, IEnumerable <string> > headers, INapackStorageManager storageManager, List <string> authorizedUserIds)
        {
            // Check that valid inputs were provided.
            if (!headers.ContainsKey(CommonHeaders.UserKeys) || !headers.ContainsKey(CommonHeaders.UserId))
            {
                throw new UnauthorizedUserException();
            }

            List <Guid> userSecrets;
            string      userId;

            try
            {
                userSecrets = Serializer.Deserialize <List <Guid> >(
                    Encoding.UTF8.GetString(Convert.FromBase64String(string.Join(string.Empty, headers[CommonHeaders.UserKeys]))));
                userId = string.Join(string.Empty, headers[CommonHeaders.UserId]);
            }
            catch (Exception)
            {
                throw new UnauthorizedUserException();
            }

            // Check that the user is authorized to perform this operation.
            if (!authorizedUserIds.Contains(userId, StringComparer.InvariantCulture))
            {
                throw new UnauthorizedUserException();
            }

            // Check that the user is who they say they are and that the user has performed validation.
            UserIdentifier user = storageManager.GetUser(userId);
            string         hash = UserIdentifier.ComputeUserHash(userSecrets);

            if ((Global.SystemConfig.RequireEmailValidation && !user.EmailConfirmed) || !user.Hash.Equals(hash, StringComparison.InvariantCulture))
            {
                throw new UnauthorizedUserException();
            }
        }