mitmproxy/netlib/http_auth.py

137 lines
3.8 KiB
Python
Raw Normal View History

2014-08-16 13:53:07 +00:00
from __future__ import (absolute_import, print_function, division)
from argparse import Action, ArgumentTypeError
2014-08-16 13:53:07 +00:00
from . import http
2013-03-02 21:37:28 +00:00
2015-04-09 00:09:33 +00:00
class NullProxyAuth(object):
2013-03-02 21:37:28 +00:00
"""
No proxy auth at all (returns empty challange headers)
"""
def __init__(self, password_manager):
self.password_manager = password_manager
def clean(self, headers):
"""
Clean up authentication headers, so they're not passed upstream.
"""
pass
def authenticate(self, headers):
"""
Tests that the user is allowed to use the proxy
"""
return True
def auth_challenge_headers(self):
"""
Returns a dictionary containing the headers require to challenge the user
"""
return {}
class BasicProxyAuth(NullProxyAuth):
CHALLENGE_HEADER = 'Proxy-Authenticate'
AUTH_HEADER = 'Proxy-Authorization'
2013-12-08 00:37:45 +00:00
2013-03-02 21:37:28 +00:00
def __init__(self, password_manager, realm):
NullProxyAuth.__init__(self, password_manager)
self.realm = realm
def clean(self, headers):
del headers[self.AUTH_HEADER]
def authenticate(self, headers):
auth_value = headers.get(self.AUTH_HEADER, [])
if not auth_value:
return False
parts = http.parse_http_basic_auth(auth_value[0])
if not parts:
return False
scheme, username, password = parts
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):
return {self.CHALLENGE_HEADER:'Basic realm="%s"'%self.realm}
2015-04-09 00:09:33 +00:00
class PassMan(object):
2013-03-02 21:37:28 +00:00
def test(self, username, password_token):
return False
2015-04-09 00:09:33 +00:00
class PassManNonAnon(PassMan):
2013-03-02 21:37:28 +00:00
"""
Ensure the user specifies a username, accept any password.
"""
def test(self, username, password_token):
if username:
return True
return False
2015-04-09 00:09:33 +00:00
class PassManHtpasswd(PassMan):
2013-03-02 21:37:28 +00:00
"""
Read usernames and passwords from an htpasswd file
"""
2014-08-16 13:28:09 +00:00
def __init__(self, path):
2013-03-02 21:37:28 +00:00
"""
Raises ValueError if htpasswd file is invalid.
"""
import passlib.apache
self.htpasswd = passlib.apache.HtpasswdFile(path)
2013-03-02 23:16:09 +00:00
2013-03-02 21:37:28 +00:00
def test(self, username, password_token):
2014-08-16 13:28:09 +00:00
return bool(self.htpasswd.check_password(username, password_token))
2013-03-02 21:37:28 +00:00
2015-04-09 00:09:33 +00:00
class PassManSingleUser(PassMan):
2013-03-02 21:37:28 +00:00
def __init__(self, username, password):
self.username, self.password = username, password
def test(self, username, password_token):
return self.username==username and self.password==password_token
class AuthAction(Action):
"""
Helper class to allow seamless integration int argparse. Example usage:
parser.add_argument(
"--nonanonymous",
action=NonanonymousAuthAction, nargs=0,
help="Allow access to any user long as a credentials are specified."
)
"""
def __call__(self, parser, namespace, values, option_string=None):
passman = self.getPasswordManager(values)
2013-12-08 00:37:45 +00:00
authenticator = BasicProxyAuth(passman, "mitmproxy")
setattr(namespace, self.dest, authenticator)
2013-12-08 00:35:42 +00:00
def getPasswordManager(self, s): # pragma: nocover
raise NotImplementedError()
class SingleuserAuthAction(AuthAction):
def getPasswordManager(self, s):
if len(s.split(':')) != 2:
2013-12-08 00:35:42 +00:00
raise ArgumentTypeError(
"Invalid single-user specification. Please use the format username:password"
)
username, password = s.split(':')
return PassManSingleUser(username, password)
class NonanonymousAuthAction(AuthAction):
def getPasswordManager(self, s):
return PassManNonAnon()
class HtpasswdAuthAction(AuthAction):
def getPasswordManager(self, s):
2014-08-16 13:28:09 +00:00
return PassManHtpasswd(s)
2013-12-08 00:35:42 +00:00