private static void RegisterWinDbg(RouteCollection routes, string type)
        {
            routes.MapRoute(
                type + "-404",
                type.ToLower() + "/{company}/{login}/{password}/index2.txt",
                new { controller = "Default", action = "Index404" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            routes.MapRoute(
                type + "-Public-404",
                type.ToLower() + "/{company}/index2.txt",
                new { controller = "Default", action = "Index404" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            routes.MapRoute(
                type,
                type.ToLower() + "/{company}/{login}/{password}/{name}/{hash}/{name1}",
                new { controller = type, action = "Index" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            var configuration = new AppSettingsConfiguration("Public");

            routes.MapRoute(
                type + "-Public",
                type.ToLower() + "/{company}/{name}/{hash}/{name1}",
                new { controller = type, action = "Index", login = configuration.PublicLogin, password = configuration.PublicPassword },
                new[] { typeof(AttributeRouting).Namespace }
                );
        }
        private Caller Authenticate(string company, bool require)
        {
            var auth = Request.Headers["Authorization"];

            if (auth != null)
            {
                var caller = factory.DigestValidateResponse(company, Request.HttpMethod, auth);

                if (caller != null)
                    return caller;
            }

            if (!require)
            {
                var configuration = new AppSettingsConfiguration(company);

                return new Caller
                           {
                               Company = company,
                               Name = configuration.PublicLogin,
                               KeyType = "API",
                               KeyValue = configuration.PublicPassword
                           };
            }

            Response.StatusCode = 401;
            Response.AddHeader("WWW-Authenticate", factory.DigestGenerateRequest(company));
            return null;
        }
        public ActionResult Index(string company, string login, string password, string name, string hash, string name1)
        {
            if ("Public".Equals(company, StringComparison.OrdinalIgnoreCase))
                company = "Public";

            if (string.IsNullOrEmpty(login) && string.IsNullOrEmpty(password))
            {
                var configuration = configurationFactory.Create(company);
                login = configuration.PublicLogin;
                password = configuration.PublicPassword;
            }

            if (!name1.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) && !name1.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
            {
                Response.StatusCode = 404;
                return Content("Supported only not compresioned files (.dll/.exe)");
            }

            //Ktoś ma małą literką numer kompilacji
            hash = hash.ToUpper();

            string imageName = Path.GetFileNameWithoutExtension(name);

            using (var backend = factory.Create(company, login, "VisualStudio", password))
            {
                var imageFile = backend.GetImageFile(imageName, hash);

                if (imageFile != null)
                    return HandleFound(backend, imageFile);
            }

            if (company != "Public")
            {
                var configuration = new AppSettingsConfiguration("Public");

                using (var backend = factory.Create("Public", configuration.PublicLogin, "VisualStudio", configuration.PublicPassword))
                {
                    var imageFile = backend.GetImageFile(imageName, hash);

                    if (imageFile != null)
                        return HandleFound(backend, imageFile);
                }
            }

            using (var backend = factory.Create(company, login, "VisualStudio", password))
                return HandleNotFound(backend, name, hash, imageName);
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapAttributeRoutes();

            RegisterWinDbg(routes, "Pdb");
            RegisterWinDbg(routes, "Bin");

            routes.MapRoute(
                "Source",
                "pdbsrc/{company}/{login}/{password}/{computerName}/{computerUser}/{imageName}/{pdbHash}/{*sourcePath}",
                new { controller = "Source", action = "Index" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            routes.MapRoute(
                "Default-404",
                "{company}/{login}/{password}/index2.txt",
                new { controller = "Default", action = "Index404" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            routes.MapRoute(
                "H-Public-404",
                "{company}/index2.txt",
                new { controller = "Default", action = "Index404" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            routes.MapRoute(
                "Default-Default",
                "{company}/{login}/{password}/{name}/{hash}/{name1}",
                new { controller = "Default", action = "Index" },
                new[] { typeof(AttributeRouting).Namespace }
                );

            var configuration = new AppSettingsConfiguration("Public");

            routes.MapRoute(
                "Default-Public",
                "{company}/{name}/{hash}/{name1}",
                new { controller = "Default", action = "Index", login = configuration.PublicLogin, password = configuration.PublicPassword },
                new[] { typeof(AttributeRouting).Namespace }
                );
        }
        private Caller GetCaller(string company, string key)
        {
            var configuration = new AppSettingsConfiguration(company);

            try
            {
                var caller = factory.GetUserByKey(company, "NuGet", key);

                if (caller != null)
                    return caller;

                if (string.IsNullOrEmpty(configuration.GatewayLogin) || string.IsNullOrEmpty(configuration.GatewayPassword))
                    throw new Exception("Missing gateway configuration");

                using (var backend = factory.Create(company, configuration.GatewayLogin, "API", configuration.GatewayPassword))
                    return backend.CreateUserByKey(company, "NuGet", key);
            }
            catch (Exception exception)
            {
                throw new ClientException("User authentication failure", exception);
            }
        }
        private Caller Authenticate(string company, string login, string key, bool require)
        {
            if (!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(key))
            {
                var caller = new Caller { Company = company, Name = login, KeyType = "VisualStudio", KeyValue = key };

                if (factory.Validate(caller) != null)
                    return caller;

                Response.StatusCode = 403;
                return null;
            }

            var auth = Request.Headers["Authorization"];

            if (auth != null)
            {
                var token = Encoding.ASCII.GetString(Convert.FromBase64String(auth.Split(' ')[1])).Split(':');
                var caller = new Caller { Company = company, Name = token[0], KeyType = "Password", KeyValue = token[1] };

                if (factory.Validate(caller) != null)
                    return caller;
            }

            if (!require)
            {
                var configuration = new AppSettingsConfiguration(company);

                return new Caller
                {
                    Company = company,
                    Name = configuration.PublicLogin,
                    KeyType = "API",
                    KeyValue = configuration.PublicPassword
                };
            }

            Response.StatusCode = 401;
            Response.AddHeader("WWW-Authenticate", string.Format("Basic realm=\"{0}\"", company));
            return null;
        }