Skip to content

Instantly share code, notes, and snippets.

@pmrowla
Created February 21, 2020 11:48
Show Gist options
  • Save pmrowla/0615f162d1308cab4f429b6efafe276a to your computer and use it in GitHub Desktop.
Save pmrowla/0615f162d1308cab4f429b6efafe276a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Requires:
# pip install werkzeug<1.0 flask flask-httpauth flask-session
from pathlib import Path
from flask import abort, Flask, request, send_file
from flask_session import Session
from flask_httpauth import HTTPBasicAuth, HTTPDigestAuth
app = Flask(__name__)
app.config['SECRET_KEY'] = 'foo'
app.config['SESSION_TYPE'] = 'filesystem'
basic_auth = HTTPBasicAuth()
digest_auth = HTTPDigestAuth()
sess = Session()
sess.init_app(app)
users = {"dvc": "password1"}
def _get_password(username):
if username in users:
return users.get(username)
return False
@basic_auth.get_password
def get_basic_password(username):
return _get_password(username)
@digest_auth.get_password
def get_digest_password(username):
return _get_password(username)
def _test_dvc_http(path):
path = Path(path)
if request.method in ["HEAD", "GET"]:
if path.exists():
return send_file(path)
else:
abort(404)
elif request.method == "POST":
try:
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "wb") as fd:
fd.write(request.data)
return str(path)
except Exception:
abort(403)
abort(405)
@app.route("/public/<path:path>", methods=["GET", "POST"])
def test_public(path):
return _test_dvc_http(path)
@app.route("/basic/<path:path>", methods=["GET", "POST"])
@basic_auth.login_required
def test_basic(path):
return _test_dvc_http(path)
@app.route("/digest/<path:path>", methods=["GET", "POST"])
@digest_auth.login_required
def test_digest(path):
return _test_dvc_http(path)
if __name__ == '__main__':
app.run()
@pmrowla
Copy link
Author

pmrowla commented Feb 21, 2020

note: flask-session requires older werkzeug version for filesystem session storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment