def test_get_set_clear_default_credential(self):
        self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
        organization = None
        token = str(uuid.uuid4())
        set_credential(organization, token)
        self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=False)
        self.assertEqual(token, retrieved_token)
        clear_credential(organization)
        self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=False)
        self.assertEqual(None, retrieved_token)

        # setting fall_back_to_default=True should have no effect
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=True)
        self.assertEqual(None, retrieved_token)
        set_credential(organization, token)
        self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=True)
        self.assertEqual(token, retrieved_token)
        clear_credential(organization)
        self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=True)
        self.assertEqual(None, retrieved_token)
 def test_get_set_clear_credential(self):
     organization = 'https://' + str(uuid.uuid4()) + '.visualstudio.com/'
     token = str(uuid.uuid4())
     retrieved_token = get_credential(organization,
                                      fall_back_to_default=False)
     self.assertEqual(None, retrieved_token)
     self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
     set_credential(organization, token)
     self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
     retrieved_token = get_credential(organization,
                                      fall_back_to_default=False)
     self.assertEqual(
         token,
         retrieved_token,
     )
     # test casing difference
     retrieved_token = get_credential(organization.upper(),
                                      fall_back_to_default=False)
     self.assertEqual(
         token,
         retrieved_token,
     )
     clear_credential(organization)
     self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
     retrieved_token = get_credential(organization,
                                      fall_back_to_default=False)
     self.assertEqual(None, retrieved_token)
 def tearDownClass(self):
     # restore original organization list file
     if self._ORIGINAL_ORGANIZATION_LIST is not None:
         with open(ORGANIZATION_LIST_FILE, "w") as output_file:
             output_file.writelines(self._ORIGINAL_ORGANIZATION_LIST)
     if self._ORIGINAL_DEFAULT_TOKEN is not None:
         # restore original token
         set_credential(None, self._ORIGINAL_DEFAULT_TOKEN)
    def test_get_credential_fallback(self):
        organization = 'https://' + str(uuid.uuid4()) + '.visualstudio.com/'
        token = str(uuid.uuid4())
        token_default = str(uuid.uuid4())

        self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
        set_credential(None, token_default)
        self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
        # this should return the default token, because there is no token set for organization
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=True)
        self.assertEqual(token_default, retrieved_token)

        set_credential(organization, token)
        # this should return the organization token, now that it is set
        retrieved_token = get_credential(organization,
                                         fall_back_to_default=True)
        self.assertEqual(token, retrieved_token)
def credential_set(organization=None):
    """Set the credential (PAT) to use for a particular organization.
    Refer https://aka.ms/azure-devops-cli-auth for more information on providing PAT as input.
    """
    token = _get_pat_token()
    if organization is not None:
        organization = get_base_url(organization)
        logger.info("Creating connection with personal access token.")
        _verify_token(organization=organization, token=token)
    try:
        set_credential(organization=organization, token=token)
    except Exception as ex:  # pylint: disable=bare-except
        logger.warning(
            "Unable to use secure credential store in this environment.")
        logger.warning(
            "Please refer to alternate methods at https://aka.ms/azure-devops-cli-auth"
        )
        logger.warning("using Environment variable")
        logger.warning("or use 'az login'")
        raise CLIError(ex)
    _check_and_set_default_organization(organization)
def credential_set(organization=None):
    """Set the credential (PAT) to use for a particular organization
    """
    token = _get_pat_token()
    if organization is not None:
        organization = get_base_url(organization)
        logger.info("Creating connection with personal access token.")
        credentials = BasicAuthentication('', token)
        connection = _get_vss_connection(organization, credentials)
        vstsDir = 'azext_devops.vstsCompressed.'
        location_client = connection.get_client(
            vstsDir + 'location.v4_1.location_client.LocationClient')
        try:
            connection_data = location_client.get_connection_data()
        except Exception as ex2:
            logger.debug(ex2, exc_info=True)
            raise CLIError("Failed to authenticate using the supplied token.")
        # An organization with public project enabled will not throw any exception for invalid token.
        # Hence, handle anonymous user case here.
        if connection_data.authenticated_user.id == _ANONYMOUS_USER_ID:
            raise CLIError("Failed to authenticate using the supplied token.")
    set_credential(organization=organization, token=token)
    _check_and_set_default_organization(organization)
    def test_organization_list_file(self):
        organization1 = 'https://' + str(uuid.uuid4()) + '.visualstudio.com/'
        token1 = str(uuid.uuid4())
        set_credential(organization1, token1)
        retrieved_token = get_credential(organization1,
                                         fall_back_to_default=False)
        self.assertEqual(
            token1,
            retrieved_token,
        )

        organization2 = 'https://' + str(uuid.uuid4()) + '.visualstudio.com/'
        token2 = str(uuid.uuid4())
        set_credential(organization2, token2)
        retrieved_token = get_credential(organization2,
                                         fall_back_to_default=False)
        self.assertEqual(
            token2,
            retrieved_token,
        )

        organization3 = 'https://' + str(uuid.uuid4()) + '.visualstudio.com/'
        token3 = str(uuid.uuid4())
        set_credential(organization3, token3)
        retrieved_token = get_credential(organization3,
                                         fall_back_to_default=False)
        self.assertEqual(
            token3,
            retrieved_token,
        )

        # logout out of organization 1
        retrieved_token = get_credential(organization1,
                                         fall_back_to_default=False)
        self.assertEqual(token1, retrieved_token)
        self.assertTrue(os.path.isfile(ORGANIZATION_LIST_FILE))
        clear_credential(organization1)
        retrieved_token = get_credential(organization1,
                                         fall_back_to_default=False)
        self.assertEqual(None, retrieved_token)

        # test logout all
        clear_credential(None)
        self.assertFalse(os.path.isfile(ORGANIZATION_LIST_FILE))
        retrieved_token = get_credential(organization2,
                                         fall_back_to_default=False)
        self.assertEqual(None, retrieved_token)
        retrieved_token = get_credential(organization3,
                                         fall_back_to_default=False)
        self.assertEqual(None, retrieved_token)