diff --git a/README.md b/README.md new file mode 100644 index 0000000..4705e7b --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# OAuth2 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..717cac2 --- /dev/null +++ b/main.py @@ -0,0 +1,132 @@ +import flask +import json +from functools import wraps +from flask import request, Response + + +app = flask.Flask(__name__) + + +def dump_json(message): + try: + return json.dumps(json.loads(message.decode('utf-8')), indent=2) + except Exception: + return message.decode('utf-8') + + +def check_auth(username, password): + """This function is called to check if a username / + password combination is valid. + """ + return username == 'admin' and password == '1234' + + +def authenticate(): + """Sends a 401 response that enables basic auth""" + return Response( + 'Could not verify your access level for that URL.\n' + 'You have to login with proper credentials', 401, + {'WWW-Authenticate': 'Basic realm="Login Required"'}) + + +def requires_auth(f): + @wraps(f) + def decorated(*args, **kwargs): + auth = request.authorization + if not auth or not check_auth(auth.username, auth.password): + return authenticate() + return f(*args, **kwargs) + + return decorated + + +def resp(code, data): + return flask.Response( + status=code, + mimetype="application/json", + response=json.dumps(data) + ) + + +# e.g. failed to parse json +@app.errorhandler(400) +def page_not_found(e): + return resp(400, {}) + + +@app.errorhandler(404) +def page_not_found(e): + return resp(400, {}) + + +@app.errorhandler(401) +def not_auth(e): + return resp(401, {}) + +data = [] + + +def to_data(url, message): + from datetime import datetime + return dict( + datetime=str(datetime.now()), + url=url, + message=message + ) + + +def data_to_html(): + from functools import reduce + return reduce( + lambda a, b: a + b, + map( + lambda param: '
{datetime} {url}
{message}
{datetime} {url}
{message}