gitHTTP.py
Python script, ASCII text executable
1import uuid 2 3from app import app, gitCommand, User, Repo, Commit, RepoAccess, getPermissionLevel, getVisibility, db, bcrypt 4import os 5import shutil 6import config 7import flask 8import git 9import subprocess 10from flask_httpauth import HTTPBasicAuth 11import zlib 12import re 13import datetime 14 15auth = HTTPBasicAuth(realm=config.AUTH_REALM) 16 17 18authRequired = flask.Response("Unauthorized Access", 401, {"WWW-Authenticate": 'Basic realm="Login Required"'}) 19 20 21@auth.verify_password 22def verifyPassword(username, password): 23user = User.query.filter_by(username=username).first() 24 25if user and bcrypt.check_password_hash(user.passwordHashed, password): 26flask.g.user = username 27return True 28 29return False 30 31 32@app.route("/git/<username>/<repository>/git-upload-pack", methods=["POST"]) 33@auth.login_required(optional=True) 34def gitUploadPack(username, repository): 35if auth.current_user() is None and not getVisibility(username, repository): 36return authRequired 37if not (getVisibility(username, repository) or getPermissionLevel(flask.g.user, username, repository) is not None): 38flask.abort(403) 39 40serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 41text = gitCommand(serverRepoLocation, flask.request.data, "upload-pack", "--stateless-rpc", ".") 42 43return flask.Response(text, content_type="application/x-git-upload-pack-result") 44 45 46@app.route("/git/<username>/<repository>/git-receive-pack", methods=["POST"]) 47@auth.login_required 48def gitReceivePack(username, repository): 49if not getPermissionLevel(flask.g.user, username, repository): 50flask.abort(403) 51 52serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 53text = gitCommand(serverRepoLocation, flask.request.data, "receive-pack", "--stateless-rpc", ".") 54 55sha = flask.request.data.split(b" ", 2)[1].decode() 56info = gitCommand(serverRepoLocation, None, "show", "-s", "--format='%H%n%at%n%cn <%ce>%n%B'", sha).decode() 57 58if re.match("^[0-9a-fA-F]{40}$", info[:40]): 59print(info.split("\n", 3)) 60sha, time, identity, body = info.split("\n", 3) 61login = flask.g.user 62 63user = User.query.filter_by(username=login).first() 64repo = Repo.query.filter_by(route=f"/{username}/{repository}").first() 65commit = Commit(sha, user, repo, time, body, identity) 66 67db.session.add(commit) 68db.session.commit() 69 70return flask.Response(text, content_type="application/x-git-receive-pack-result") 71 72 73@app.route("/git/<username>/<repository>/info/refs", methods=["GET"]) 74@auth.login_required(optional=True) 75def gitInfoRefs(username, repository): 76serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 77 78repo = git.Repo(serverRepoLocation) 79repoData = Repo.query.filter_by(route=f"/{username}/{repository}").first() 80if not repoData.defaultBranch: 81if repo.heads: 82repoData.defaultBranch = repo.heads[0].name 83repo.git.checkout("-f", repoData.defaultBranch) 84 85if auth.current_user() is None and (not getVisibility(username, repository) or flask.request.args.get("service") == "git-receive-pack"): 86return authRequired 87try: 88if not (getVisibility(username, repository) or getPermissionLevel(flask.g.user, username, repository) is not None): 89flask.abort(403) 90except AttributeError: 91return authRequired 92 93service = flask.request.args.get("service") 94 95if service.startswith("git"): 96service = service[4:] 97else: 98flask.abort(403) 99 100if service == "receive-pack": 101try: 102if not getPermissionLevel(flask.g.user, username, repository): 103flask.abort(403) 104except AttributeError: 105return authRequired 106 107serviceLine = f"# service=git-{service}\n" 108serviceLine = (f"{len(serviceLine) + 4:04x}" + serviceLine).encode() 109 110if service == "upload-pack": 111text = serviceLine + b"0000" + gitCommand(serverRepoLocation, None, "upload-pack", "--stateless-rpc", "--advertise-refs", "--http-backend-info-refs", ".") 112elif service == "receive-pack": 113refs = gitCommand(serverRepoLocation, None, "receive-pack", "--http-backend-info-refs", ".") 114text = serviceLine + b"0000" + refs 115else: 116flask.abort(403) 117 118response = flask.Response(text, content_type=f"application/x-git-{service}-advertisement") 119response.headers["Cache-Control"] = "no-cache" 120 121return response 122 123