aboutsummaryrefslogtreecommitdiffstats
path: root/ddns/auth.py
blob: 6624aade794a3459b61cc31d31e36c6f7f78869d (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
26
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