blob: 030d23980ef0551eb7afc7ace6abb9317840e68b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
## 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
|