def prepare_auth(auth, username, password): if username and password: if auth == 'basic' or auth is None: return (username, password) elif auth == 'digest': from requests.auth import HTTPDigestAuth return HTTPDigestAuth(username, password) elif auth == 'guess': try: from requests_toolbelt.auth.guess import GuessAuth except ImportError: raise exceptions.UserError( 'Your version of requests_toolbelt is too ' 'old for `guess` authentication. At least ' 'version 0.4.0 is required.') else: return GuessAuth(username, password) else: raise exceptions.UserError( 'Unknown authentication method: {}'.format(auth)) elif auth: raise exceptions.UserError('You need to specify username and password ' 'for {} authentication.'.format(auth)) else: return None
def prepare_auth(auth, username, password): if username and password: if auth == "basic" or auth is None: return (username, password) elif auth == "digest": from requests.auth import HTTPDigestAuth return HTTPDigestAuth(username, password) elif auth == "guess": try: from requests_toolbelt.auth.guess import GuessAuth except ImportError: raise exceptions.UserError( "Your version of requests_toolbelt is too " "old for `guess` authentication. At least " "version 0.4.0 is required.") else: return GuessAuth(username, password) else: raise exceptions.UserError( "Unknown authentication method: {}".format(auth)) elif auth: raise exceptions.UserError("You need to specify username and password " "for {} authentication.".format(auth)) else: return None
def test_digest(self): with self.cassette('digest'): r = self.session.request( 'GET', 'http://httpbin.org/digest-auth/auth/user/passwd', auth=GuessAuth('user', 'passwd')) assert r.json() == {'authenticated': True, 'user': '******'}
def test_no_auth(self): with self.cassette('none'): url = 'http://httpbin.org/get?a=1' r = self.session.request('GET', url, auth=GuessAuth('user', 'passwd')) j = r.json() assert j['args'] == {'a': '1'} assert j['url'] == url assert 'user' not in r.text assert 'passwd' not in r.text