def test_existing_credentials_arguments_passed_on(self): # When re-using existing credentials, the arguments login_with # is called with are passed on the the __init__() method. os.makedirs( os.path.join(self.temp_dir, 'api.example.com', 'credentials')) credentials_file_path = os.path.join( self.temp_dir, 'api.example.com', 'credentials', 'app name') credentials = Credentials( 'app name', consumer_secret='consumer_secret:42', access_token=AccessToken('access_key:84', 'access_secret:168')) credentials.save_to_path(credentials_file_path) timeout = object() proxy_info = object() version = "foo" launchpad = NoNetworkLaunchpad.login_with( 'app name', launchpadlib_dir=self.temp_dir, service_root=SERVICE_ROOT, timeout=timeout, proxy_info=proxy_info, version=version) expected_arguments = dict( service_root=SERVICE_ROOT, timeout=timeout, proxy_info=proxy_info, version=version, cache=os.path.join(self.temp_dir, 'api.example.com', 'cache')) for key, expected in expected_arguments.items(): actual = launchpad.passed_in_args[key] self.assertEqual(actual, expected)
def test_from_string_with_context(self): access_token = AccessToken.from_string( "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key&" "lp.context=firefox") self.assertEqual("lock&key", access_token.key) self.assertEqual("secret=password", access_token.secret) self.assertEqual("firefox", access_token.context)
def launchpadlib_credentials_for(consumer_name, person, permission=OAuthPermission.WRITE_PRIVATE, context=None): """Create launchpadlib credentials for the given person. :param consumer_name: An OAuth consumer name. :param person: A person (or the name of a person) for whom to create or find credentials. :param permission: An OAuthPermission (or its token) designating the level of permission the credentials should have. :param context: The OAuth context for the credentials. :return: A launchpadlib Credentials object. """ # Start an interaction so that oauth_access_token_for will # succeed. oauth_access_token_for may be called in any layer, but # launchpadlib_credentials_for is only called in the # PageTestLayer, when a Launchpad instance is running for # launchpadlib to use. login(ANONYMOUS) access_token = oauth_access_token_for(consumer_name, person, permission, context) logout() launchpadlib_token = AccessToken(access_token.key, access_token.secret) return Credentials(consumer_name=consumer_name, access_token=launchpadlib_token)
def make_end_user_authorize_token(self, credentials, request_token): """Pretend to exchange a request token for an access token. We do this by simply setting the access_token property. """ credentials.access_token = AccessToken(self.ACCESS_TOKEN_KEY, 'access_secret:168') self.access_tokens_obtained += 1
def login(cls, consumer_name, token_string, access_secret, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, authorization_engine=None, allow_access_levels=None, max_failed_attempts=None, credential_store=None, credential_save_failed=None, version=DEFAULT_VERSION): """Convenience method for setting up access credentials. When all three pieces of credential information (the consumer name, the access token and the access secret) are available, this method can be used to quickly log into the service root. This method is deprecated as of launchpadlib version 1.9.0. You should use Launchpad.login_anonymously() for anonymous access, and Launchpad.login_with() for all other purposes. :param consumer_name: the application name. :type consumer_name: string :param token_string: the access token, as appropriate for the `AccessToken` constructor :type token_string: string :param access_secret: the access token's secret, as appropriate for the `AccessToken` constructor :type access_secret: string :param service_root: The URL to the root of the web service. :type service_root: string :param authorization_engine: See `Launchpad.__init__`. If you don't provide an authorization engine, a default engine will be constructed using your values for `service_root` and `credential_save_failed`. :param allow_access_levels: This argument is ignored, and only present to preserve backwards compatibility. :param max_failed_attempts: This argument is ignored, and only present to preserve backwards compatibility. :return: The web service root :rtype: `Launchpad` """ cls._warn_of_deprecated_login_method("login") access_token = AccessToken(token_string, access_secret) credentials = Credentials(consumer_name=consumer_name, access_token=access_token) if authorization_engine is None: authorization_engine = cls.authorization_engine_factory( service_root, consumer_name, allow_access_levels) if credential_store is None: credential_store = cls.credential_store_factory( credential_save_failed) return cls(credentials, authorization_engine, credential_store, service_root, cache, timeout, proxy_info, version)
def use_access_token(credentials): global user_agents if not session['access_token_parts']['oauth_token'] in user_agents: credentials.access_token = AccessToken.from_params( session['access_token_parts']) user_agents[credentials.access_token.key] = LaunchpadClient(credentials) is_authorized = True session['is_authorized'] = is_authorized return (False, None, is_authorized)
def use_access_token(credentials): global user_agents if not session['access_token_parts']['oauth_token'] in user_agents: credentials.access_token = AccessToken.from_params( session['access_token_parts']) user_agents[credentials.access_token.key] = LaunchpadClient( credentials) is_authorized = True session['is_authorized'] = is_authorized return (False, None, is_authorized)
def get_access_token(credentials): global user_agents credentials._request_token = AccessToken.from_params( session['request_token_parts']) request_token_key = credentials._request_token.key try: credentials.exchange_request_token_for_access_token(LPNET_WEB_ROOT) except Exception as e: return (False, None, False) user_agents[credentials.access_token.key] = LaunchpadClient(credentials) session['access_token_parts'] = { 'oauth_token': credentials.access_token.key, 'oauth_token_secret': credentials.access_token.secret, 'lp.context': credentials.access_token.context } is_authorized = True session['is_authorized'] = is_authorized del session['request_token_parts'] return (False, None, is_authorized)
def make_credential(self, consumer_key): """Helper method to make a fake credential.""" return Credentials("app name", consumer_secret='consumer_secret:42', access_token=AccessToken(consumer_key, 'access_secret:168'))
def test_from_string(self): access_token = AccessToken.from_string( "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key") self.assertEqual("lock&key", access_token.key) self.assertEqual("secret=password", access_token.secret) self.assertIsNone(access_token.context)
def __init__(self, token_string, access_secret): self.token_string = token_string self.access_secret = access_secret self.token = AccessToken(token_string, access_secret) self.credentials = Credentials(consumer_name="launchpad-library", access_token=self.token)