diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2013-11-21 13:09:11 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2013-11-21 13:09:11 +1300 |
commit | bed2aed9db346f37b858c8f7f069f65e9f59d0e9 (patch) | |
tree | 6e14e2c818ce3480e744b9ccda445c024d9fe694 /netlib/http_auth.py | |
parent | 07e970346f216ddafbd3a43b411fcb4e14426ada (diff) | |
parent | e402e3b862312ca4f7bd7dd633db3654143c3380 (diff) | |
download | mitmproxy-bed2aed9db346f37b858c8f7f069f65e9f59d0e9.tar.gz mitmproxy-bed2aed9db346f37b858c8f7f069f65e9f59d0e9.tar.bz2 mitmproxy-bed2aed9db346f37b858c8f7f069f65e9f59d0e9.zip |
Merge branch 'master' of ssh.github.com:cortesi/netlib
Diffstat (limited to 'netlib/http_auth.py')
-rw-r--r-- | netlib/http_auth.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/netlib/http_auth.py b/netlib/http_auth.py index 4adae179..6c91c7c5 100644 --- a/netlib/http_auth.py +++ b/netlib/http_auth.py @@ -1,6 +1,7 @@ import binascii import contrib.md5crypt as md5crypt import http +from argparse import Action, ArgumentTypeError class NullProxyAuth(): @@ -111,3 +112,46 @@ class PassManSingleUser: 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) + if passman: + authenticator = BasicProxyAuth(passman, "mitmproxy") + else: + authenticator = NullProxyAuth(None) + setattr(namespace, "authenticator", authenticator) + + def getPasswordManager(self, s): + """ + returns the password manager + """ + raise NotImplementedError() + + +class SingleuserAuthAction(AuthAction): + def getPasswordManager(self, s): + if len(s.split(':')) != 2: + 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): + with open(s, "r") as f: + return PassManHtpasswd(f)
\ No newline at end of file |