Skip to content
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

Feature Show Logged in account email or client ID for azd auth login and azd auth login --check-status #2856

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
34 changes: 30 additions & 4 deletions cli/azd/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/oneauth"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/tools/github"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -318,14 +319,28 @@ func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) {
var msg string
switch res.Status {
case contracts.LoginStatusSuccess:
msg = "Logged in to Azure."
msg = "Logged in to Azure"
case contracts.LoginStatusUnauthenticated:
msg = "Not logged in, run `azd auth login` to login to Azure."
msg = "Not logged in, run `azd auth login` to login to Azure"
default:
panic("Unhandled login status")
}

fmt.Fprintln(la.console.Handles().Stdout, msg)
// get user account information - login --check-status
details, err := la.authManager.LogInDetails(ctx)

// error getting user account or not logged in
if err != nil {
log.Printf("error: getting signed in account: %v", err)
fmt.Fprintln(la.console.Handles().Stdout, msg)
return nil, nil
}

// only print the message if the user is logged in
la.console.MessageUxItem(ctx, &ux.LoggedIn{
LoggedInAs: details.Account,
LoginType: ux.LoginType(details.LoginType),
})
return nil, nil
}
}
Expand Down Expand Up @@ -354,7 +369,18 @@ func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}
}

la.console.Message(ctx, "Logged in to Azure.")
details, err := la.authManager.LogInDetails(ctx)

// error getting user account, successful log in
if err != nil {
log.Printf("error: getting signed in account: %v", err)
la.console.Message(ctx, "Logged in to Azure")
return nil, nil
}
la.console.MessageUxItem(ctx, &ux.LoggedIn{
LoggedInAs: details.Account,
LoginType: ux.LoginType(details.LoginType),
})
return nil, nil
}

Expand Down
47 changes: 47 additions & 0 deletions cli/azd/pkg/auth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,3 +1134,50 @@ func readUserProperties(cfg config.Config) (*userProperties, error) {

return &user, nil
}

const (
EmailLoginType LoginType = "email"
ClientIdLoginType LoginType = "clientId"
)

type LoginType string

type LogInDetails struct {
LoginType LoginType
Account string
}

// LogInDetails method for Manager to return login details
func (m *Manager) LogInDetails(ctx context.Context) (*LogInDetails, 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.HomeAccountID != nil {
accounts, err := m.publicClient.Accounts(ctx)
if err != nil {
return nil, err
}
for _, account := range accounts {
if account.HomeAccountID == *currentUser.HomeAccountID {
return &LogInDetails{
LoginType: EmailLoginType,
Account: account.PreferredUsername,
}, nil
}
}
} else if currentUser.ClientID != nil {
return &LogInDetails{
LoginType: ClientIdLoginType,
Account: *currentUser.ClientID,
}, nil
}

return nil, ErrNoCurrentUser
}
53 changes: 53 additions & 0 deletions cli/azd/pkg/output/ux/logged_in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package ux

import (
"encoding/json"
"fmt"

"github.com/azure/azure-dev/cli/azd/pkg/output"
)

const cLoginSuccessMessage = "Logged in to Azure"
const (
EmailLoginType LoginType = "email"
ClientIdLoginType LoginType = "clientId"
)

type LoginType string

type LoggedIn struct {
LoggedInAs string
LoginType LoginType
}

func (cr *LoggedIn) ToString(currentIndentation string) string {
switch cr.LoginType {
case EmailLoginType:
return fmt.Sprintf(
"%s%s as %s",
currentIndentation,
cLoginSuccessMessage,
output.WithBold("%s", cr.LoggedInAs))
case ClientIdLoginType:
return fmt.Sprintf(
"%s%s as (%s)",
currentIndentation,
cLoginSuccessMessage,
output.WithGrayFormat("%s", cr.LoggedInAs))
default:
}

return fmt.Sprintf(
"%s%s",
currentIndentation,
cLoginSuccessMessage)
}

func (cr *LoggedIn) MarshalJSON() ([]byte, error) {
// reusing the same envelope from console messages
return json.Marshal(output.EventForMessage(
fmt.Sprintf("%s as %s", cLoginSuccessMessage, cr.LoggedInAs)))
}