Beispiel #1
0
 def _update_token(self, user, key_to_update, consumer_name):
     try:
         creds_tuple = convert_string_to_tuple(key_to_update)
     except ValueError as e:
         raise CommandError(e)
     _, token_key, _ = creds_tuple
     try:
         user.userprofile.modify_consumer_name(token_key, consumer_name)
     except Http404:
         raise CommandError("No matching api key found.")
Beispiel #2
0
 def _delete_token(self, user, key_to_delete):
     # Extract the token key from the api key string.
     try:
         creds_tuple = convert_string_to_tuple(key_to_delete)
     except ValueError as e:
         raise CommandError(e)
     _, token_key, _ = creds_tuple
     try:
         user.userprofile.delete_authorisation_token(token_key)
     except Http404:
         raise CommandError("No matching api key found.")
Beispiel #3
0
 def test_check_valid_apikey_catches_invalid_key(self):
     options = make_options()
     content = factory.make_name('content')
     mock_request = self.patch(httplib2.Http, 'request')
     response = httplib2.Response({})
     response['status'] = http.client.UNAUTHORIZED
     mock_request.return_value = response, json.dumps(content)
     self.assertFalse(
         auth.check_valid_apikey(
             options.url, convert_string_to_tuple(options.credentials),
             options.insecure))
Beispiel #4
0
 def test_check_valid_apikey_raises_if_unexpected_response(self):
     options = make_options()
     content = factory.make_name('content')
     mock_request = self.patch(httplib2.Http, 'request')
     response = httplib2.Response({})
     response['status'] = http.client.GONE
     mock_request.return_value = response, json.dumps(content)
     self.assertRaises(auth.UnexpectedResponse, auth.check_valid_apikey,
                       options.url,
                       convert_string_to_tuple(options.credentials),
                       options.insecure)
Beispiel #5
0
def get_recorded_api_credentials():
    """Return API credentials as last received from the server.

    :return: If credentials have been received, a tuple of
        (consumer_key, resource_token, resource_secret) as expected by
        :class:`MAASOauth`.  Otherwise, None.
    """
    credentials_string = cache.cache.get(API_CREDENTIALS_CACHE_KEY)
    if credentials_string is None:
        return None
    else:
        return convert_string_to_tuple(credentials_string)
Beispiel #6
0
def get_recorded_api_credentials():
    """Return API credentials as last received from the server.

    :return: If credentials have been received, a tuple of
        (consumer_key, resource_token, resource_secret) as expected by
        :class:`MAASOauth`.  Otherwise, None.
    """
    credentials_string = cache.cache.get(API_CREDENTIALS_CACHE_KEY)
    if credentials_string is None:
        return None
    else:
        return convert_string_to_tuple(credentials_string)
Beispiel #7
0
 def test_cmd_login_ensures_valid_apikey(self):
     parser = ArgumentParser()
     options = make_options()
     check_key = self.patch(cli, "check_valid_apikey")
     check_key.return_value = False
     login = cli.cmd_login(parser)
     error = self.assertRaises(SystemExit, login, options)
     self.assertEqual("The MAAS server rejected your API key.", str(error))
     check_key.assert_called_once_with(
         options.url,
         convert_string_to_tuple(options.credentials),
         options.insecure,
     )
Beispiel #8
0
 def test_check_valid_apikey_passes_valid_key(self):
     options = make_options()
     content = factory.make_name("content")
     mock_request = self.patch(httplib2.Http, "request")
     response = httplib2.Response({})
     response["status"] = http.client.OK
     mock_request.return_value = response, json.dumps(content)
     self.assertTrue(
         auth.check_valid_apikey(
             options.url,
             convert_string_to_tuple(options.credentials),
             options.insecure,
         ))
Beispiel #9
0
def obtain_credentials(credentials):
    """Prompt for credentials if possible.

    If the credentials are "-" then read from stdin without interactive
    prompting.
    """
    if credentials == "-":
        credentials = sys.stdin.readline().strip()
    elif credentials is None:
        credentials = try_getpass(
            "API key (leave empty for anonymous access): ")
    # Ensure that the credentials have a valid form.
    if credentials and not credentials.isspace():
        return convert_string_to_tuple(credentials)
    else:
        return None
Beispiel #10
0
def obtain_credentials(credentials):
    """Prompt for credentials if possible.

    If the credentials are "-" then read from stdin without interactive
    prompting.
    """
    if credentials == "-":
        credentials = sys.stdin.readline().strip()
    elif credentials is None:
        credentials = try_getpass(
            "API key (leave empty for anonymous access): ")
    # Ensure that the credentials have a valid form.
    if credentials and not credentials.isspace():
        return convert_string_to_tuple(credentials)
    else:
        return None
Beispiel #11
0
 def test_convert_string_to_tuple_inverts_convert_tuple_to_string(self):
     creds_tuple = self.make_tuple()
     self.assertEqual(
         creds_tuple,
         convert_string_to_tuple(convert_tuple_to_string(creds_tuple)))
Beispiel #12
0
 def test_get_creds_tuple_integrates_with_api_client(self):
     creds_tuple = get_creds_tuple(create_auth_token(factory.make_user()))
     self.assertEqual(
         creds_tuple,
         convert_string_to_tuple(convert_tuple_to_string(creds_tuple)))
Beispiel #13
0
 def test_convert_string_to_tuple_converts_string_to_tuple(self):
     creds_tuple = self.make_tuple()
     creds_string = ':'.join(creds_tuple)
     self.assertEqual(creds_tuple, convert_string_to_tuple(creds_string))
Beispiel #14
0
 def test_convert_string_to_tuple_converts_string_to_tuple(self):
     creds_tuple = self.make_tuple()
     creds_string = ':'.join(creds_tuple)
     self.assertEqual(creds_tuple, convert_string_to_tuple(creds_string))
Beispiel #15
0
 def test_get_creds_tuple_integrates_with_api_client(self):
     creds_tuple = get_creds_tuple(create_auth_token(factory.make_user()))
     self.assertEqual(
         creds_tuple,
         convert_string_to_tuple(convert_tuple_to_string(creds_tuple)))
Beispiel #16
0
 def test_convert_string_to_tuple_inverts_convert_tuple_to_string(self):
     creds_tuple = self.make_tuple()
     self.assertEqual(
         creds_tuple,
         convert_string_to_tuple(convert_tuple_to_string(creds_tuple)))
Beispiel #17
0
 def _delete_token(self, user, key_to_delete):
     # Extract the token key from the api key string.
     try:
         creds_tuple = convert_string_to_tuple(key_to_delete)
     except ValueError, e:
         raise CommandError(e)
Beispiel #18
0
 def _delete_token(self, user, key_to_delete):
     # Extract the token key from the api key string.
     try:
         creds_tuple = convert_string_to_tuple(key_to_delete)
     except ValueError, e:
         raise CommandError(e)