aboutsummaryrefslogtreecommitdiffstats
path: root/ddns/auth.py
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2014-03-17 04:16:49 +0100
committerMarius Halden <marius.h@lden.org>2014-03-17 04:16:49 +0100
commit7c575aaa8e98a6aa7eda8d69e2b14d014ee91b09 (patch)
tree7bd082fd216e1577440cf1ea599467993c2fef36 /ddns/auth.py
downloadDDNS.py-7c575aaa8e98a6aa7eda8d69e2b14d014ee91b09.tar.gz
DDNS.py-7c575aaa8e98a6aa7eda8d69e2b14d014ee91b09.tar.bz2
DDNS.py-7c575aaa8e98a6aa7eda8d69e2b14d014ee91b09.tar.xz
Initial commit
Diffstat (limited to 'ddns/auth.py')
-rw-r--r--ddns/auth.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/ddns/auth.py b/ddns/auth.py
new file mode 100644
index 0000000..6624aad
--- /dev/null
+++ b/ddns/auth.py
@@ -0,0 +1,27 @@
+## These functions are modified versions of these: http://flask.pocoo.org/snippets/8/
+from flask import request, Response
+from functools import wraps
+import ddns.cfg_parser
+import hash
+
+auth_cfg = ddns.cfg_parser.cfg['users']
+
+def check_auth(username, password):
+ for user in auth_cfg:
+ if username == user['username'] and \
+ hash.hash(user['hash'], password) == user['password']:
+ return True
+ return False
+
+def authenticate(message='badauth'):
+ return Response(message, 401,
+ {'WWW-Authenticate': 'Basic realm="login required"'})
+
+def require_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