-
Notifications
You must be signed in to change notification settings - Fork 267
Better handle auth when not using built-in auth #5954
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
base: main
Are you sure you want to change the base?
Changes from all commits
52c5f9c
43c3339
64afef9
059e365
7d81486
a66467c
a8bef4c
0944560
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.", | ||
|
||
| }) | ||
| 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() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
|
|
||
| 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) | ||
|
||
| } | ||
|
|
||
| // 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
|
||
Uh oh!
There was an error while loading. Please reload this page.