Exemple #1
0
async def test_iterates_only_once():
    """When a credential succeeds, AzureApplicationCredential should use that credential thereafter"""

    expected_token = AccessToken("***", 42)
    unavailable_credential = Mock(get_token=Mock(
        side_effect=CredentialUnavailableError(message="...")))
    successful_credential = Mock(get_token=Mock(
        return_value=get_completed_future(expected_token)))

    credential = AzureApplicationCredential()
    credential.credentials = [
        unavailable_credential,
        successful_credential,
        Mock(get_token=Mock(side_effect=Exception(
            "iteration didn't stop after a credential provided a token"))),
    ]

    for n in range(3):
        token = await credential.get_token("scope")
        assert token.token == expected_token.token
        assert unavailable_credential.get_token.call_count == 1
        assert successful_credential.get_token.call_count == n + 1
Exemple #2
0
    async def get_token(self, *scopes, **kwargs):  # pylint:disable=unused-argument
        # type: (*str, **Any) -> AccessToken
        """Request an access token for `scopes`.
        This method is called automatically by Azure SDK clients.
        :param str scopes: desired scopes for the access token. This method only returns tokens for the https://quantum.microsoft.com/.default scope.
        :raises ~azure.identity.CredentialUnavailableError: when failing to get token. The exception has a
          `message` attribute with the error message.
        """
        if not self.token_file:
            raise CredentialUnavailableError(
                message="Token file location not set.")

        if not os.path.isfile(self.token_file):
            raise CredentialUnavailableError(
                message="Token file at {} does not exist.".format(
                    self.token_file))

        try:
            token = await self._parse_token_file(self.token_file)
        except JSONDecodeError:
            raise CredentialUnavailableError(
                message="Failed to parse token file: Invalid JSON.")
        except KeyError as e:
            raise CredentialUnavailableError(
                message="Failed to parse token file: Missing expected value: "
                + str(e))
        except Exception as e:
            raise CredentialUnavailableError(
                message="Failed to parse token file: " + str(e))

        if token.expires_on <= time.time():
            raise CredentialUnavailableError(
                message="Token already expired at {}".format(
                    time.asctime(time.gmtime(token.expires_on))))

        return token
Exemple #3
0
 async def credential_unavailable(message="it didn't work"):
     raise CredentialUnavailableError(message)