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
4 changes: 2 additions & 2 deletions cmd/device_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func consoleTopic(identifier string) string {
// device in status messages.
func runDeviceTerminalSession(cmd *cobra.Command, topic, label string, proto terminalProtocol) error {
cfg := config.From(cmd.Context())
if cfg.Token == "" {
return errors.New("not authenticated: run `nh user auth` or set NERVES_HUB_TOKEN")
if err := requireToken(cfg); err != nil {
return err
}

in, out, ok := consoleTerminal(cmd)
Expand Down
19 changes: 14 additions & 5 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,24 @@ func userAgent() string {
return "nh/" + version
}

// requireToken returns an error when there's no token to send: why the saved
// one can't be used (it's for another server), or that there's none at all.
func requireToken(cfg *config.Config) error {
if cfg.Token != "" {
return nil
}
if cfg.TokenErr != nil {
return cfg.TokenErr
}
return errors.New("not authenticated: run `nh user auth` or set NERVES_HUB_TOKEN")
}

// newAuthedClient builds an API client using the resolved token, returning a
// helpful error when no token is available so commands fail clearly rather
// than with a 401. Extra options (e.g. a custom HTTP client) may be supplied.
func newAuthedClient(cfg *config.Config, opts ...api.Option) (*api.Client, error) {
if cfg.Token == "" {
if cfg.TokenErr != nil {
return nil, cfg.TokenErr
}
return nil, errors.New("not authenticated: run `nh user auth` or set NERVES_HUB_TOKEN")
if err := requireToken(cfg); err != nil {
return nil, err
}
opts = append([]api.Option{api.WithUserAgent(userAgent())}, opts...)
return api.NewClient(cfg.URI, cfg.Token, opts...)
Expand Down
25 changes: 25 additions & 0 deletions cmd/token_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,28 @@ func TestExplicitEmptyTokenFlagSkipsSavedToken(t *testing.T) {
t.Errorf("got token=%q err=%v", c.Token, c.TokenErr)
}
}

// The console and shell open a websocket rather than an API client, and give
// the same error as API commands when the saved token is for another server.
func TestTerminalSessionsNameTheSavedTokensServer(t *testing.T) {
for _, sub := range []string{"console", "shell"} {
t.Run(sub, func(t *testing.T) {
t.Setenv("NERVES_CLOUD_TOKEN", "")
t.Setenv("NERVES_HUB_TOKEN", "")
dir := t.TempDir()
if _, err := config.SaveToken(dir, "https://hub.corp", "nhu_corp"); err != nil {
t.Fatal(err)
}

_, err := execCmd(t, "", "device", sub, "dev-001",
"--org", "acme", "--uri", "https://example.com", "--data-dir", dir)
var mismatch *config.TokenMismatchError
if !errors.As(err, &mismatch) {
t.Fatalf("want a token mismatch error, got %v", err)
}
if !strings.Contains(err.Error(), "saved token is for https://hub.corp") {
t.Errorf("error should name the token's server: %v", err)
}
})
}
}
Loading