From b8bff45becc5366acb782a14cd32e49fea9ddf3e Mon Sep 17 00:00:00 2001 From: Davide Bongiovanni Date: Tue, 1 Nov 2016 19:06:21 +0100 Subject: [PATCH] Added HTTP Basic Auth --- ...project => ELAB-RFID-locks.sublime-project | 0 ELAB-RFID-locks.sublime-workspace | 1 + server.py | 23 +++++++++++++++++- server.pyc | Bin 2960 -> 4102 bytes 4 files changed, 23 insertions(+), 1 deletion(-) rename fridge_lock.sublime-project => ELAB-RFID-locks.sublime-project (100%) create mode 100644 ELAB-RFID-locks.sublime-workspace diff --git a/fridge_lock.sublime-project b/ELAB-RFID-locks.sublime-project similarity index 100% rename from fridge_lock.sublime-project rename to ELAB-RFID-locks.sublime-project diff --git a/ELAB-RFID-locks.sublime-workspace b/ELAB-RFID-locks.sublime-workspace new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/ELAB-RFID-locks.sublime-workspace @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/server.py b/server.py index 923dc7a..a3d167e 100644 --- a/server.py +++ b/server.py @@ -1,13 +1,30 @@ +from functools import wraps 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 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('/') def index(): return '{"onFire": false}' @app.route('/status') +@requires_auth def somehtml(): history = [] with open('history.json', 'r') as json_data: @@ -18,19 +35,23 @@ def somehtml(): return render_template('status.html', events = history[::-1]) @app.route('/users') +@requires_auth def users(): return 'You have reached Users' @app.route('/history') +@requires_auth def history(): return 'You have reached history' @app.route('/getcardinfo/') +@requires_auth def getCardInfo(cardID): return send_from_directory('cards', cardID + '.json') @app.route('/addcard///') +@requires_auth def addCard(cardID, name, authCode): authCode = int(authCode) data = {} diff --git a/server.pyc b/server.pyc index 2dd1b3d05016327210245979a65e4cfbf810d5df..c74721d7638f005bf7bba2771723b1e65a44773f 100644 GIT binary patch literal 4102 zcmbVP+io1k5v|$T3zs)hltfCh5)W2^y?{Wi6Ci#NHcV_P1RNN2Jaj;LfnYtF=_QAp z%W8U-6l3y9agwJPL4G1%l7GlUJ|f5mB^=&4y+je4}V=)QpyIpvmzMV`E)l4>`AxI-5cl%lSQT&mL?r1L21fcShH9gj*2`T zMsZ=nGB3`{meRhM7+aPL82iSK^URvPu!b2Mow$Q$H_%(s)d4V^vrpSWSY>fkJ&+;Pj!aRK*XF(a~nHO8{G)Us8Y~olHoW_wU$!2^O zb@3xz-mN0d_~>NDtD+&H{QzC>C{InVV$=Ke=bwDKbSudrO%HOMn&R>Jyj%on zND+s@q&%YTme)uvWY0fJvx8K%t(a zYg>-jL?+A&xFTY3S>7(Sw(}#WKd=qwd?qbN*5I*tU`4a3zoWw z^Io$g&z@RdSJ3UC={~$P^^Bzq-*)nB7r^Q~@kT*n&1EGNkIhHdk&GaWky>6dKBmEA z8Av@_2Ia)6R4(Q;kxF%r!J)|Im2iUpzuyqx2$~!N8V*w+2o55VFP|~Txbi;cFFQoW z)8F4Gl#m^O9>m~15LcuAi)}f8+mZBRWHY&phxT1oMt5MXO+C_1Ly|>DC_lIPg z9-hlIA6-K_{{eVELYoQ3YTG+BWf&AuoQ?8cpQF#7Zfl~X_2_i9AcL$b6NFz)Hi7M` z9`31gdC*$AS%Ft23m4NWA<;P%?{|`1F$Y0+C>{j;2<&D`JjA#cL=oA)R-o5sMc(f9 znNoN1$ZUrchM;6qm8c|CLWnUcQT!0Rh&==yN}wZs#-7&w;+G6yuDa5nj_T6SQVq`r zp9aNq&HZnX{6AywIBC93Ve@`Hab^!E_SgmAInRNJdSiMiyzR+Kp*4dih8$8c0D8<+3Fg zEs0j;9?EX+iJ6nJ;mWGVux$bOfFVo&o3eF7`P?8L*U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