2012-12-30 09:24:30 +00:00
|
|
|
import binascii
|
|
|
|
import contrib.md5crypt as md5crypt
|
|
|
|
|
|
|
|
class NullProxyAuth():
|
2012-12-30 20:15:56 +00:00
|
|
|
"""
|
|
|
|
No proxy auth at all (returns empty challange headers)
|
|
|
|
"""
|
|
|
|
def __init__(self, password_manager):
|
2012-12-30 09:24:30 +00:00
|
|
|
self.password_manager = password_manager
|
|
|
|
self.username = ""
|
|
|
|
|
2012-12-30 21:56:44 +00:00
|
|
|
def clean(self, headers):
|
|
|
|
"""
|
|
|
|
Clean up authentication headers, so they're not passed upstream.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
def authenticate(self, headers):
|
|
|
|
"""
|
2012-12-30 21:56:44 +00:00
|
|
|
Tests that the user is allowed to use the proxy
|
2012-12-30 20:15:56 +00:00
|
|
|
"""
|
2012-12-30 09:24:30 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def auth_challenge_headers(self):
|
2012-12-30 20:15:56 +00:00
|
|
|
"""
|
|
|
|
Returns a dictionary containing the headers require to challenge the user
|
|
|
|
"""
|
2012-12-30 09:24:30 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class BasicProxyAuth(NullProxyAuth):
|
2012-12-30 21:56:44 +00:00
|
|
|
CHALLENGE_HEADER = 'Proxy-Authenticate'
|
|
|
|
AUTH_HEADER = 'Proxy-Authorization'
|
|
|
|
def __init__(self, password_manager, realm):
|
2012-12-30 09:24:30 +00:00
|
|
|
NullProxyAuth.__init__(self, password_manager)
|
2012-12-30 21:56:44 +00:00
|
|
|
self.realm = realm
|
|
|
|
|
|
|
|
def clean(self, headers):
|
|
|
|
del headers[self.AUTH_HEADER]
|
2012-12-30 09:24:30 +00:00
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
def authenticate(self, headers):
|
2012-12-30 21:56:44 +00:00
|
|
|
auth_value = headers.get(self.AUTH_HEADER, [])
|
2012-12-30 20:15:56 +00:00
|
|
|
if not auth_value:
|
|
|
|
return False
|
2012-12-30 09:24:30 +00:00
|
|
|
try:
|
2012-12-30 21:34:25 +00:00
|
|
|
scheme, username, password = self.parse_auth_value(auth_value[0])
|
|
|
|
except ValueError:
|
2012-12-30 09:24:30 +00:00
|
|
|
return False
|
|
|
|
if scheme.lower()!='basic':
|
|
|
|
return False
|
|
|
|
if not self.password_manager.test(username, password):
|
|
|
|
return False
|
|
|
|
self.username = username
|
|
|
|
return True
|
|
|
|
|
|
|
|
def auth_challenge_headers(self):
|
2012-12-30 21:56:44 +00:00
|
|
|
return {self.CHALLENGE_HEADER:'Basic realm="%s"'%self.realm}
|
2012-12-30 09:24:30 +00:00
|
|
|
|
2012-12-30 21:34:25 +00:00
|
|
|
def unparse_auth_value(self, scheme, username, password):
|
|
|
|
v = binascii.b2a_base64(username + ":" + password)
|
|
|
|
return scheme + " " + v
|
|
|
|
|
|
|
|
def parse_auth_value(self, auth_value):
|
2012-12-30 09:24:30 +00:00
|
|
|
words = auth_value.split()
|
2012-12-30 21:34:25 +00:00
|
|
|
if len(words) != 2:
|
|
|
|
raise ValueError("Invalid basic auth credential.")
|
2012-12-30 09:24:30 +00:00
|
|
|
scheme = words[0]
|
2012-12-30 21:34:25 +00:00
|
|
|
try:
|
|
|
|
user = binascii.a2b_base64(words[1])
|
|
|
|
except binascii.Error:
|
|
|
|
raise ValueError("Invalid basic auth credential: user:password pair not valid base64: %s"%words[1])
|
|
|
|
parts = user.split(':')
|
|
|
|
if len(parts) != 2:
|
|
|
|
raise ValueError("Invalid basic auth credential: decoded user:password pair not valid: %s"%user)
|
|
|
|
return scheme, parts[0], parts[1]
|
2012-12-30 09:24:30 +00:00
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
|
2012-12-30 09:24:30 +00:00
|
|
|
class PasswordManager():
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test(self, username, password_token):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
class PermissivePasswordManager(PasswordManager):
|
2012-12-30 09:24:30 +00:00
|
|
|
def __init__(self):
|
|
|
|
PasswordManager.__init__(self)
|
|
|
|
|
|
|
|
def test(self, username, password_token):
|
|
|
|
if username:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
class HtpasswdPasswordManager(PasswordManager):
|
|
|
|
"""
|
|
|
|
Read usernames and passwords from a file created by Apache htpasswd
|
|
|
|
"""
|
2012-12-30 09:24:30 +00:00
|
|
|
def __init__(self, filehandle):
|
|
|
|
PasswordManager.__init__(self)
|
|
|
|
entries = (line.strip().split(':') for line in filehandle)
|
|
|
|
valid_entries = (entry for entry in entries if len(entry)==2)
|
|
|
|
self.usernames = {username:token for username,token in valid_entries}
|
|
|
|
|
|
|
|
def test(self, username, password_token):
|
|
|
|
if username not in self.usernames:
|
|
|
|
return False
|
|
|
|
full_token = self.usernames[username]
|
|
|
|
dummy, magic, salt, hashed_password = full_token.split('$')
|
|
|
|
expected = md5crypt.md5crypt(password_token, salt, '$'+magic+'$')
|
|
|
|
return expected==full_token
|
|
|
|
|
|
|
|
|
2012-12-30 20:15:56 +00:00
|
|
|
class SingleUserPasswordManager(PasswordManager):
|
2012-12-30 09:24:30 +00:00
|
|
|
def __init__(self, username, password):
|
|
|
|
PasswordManager.__init__(self)
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
|
|
|
|
def test(self, username, password_token):
|
|
|
|
return self.username==username and self.password==password_token
|