Skip to content

Add MFA support for azd #42488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add MFA support for azd #42488

wants to merge 2 commits into from

Conversation

xiangyan99
Copy link
Member

For AzdCredential, upon receiving claims during the get_token call, we need to invoke azd auth token --claims <claims_challenge> to allow Azd to store the claims.

Subsequently, we notify the user to run azd auth login again to re-authenticate.

@xiangyan99 xiangyan99 marked this pull request as ready for review August 13, 2025 23:59
@Copilot Copilot AI review requested due to automatic review settings August 13, 2025 23:59
@xiangyan99 xiangyan99 requested review from pvaneck and a team as code owners August 13, 2025 23:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds Multi-Factor Authentication (MFA) support for the Azure Developer CLI credential by implementing claims handling in both sync and async versions of AzureDeveloperCliCredential.

Key changes:

  • Enables the claims parameter in get_token() methods to handle MFA challenges
  • Adds a new utility function to extract user-friendly error messages from azd CLI JSON output
  • Improves error handling by parsing structured error messages from azd CLI output

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
sdk/identity/azure-identity/azure/identity/aio/_credentials/azd_cli.py Async version - adds claims parameter support and improved error message extraction
sdk/identity/azure-identity/azure/identity/_credentials/azd_cli.py Sync version - implements claims parameter support and adds extract_cli_error_message utility function
Comments suppressed due to low confidence (1)

sdk/identity/azure-identity/azure/identity/_credentials/azd_cli.py:329

  • Use f-string formatting instead of .format() for consistency with the rest of the codebase. Replace with combined_text = f"{ex.output or ''}\n{ex.stderr or ''}"
        combined_text = "{}\n{}".format(ex.output or "", ex.stderr or "")

@@ -175,16 +179,23 @@ def _get_token_base(
)
if tenant:
command_args += ["--tenant-id", tenant]
if claims:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If claims are populated, shouldn't we raise immediately with the error message containing the command to run to login again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The azd implementation differs from the CLI/PSH by not using "azd auth login --claims."

Instead, users must first run "azd auth token --claims", which stores the claims.

Then, users can execute "azd auth login" without the "--claims" flag, as azd retrieves the claims from the cached data created in the previous step.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but shouldn't we raise immediately when we have claims to tell the user to run those commands to proceed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my observations, the command 'azd auth token --claims' may return a valid token, such as when the device is enrolled. To ensure robust code, let's handle both scenarios.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we know if it is a valid token? Once we leave the credential and try to use the token in a policy, where would we throw with the azd specific error message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we execute azd auth token --claims and receive a token, it is considered valid.

If token retrieval fails, an error is returned in JSON format from azd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

2 participants