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): 76if auth.current_user() is None and (not getVisibility(username, repository) or flask.request.args.get("service") == "git-receive-pack"): 77return authRequired 78try: 79if not (getVisibility(username, repository) or getPermissionLevel(flask.g.user, username, repository) is not None): 80flask.abort(403) 81except AttributeError: 82return authRequired 83 84serverRepoLocation = os.path.join(config.REPOS_PATH, username, repository, ".git") 85service = flask.request.args.get("service") 86 87if service.startswith("git"): 88service = service[4:] 89else: 90flask.abort(403) 91 92if service == "receive-pack": 93print(getPermissionLevel(flask.g.user, username, repository)) 94if not getPermissionLevel(flask.g.user, username, repository): 95flask.abort(403) 96 97serviceLine = f"# service=git-{service}\n" 98serviceLine = (f"{len(serviceLine) + 4:04x}" + serviceLine).encode() 99 100if service == "upload-pack": 101text = serviceLine + b"0000" + gitCommand(serverRepoLocation, None, "upload-pack", "--stateless-rpc", "--advertise-refs", "--http-backend-info-refs", ".") 102elif service == "receive-pack": 103text = serviceLine + b"0000" + gitCommand(serverRepoLocation, None, "receive-pack", "--http-backend-info-refs", ".") 104else: 105flask.abort(403) 106 107response = flask.Response(text, content_type=f"application/x-git-{service}-advertisement") 108response.headers["Cache-Control"] = "no-cache" 109 110return response 111 112