Ejemplo n.º 1
0
        public bool Authorize(FeedPrincipal principal, IEnumerable <PackageName> packageName)
        {
            if (!configuration.AllowNamedFeeds && !string.IsNullOrEmpty(principal.FeedName))
            {
                return(false);
            }

            if (!principal.Identity.IsAuthenticated)
            {
                return(true);
            }

            var apiKeyIdentity = (NuGetApiKeyIdentity)principal.Identity;

            return(configuration.PushApiKeys.Contains(apiKeyIdentity.ApiKey, StringComparer.InvariantCultureIgnoreCase));
        }
Ejemplo n.º 2
0
        public bool Authorize(FeedPrincipal principal, IEnumerable <PackageName> packageNames)
        {
            if (!string.IsNullOrEmpty(principal.FeedName))
            {
                return(false);
            }

            if (packageNames == null)
            {
                packageNames = new PackageName[0];
            }

            switch (principal.AuthenticatedArea)
            {
            case AuthenticatedArea.Pushing:
            case AuthenticatedArea.Deleting:
            case AuthenticatedArea.Retrying:
            case AuthenticatedArea.RetryingAll:
            case AuthenticatedArea.RetryingOwn:
            {
                var apiKeyIdentity = (NuGetApiKeyIdentity)principal.Identity;

                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var packageName in packageNames)
                {
                    if (VerifyKey(apiKeyIdentity.ApiKey, packageName.Id))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            case AuthenticatedArea.Querying:
            case AuthenticatedArea.QueryingAll:
            case AuthenticatedArea.QueryingOwn:
            case AuthenticatedArea.Debugging:
                return(true);

            default:
                throw new ArgumentOutOfRangeException("area", principal.AuthenticatedArea, null);
            }
        }
Ejemplo n.º 3
0
        public bool Authorize(FeedPrincipal principal, IEnumerable <PackageName> packageNames)
        {
            if (string.IsNullOrEmpty(principal.FeedName))
            {
                return(false);
            }

            if (packageNames == null)
            {
                packageNames = new PackageName[0];
            }

            var apiKeyIdentity = principal.Identity as NuGetApiKeyIdentity;
            var basicIdentity  = null as BasicAuthenticationData;

            foreach (var packageName in packageNames)
            {
                MyGetOrgResponse result;

                if (apiKeyIdentity != null)
                {
                    result = endpoint.VerifyAccess(configuration.Host, configuration.Secret, principal.FeedName, packageName, apiKeyIdentity.ApiKey, null);
                }
                else if (basicIdentity != null)
                {
                    var credential = new NetworkCredential(basicIdentity.Username, basicIdentity.Password);
                    result = endpoint.VerifyAccess(configuration.Host, configuration.Secret, principal.FeedName, packageName, null, credential);
                }
                else
                {
                    result = endpoint.VerifyAccess(configuration.Host, configuration.Secret, principal.FeedName, packageName, null, null);
                }

                switch (principal.AuthenticatedArea)
                {
                case AuthenticatedArea.Pushing:
                case AuthenticatedArea.Deleting:
                {
                    if (result.write)
                    {
                        return(true);
                    }
                    break;
                }

                case AuthenticatedArea.Querying:
                case AuthenticatedArea.Debugging:
                {
                    if (result.read)
                    {
                        return(true);
                    }
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("area", principal.AuthenticatedArea, null);
                }
            }

            return(false);
        }