Question

I tried to make an authorization in my asp with my git client, so my git client will be requested an authorization from my server. When i tried to send a request to my git client, it was showing an error

repository http://localhost/git/user/try.git/info/ref not found

Here is my routeconfig

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        #region remoteURL
        routes.MapRoute(
            name: "RemoteURL",
            url: "git/{project}.git/{*verb}",
            defaults: new { controller = "Git", action = "Smart" }
            );

        routes.MapRoute(
            name: "Git",
            url: "git/{project}/{*verb}",
            defaults: new { controller = "Git", action = "Smart" }
        );
        #endregion
        #region Account;
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        #endregion;
    }

and this is my controller that use an attribute :

public class GitController : Controller
{
    [SmartGit]
    public ActionResult Smart(string project, string service, string verb)
    {
        switch (verb)
        {
            case "info/refs":
                return InfoRefs(project, service);
            case "git-upload-pack":
                return ExecutePack(project, "git-upload-pack");
            case "git-receive-pack":
                return ExecutePack(project, "git-receive-pack");
            default:
                return RedirectToAction("Tree", "Repository", new { Name = project });
        }
    }

and then this is my attribute smartgit

public class SmartGitAttribute : SmartAuthorizeAttribute
{
    private const string AuthKey = "GitCodeGitAuthorize";
    private GitCodeContext db = new GitCodeContext();
    private string project;
    private string verb;
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
        var right = false;
        var userfound = false;
        List<string> paramParsing = new List<string>();


        //url.Split("")
        //base.OnAuthorization(filterContext);
        var controller = filterContext.Controller as GitController;
        if (controller == null)
            return;

        // git.exe not accept cookies as well as no session available
        var auth = controller.HttpContext.Request.Headers["Authorization"];

        if (!String.IsNullOrEmpty(auth))
        {
            var bytes = Convert.FromBase64String(auth.Substring(6));
            var certificate = Encoding.ASCII.GetString(bytes);
            var index = certificate.IndexOf(':');
            var password = certificate.Substring(index + 1);
            var username = certificate.Substring(0, index);

            //var user = controller.MembershipService.Login(username, password);
            if (WebSecurity.Login(username, password))
            {
                WebSecurity.Login(username, password);
                userfound = true;
            }
        }

        var projectField = controller.ValueProvider.GetValue("project");
        var serviceField = controller.ValueProvider.GetValue("service");
        var verbField = controller.ValueProvider.GetValue("service");
        //filterContext.Controller.ValueProvider
        var project = projectField == null ? null : projectField.AttemptedValue;
        var service = serviceField == null ? null : serviceField.AttemptedValue;
        var verb = verbField == null ? null : serviceField.AttemptedValue;

        if (string.IsNullOrEmpty(service) && userfound) // redirect to git browser
        {
            right = true;
        }
        else if (string.Equals(service, "git-receive-pack", StringComparison.OrdinalIgnoreCase) && userfound) // git push
        {
            //right = controller.RepositoryService.CanWriteRepository(project, username);
            right = true;
        }
        else if (string.Equals(service, "git-upload-pack", StringComparison.OrdinalIgnoreCase) && userfound ) // git fetch
        {
            //right = controller.RepositoryService.CanReadRepository(project, username);
            right = true;
        }

        if (!userfound)
        {
            if (WebSecurity.CurrentUserName == "")
            {
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.AddHeader("WWW-Authenticate", "Basic realm=\"coba\"");
                filterContext.Result = new HttpUnauthorizedResult();
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
    }
Was it helpful?

Solution

I found my own mistake, maybe my response doesn't have enough information so i decide to add a few information in my SmartGitAttribute

filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
filterContext.HttpContext.Response.AddHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"");
filterContext.HttpContext.Response.Write("401, please authenticate");
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.Result = new EmptyResult();
filterContext.HttpContext.Response.End();

this is reference that can help you to solve response authentication

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top