diff --git a/cli/azd/cmd/auth_login.go b/cli/azd/cmd/auth_login.go index 6103f737c70..efb30e08508 100644 --- a/cli/azd/cmd/auth_login.go +++ b/cli/azd/cmd/auth_login.go @@ -325,7 +325,30 @@ func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) { panic("Unhandled login status") } - fmt.Fprintln(la.console.Handles().Stdout, msg) + // get user account information - login --check-status + act, err := la.authManager.GetSignedInAccount(ctx) + + // error getting user account + if err != nil { + log.Printf("error: getting signed in account: %v", err) + fmt.Fprintln(la.console.Handles().Stdout, msg) + return nil, nil + } + + // service principal account - login --check-status + if act == nil { + // get service principal client id and display it + value, err := la.authManager.GetLoggedInServicePrincipalClientID(ctx) + if err != nil || value == nil { + log.Printf("error: getting signed in service principal: %v", err) + fmt.Fprintln(la.console.Handles().Stdout, msg) + return nil, nil + } + fmt.Fprintln(la.console.Handles().Stdout, fmt.Sprintf("(%s) %s", *value, msg)) + return nil, nil + } + + fmt.Fprintln(la.console.Handles().Stdout, fmt.Sprintf("(%s) %s", act.PreferredUsername, msg)) return nil, nil } } @@ -349,7 +372,30 @@ func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) { } } - la.console.Message(ctx, "Logged in to Azure.") + // get user account information - login + act, err := la.authManager.GetSignedInAccount(ctx) + + // error getting user account, successful log in + if err != nil { + log.Printf("error: getting signed in account: %v", err) + la.console.Message(ctx, cLoginSuccessMessage) + return nil, nil + } + + // service principal account - login + if act == nil { + // get service principal client id and display it + value, err := la.authManager.GetLoggedInServicePrincipalClientID(ctx) + if err != nil || value == nil { + log.Printf("error: getting signed in service principal: %v", err) + fmt.Fprintln(la.console.Handles().Stdout, cLoginSuccessMessage) + return nil, nil + } + fmt.Fprintln(la.console.Handles().Stdout, fmt.Sprintf("(%s) %s", *value, cLoginSuccessMessage)) + return nil, nil + } + + la.console.Message(ctx, fmt.Sprintf("(%s) %s", act.PreferredUsername, cLoginSuccessMessage)) return nil, nil } diff --git a/cli/azd/pkg/auth/manager.go b/cli/azd/pkg/auth/manager.go index c051e3820a1..10aeae8e9b1 100644 --- a/cli/azd/pkg/auth/manager.go +++ b/cli/azd/pkg/auth/manager.go @@ -852,7 +852,7 @@ func (m *Manager) LoginWithAzurePipelinesFederatedTokenProvider( // Logout signs out the current user and removes any cached authentication information func (m *Manager) Logout(ctx context.Context) error { - act, err := m.getSignedInAccount(ctx) + act, err := m.GetSignedInAccount(ctx) if err != nil && !errors.Is(err, ErrNoCurrentUser) { return fmt.Errorf("fetching current user: %w", err) } @@ -932,9 +932,9 @@ func (m *Manager) saveLoginForServicePrincipal(tenantId, clientId string, secret return nil } -// getSignedInAccount fetches the public.Account for the signed in user, or nil if one does not exist +// GetSignedInAccount fetches the public.Account for the signed in user, or nil if one does not exist // (e.g when logged in with a service principal). -func (m *Manager) getSignedInAccount(ctx context.Context) (*public.Account, error) { +func (m *Manager) GetSignedInAccount(ctx context.Context) (*public.Account, error) { cfg, err := m.readAuthConfig() if err != nil { return nil, fmt.Errorf("fetching current user: %w", err) @@ -960,6 +960,26 @@ func (m *Manager) getSignedInAccount(ctx context.Context) (*public.Account, erro return nil, nil } +// GetLoggedInServicePrincipalClientID fetches the client ID for the signed in service principal, +// or nil if one does not exist. +func (m *Manager) GetLoggedInServicePrincipalClientID(ctx context.Context) (*string, error) { + cfg, err := m.readAuthConfig() + if err != nil { + return nil, fmt.Errorf("fetching current user: %w", err) + } + + currentUser, err := readUserProperties(cfg) + if err != nil { + return nil, ErrNoCurrentUser + } + + if currentUser.ClientID != nil { + return currentUser.ClientID, nil + } + + return nil, nil +} + // saveUserProperties writes the properties under [cCurrentUserKey], overwriting any existing value. func (m *Manager) saveUserProperties(user *userProperties) error { cfg, err := m.readAuthConfig()