Skip to content
Merged
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
27 changes: 9 additions & 18 deletions cli/command/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strconv"
"wpm/cli"
"wpm/cli/command"
"wpm/pkg/api"
Expand Down Expand Up @@ -72,13 +71,7 @@ func tokenStdinPrompt(ctx context.Context, wpmCli command.Cli, opts *loginOption
return nil
}

type AuthResponse struct {
Uid int `json:"uid"`
Tid int `json:"tid"`
Username string `json:"username"`
}

func validateToken(wpmCli command.Cli, token string) (*AuthResponse, error) {
func validateToken(wpmCli command.Cli, token string) (string, error) {
client, err := api.NewRESTClient(api.ClientOptions{
Log: wpmCli.Err(),
AuthToken: token,
Expand All @@ -87,16 +80,16 @@ func validateToken(wpmCli command.Cli, token string) (*AuthResponse, error) {
LogColorize: !wpmTerm.IsColorDisabled() && term.IsTerminal(wpmCli.Err().FD()),
})
if err != nil {
return nil, err
return "", err
}

var response AuthResponse
var response string
err = wpmCli.Progress().RunWithProgress("validating token", func() error { return client.Get("/-/whoami", &response) }, wpmCli.Err())
if err != nil {
return nil, err
return "", err
}

return &response, nil
return response, nil
}

func runLogin(ctx context.Context, wpmCli command.Cli, opts loginOptions) error {
Expand All @@ -110,25 +103,23 @@ func runLogin(ctx context.Context, wpmCli command.Cli, opts loginOptions) error
}
}

resp, err := validateToken(wpmCli, opts.token)
username, err := validateToken(wpmCli, opts.token)
if err != nil {
return err
}
if resp == nil || resp.Username == "" || resp.Tid == 0 || resp.Uid == 0 {
if username == "" {
return errors.New(aec.RedF.Apply("unable to resolve identity from token"))
}

cfg := wpmCli.ConfigFile()
cfg.AuthToken = opts.token
cfg.DefaultUser = resp.Username
cfg.DefaultUId = strconv.Itoa(resp.Uid)
cfg.DefaultTId = strconv.Itoa(resp.Tid)
cfg.DefaultUser = username

if err := cfg.Save(); err != nil {
return err
}

_, _ = fmt.Fprintf(wpmCli.Out(), "welcome %s!\n", resp.Username)
_, _ = fmt.Fprintf(wpmCli.Out(), "welcome %s!\n", username)

return nil
}
2 changes: 1 addition & 1 deletion cli/command/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func runPublish(ctx context.Context, wpmCli command.Cli, opts publishOptions) er
}

cfg := wpmCli.ConfigFile()
if cfg.AuthToken == "" || cfg.DefaultTId == "" {
if cfg.DefaultUser == "" || cfg.AuthToken == "" {
return errors.New("user must be logged in to perform this action")
}

Expand Down
20 changes: 9 additions & 11 deletions cli/command/whoami/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"wpm/cli"
"wpm/cli/command"
"wpm/cli/command/auth"
"wpm/pkg/api"
"wpm/pkg/config"
wpmTerm "wpm/pkg/term"
Expand All @@ -26,7 +25,7 @@ func NewWhoamiCommand(wpmCli command.Cli) *cobra.Command {
return cmd
}

func validateToken(wpmCli command.Cli, token string) (*auth.AuthResponse, error) {
func validateToken(wpmCli command.Cli, token string) (string, error) {
client, err := api.NewRESTClient(api.ClientOptions{
Log: wpmCli.Err(),
AuthToken: token,
Expand All @@ -38,16 +37,16 @@ func validateToken(wpmCli command.Cli, token string) (*auth.AuthResponse, error)
LogColorize: !wpmTerm.IsColorDisabled() && term.IsTerminal(wpmCli.Err().FD()),
})
if err != nil {
return nil, err
return "", err
}

var response auth.AuthResponse
var response string
err = client.Get("/-/whoami", &response)
if err != nil {
return nil, err
return "", err
}

return &response, nil
return response, nil
}

func runWhoami(wpmCli command.Cli) error {
Expand All @@ -56,21 +55,20 @@ func runWhoami(wpmCli command.Cli) error {
return errors.New("user must be logged in to perform this action")
}

var resp *auth.AuthResponse
var username string
err := wpmCli.Progress().RunWithProgress("", func() error {
var err error
resp, err = validateToken(wpmCli, cfg.AuthToken)
username, err = validateToken(wpmCli, cfg.AuthToken)
return err
}, wpmCli.Out())

if err != nil {
return err
}
if resp.Username == "" {
if username == "" {
return errors.New(aec.RedF.Apply("unable to resolve identity from token"))
}

_, _ = fmt.Fprintln(wpmCli.Out(), resp.Username)

_, _ = fmt.Fprintln(wpmCli.Out(), username)
return nil
}
2 changes: 0 additions & 2 deletions pkg/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type ConfigFile struct {
Filename string `json:"-"` // Note: for internal use only
AuthToken string `json:"authToken,omitempty"`
DefaultUser string `json:"defaultUser,omitempty"`
DefaultUId string `json:"defaultUid,omitempty"`
DefaultTId string `json:"defaultTid,omitempty"`
UsersAuthTokens map[string]UsersAuthConfig `json:"usersAuthTokens,omitempty"`
PluginsAuthToken map[string]PluginsAuthConfig `json:"pluginsAuthToken,omitempty"`
}
Expand Down