diff options
Diffstat (limited to 'ddns/auth.py')
-rw-r--r-- | ddns/auth.py | 27 |
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 |