aboutsummaryrefslogtreecommitdiff
path: root/backend/LocalBasicAuth.py
blob: 967707cae546e4b6ac2595dd5a3fd423ce88c557 (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
28
29
30
31
32
33
34
35
import config
import utils
import base64
from passlib.apache import HtpasswdFile

# This authentication provider reads a local Apache-style htpassword file
# and performs HTTP Basic authentication.

passwd_file = ".htpasswd"
realm = "cargohold"

def login(env, post):
	auth = get(env)

	if not auth:
		return ["Please authenticate", [("WWW-Authenticate",'Basic realm="' + realm + '"')], "401 Authenticate"]

	utils.ensure_user(auth["user"])
	return utils.redirect("/admin")

def get(env):
	auth = env.get("HTTP_AUTHORIZATION", "")
	if auth and auth.startswith("Basic "):
		auth = str(base64.b64decode(auth[6:]), "utf-8").split(":")
		try:
			ht = HtpasswdFile(passwd_file)
			if ht.check_password(auth[0], auth[1]):
				return {"user": auth[0], "expire": None}
		except IOError:
			print("LocalBasicAuth: Failed to read credentials file at " + passwd_file)
	return None

def logout():
	# TODO
	return False