Skip to content

Commit

Permalink
apply Victor and Wei comments
Browse files Browse the repository at this point in the history
  • Loading branch information
john0isaac committed Jan 11, 2025
1 parent 515d6e7 commit 0020bd3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
54 changes: 13 additions & 41 deletions cli/azd/cmd/auth_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,38 +318,34 @@ 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")
}

// get user account information - login --check-status
act, err := la.authManager.GetSignedInAccount(ctx)
details, err := la.authManager.LogInDetails(ctx)

// error getting user account
if err != nil {
log.Printf("error: getting signed in account: %v", err)
fmt.Fprintln(la.console.Handles().Stdout, msg)
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))
switch details.LoginType {
case auth.EmailLoginType:
fmt.Fprintf(la.console.Handles().Stdout, "%s as %s.", msg, output.WithBold("%s", details.Account))
return nil, nil
case auth.ClientIdLoginType:
fmt.Fprintf(la.console.Handles().Stdout, "%s as (%s).", msg, output.WithGrayFormat("%s", details.Account))
return nil, nil
default:
fmt.Fprintf(la.console.Handles().Stdout, "%s.", msg)
return nil, nil
}

fmt.Fprintln(la.console.Handles().Stdout, fmt.Sprintf("(%s) %s", act.PreferredUsername, msg))
return nil, nil
}
}

Expand Down Expand Up @@ -377,30 +373,6 @@ func (la *loginAction) Run(ctx context.Context) (*actions.ActionResult, error) {
}
}

// 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
}

Expand Down
73 changes: 50 additions & 23 deletions cli/azd/pkg/auth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -960,26 +960,6 @@ 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()
Expand Down Expand Up @@ -1154,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
}

0 comments on commit 0020bd3

Please sign in to comment.