Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cli/azd/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,34 @@ func newLoginAction(
}

func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) {
loginMode, err := la.authManager.Mode()
if err != nil {
return nil, err
}
if loginMode != auth.AzdBuiltIn && !la.flags.onlyCheckStatus {
la.console.MessageUxItem(ctx, &ux.WarningAltMessage{
Message: fmt.Sprintf(
"Azd is not using the built-in authentication mode, but rather '%s'", loginMode),
})
la.console.Message(ctx, "If you want to use 'azd auth login', you need to disable the current auth mode.")
response, err := la.console.Confirm(ctx, input.ConsoleOptions{
Message: "Do you want to switch back to azd built-in authentication?",
DefaultValue: false,
Help: "Azd supports multiple authentication modes, including Azure CLI authentication and External " +
"request for Auth. Switching back to azd built-in authentication will try to disable the current mode.",
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The word "Auth" should be lowercase "auth" for consistency with the rest of the message. The capitalization of "Auth" here appears to be unintentional as it's not a proper noun or acronym in this context.

Copilot uses AI. Check for mistakes.
})
if err != nil {
return nil, err
}
if !response {
return nil, fmt.Errorf("log in is not supported on current mode: %s", loginMode)
}
if err := la.authManager.SetBuiltInAuthMode(); err != nil {
return nil, fmt.Errorf("setting auth mode: %w", err)
}
la.console.Message(ctx, "Authentication mode set to azd built-in. Continuing login...")
}

if len(la.flags.scopes) == 0 {
la.flags.scopes = la.authManager.LoginScopes()
}
Expand Down
64 changes: 64 additions & 0 deletions cli/azd/pkg/auth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,3 +1409,67 @@ func (m *Manager) LogInDetails(ctx context.Context) (*LogInDetails, error) {

return nil, ErrNoCurrentUser
}

type AuthMode string

const (
AzdBuiltIn AuthMode = "azd built in"
AzDelegated AuthMode = "delegated to az cli"
ExternalRequest AuthMode = "external token request"
)

func (m *Manager) Mode() (AuthMode, error) {
// Check external
if m.UseExternalAuth() {
return ExternalRequest, nil
}

// check az delegation
cfg, err := m.userConfigManager.Load()
if err != nil {
return "", fmt.Errorf("fetching current user: %w", err)
}

if shouldUseLegacyAuth(cfg) {
return AzDelegated, nil
}

// default to azd
return AzdBuiltIn, nil
}
Comment on lines +1421 to +1439
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The new Mode() method lacks test coverage. This method has multiple code paths (external auth, az delegation, and built-in) that should be tested to ensure the auth mode detection works correctly in all scenarios. Consider adding tests in manager_test.go to cover: 1) external auth mode detection when UseExternalAuth() returns true, 2) az delegation mode when useAzCliAuthKey is set, and 3) built-in mode as the default case.

Copilot uses AI. Check for mistakes.

func (m *Manager) SetBuiltInAuthMode() error {
currentMode, err := m.Mode()
if err != nil {
return fmt.Errorf("fetching current auth mode: %w", err)
}
if currentMode == AzdBuiltIn {
return nil
}

if currentMode == ExternalRequest {
return fmt.Errorf("cannot change auth mode when external token mode is set. See %s",
"https://github.com/Azure/azure-dev/blob/main/cli/azd/docs/external-authentication.md")
}

// protecting against unexpected modes. There should be only azDelegated left.
if currentMode != AzDelegated {
return fmt.Errorf("Unexpected mode found: %s", currentMode)
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

Error message should start with a lowercase letter to follow Go error message conventions. Error messages in Go should not be capitalized (unless starting with proper nouns or acronyms) as they are often wrapped by other error messages.

Copilot uses AI. Check for mistakes.
}

// Unset the useAzCliAuthKey flag
cfg, err := m.userConfigManager.Load()
if err != nil {
return fmt.Errorf("reading user config: %w", err)
}

if err := cfg.Unset(useAzCliAuthKey); err != nil {
return fmt.Errorf("unsetting %s: %w", useAzCliAuthKey, err)
}

if err := m.userConfigManager.Save(cfg); err != nil {
return fmt.Errorf("saving user config: %w", err)
}

return nil
}
Comment on lines +1441 to +1475
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The new SetBuiltInAuthMode() method lacks test coverage. This method has several important code paths that should be tested: 1) no-op when already in built-in mode, 2) error when in external request mode, 3) successful switch from az delegation mode to built-in mode (unsetting the useAzCliAuthKey flag), and 4) proper error handling when loading/saving config fails. Consider adding tests in manager_test.go similar to TestLegacyAzCliCredentialSupport to verify the config changes are correctly applied.

Copilot uses AI. Check for mistakes.
Loading