## 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 def check_auth(username, password): for user in ddns.cfg_parser.cfg.get('users'): 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