def test_request_authorization(self): """Test if we can request authorization correctly.""" cert = x509.parse_certificate(_TEST_CERT) auth = authenticator.Authenticator("Unit Test", cert=_TEST_CERT, key=_TEST_KEY) dest = authenticator.urlparse( auth.request_authorization( 'http://bazquux.foo/asdf/bsdf?q=x&r=x#asd')) self.assertEqual("https", dest.scheme) self.assertEqual("login.ancient-solutions.com", dest.hostname) self.assertEqual("/", dest.path) query = authenticator.parse_qs(dest.query) self.assertEqual(["token"], query["response_type"]) self.assertEqual(["http://bazquux.foo/asdf/bsdf?q=x&r=x#asd"], query["redirect_uri"]) self.assertIn("client_id", query) atr = token_pb2.AuthTokenRequest() cdc = token_cookie.AuthTokenRequestCodec(atr, pubkey=cert.get_pub_key()) try: cdc.decode(query["client_id"][0]) except token_cookie.SignatureException as e: self.fail("Cannot validate signature on auth token request") self.assertEqual("Unit Test", atr.app_name) self.assertEqual("http://bazquux.foo/asdf/bsdf?q=x&r=x#asd", atr.original_uri) self.assertEqual("http://bazquux.foo/login", atr.return_uri)
def test_authenticated_user(self): """Test if we can extract the user from a TokenCookie.""" key = importKey(_TEST_KEY) token = token_pb2.TokenCookie() token.basic_creds.user_name = 'testosteronius' token.basic_creds.scope.append('users') token.basic_creds.expires = calendar.timegm( datetime.utcnow().timetuple()) + 30 codec = token_cookie.TokenCookieCodec(token, privkey=key) cookie = codec.encode() auth = authenticator.Authenticator("Unit Test", cert=_TEST_CERT) self.assertEquals(auth.get_authenticated_user(cookie), 'testosteronius')
def test_login_handler(self): """Check whether an authentication token response is handled and decoded correctly by the authenticator implementation.""" now = datetime.now() expires = now + timedelta(0, 300) pkey = importKey(_TEST_KEY) # We need a CA signed certificate, so we use the one # from above. cert = x509.parse_certificate(_TEST_CERT) cacert = x509.parse_certificate(_TEST_CA) # Sanity check for the above certificates. self.assertTrue(cert.check_signature(cacert)) auth = authenticator.Authenticator("Unit Test", cert=_TEST_CERT, key=_TEST_KEY, ca_bundle=_TEST_CA) atres = token_pb2.AuthTokenResponse() atres.basic_creds.user_name = 'testosteronius' atres.basic_creds.scope.extend(['one', 'two']) atres.basic_creds.expires = calendar.timegm(expires.utctimetuple()) atres.app_name = 'Unit Test' atres.original_uri = 'http://lolcathost:8080/foo/bar' atres.certificate = _TEST_CERT atres.granted = calendar.timegm(now.utctimetuple()) # FIXME(caoimhe): this should go away. atres.random = 'A' * 64 atrc = token_cookie.AuthTokenResponseCodec(atres, privkey=pkey) response = atrc.encode() data = auth.login_handler(response) self.assertEquals(2, len(data)) cookiedata = data[0] nexturl = data[1] self.assertEquals('http://lolcathost:8080/foo/bar', nexturl)
from tweepy.streaming import StreamListener import authenticator from tweepy import Stream class TwitterStreamListener(StreamListener): def on_data(self, raw_data): print(raw_data) def on_error(self, status_code): print(status_code) if __name__ == "__main__": topic = "life" my_listener_obj = TwitterStreamListener() auth = authenticator.Authenticator().get_auth() actual_stream = Stream(auth, my_listener_obj) actual_stream.filter(track=[topic])
def __init__(self): self.api_instance = API(authenticator.Authenticator().get_auth())
def __init__(self, access_token, access_token_secret): self.auth = authenticator.Authenticator(tokens.CONSUMER_KEY, tokens.CONSUMER_SECRET, access_token, access_token_secret).authenticate() self.api = tweepy.API(self.auth)