Added HTTP Basic Auth

master
Davide Bongiovanni 8 years ago
parent e4d5c9c1fa
commit b8bff45bec

@ -1,13 +1,30 @@
from functools import wraps
from flask import Flask from flask import Flask
from flask import render_template, send_from_directory from flask import render_template, send_from_directory, request, Response
import json, time import json, time
app = Flask(__name__) app = Flask(__name__)
def check_auth(username, password):
return username == 'davide' and password == 'pwd'
def authenticate():
return Response('Could not verify access level. Please retry', 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
@app.route('/') @app.route('/')
def index(): def index():
return '{"onFire": false}' return '{"onFire": false}'
@app.route('/status') @app.route('/status')
@requires_auth
def somehtml(): def somehtml():
history = [] history = []
with open('history.json', 'r') as json_data: with open('history.json', 'r') as json_data:
@ -18,19 +35,23 @@ def somehtml():
return render_template('status.html', events = history[::-1]) return render_template('status.html', events = history[::-1])
@app.route('/users') @app.route('/users')
@requires_auth
def users(): def users():
return 'You have reached Users' return 'You have reached Users'
@app.route('/history') @app.route('/history')
@requires_auth
def history(): def history():
return 'You have reached history' return 'You have reached history'
@app.route('/getcardinfo/<cardID>') @app.route('/getcardinfo/<cardID>')
@requires_auth
def getCardInfo(cardID): def getCardInfo(cardID):
return send_from_directory('cards', cardID + '.json') return send_from_directory('cards', cardID + '.json')
@app.route('/addcard/<cardID>/<name>/<authCode>') @app.route('/addcard/<cardID>/<name>/<authCode>')
@requires_auth
def addCard(cardID, name, authCode): def addCard(cardID, name, authCode):
authCode = int(authCode) authCode = int(authCode)
data = {} data = {}

Binary file not shown.
Loading…
Cancel
Save