ca90ce422f
gh / lark / gcloud / stripe all ship a logout command and a way to
enumerate stored credentials on day one. WeKnora's `auth` subtree had
only login + status, leaving no documented purge path for keyring
secrets — a real concern for `--with-token` (sk-…) and JWT flows that
write credentials to OS keychains.
auth logout [--name <ctx>] [--all] [--json]
Clears keyring + file-fallback secrets (access / refresh / api_key
slots) for the named context (default: current) or every context
with --all. Removes the context entry from ~/.config/weknora/config.yaml
and clears current_context if the removed entry was active.
Mirrors `gh auth logout` and `lark auth logout`. As gh documents,
this does NOT revoke server-side — for API keys users must rotate in
the server UI, JWTs continue to be accepted until expiry.
auth list [--json]
Renders a compact table (NAME / HOST / USER / MODE) with the active
context marked `*`. Reads only config.yaml — no network, no keyring
touch. Mode is inferred from which credential ref is set (api_key
→ "api-key", token → "password"; both → "password" wins).
Mirrors gh's per-host enumeration (gh auth status iterates accounts)
and lark `auth list`. For weknora the contexts file already had this
data — the command is a thin renderer to match user muscle memory.
Deferred to a follow-up release:
- auth refresh + transparent 401 retry in the SDK (we already persist
refresh_token at login but never spend it; explicit gap)
- login --web browser OAuth flow (requires a server-side endpoint)
- auth token printer (cheap; defer with the rest)
Tests: 24 cli packages green. New: cmd/auth/logout_test.go (current
context, named, --all, no-contexts, unknown-name, no-current-no-flag,
mutex flags) + cmd/auth/list_test.go (human render, empty, JSON
envelope, inferMode edge cases). AGENTS.md command-surface note adds
the four-command auth subtree; screenshot section 4 adds `auth list`
alongside `auth status`.
151 lines
5.0 KiB
Go
151 lines
5.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/config"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
"github.com/Tencent/WeKnora/cli/internal/secrets"
|
|
)
|
|
|
|
// newLogoutFactory builds a Factory whose Config closure mutates the supplied
|
|
// cfg in place — runLogout writes back via config.Save which touches disk, so
|
|
// tests use t.Setenv("XDG_CONFIG_HOME", t.TempDir()) at the call site to
|
|
// isolate the on-disk file.
|
|
func newLogoutFactory(t *testing.T, cfg *config.Config, store secrets.Store) *cmdutil.Factory {
|
|
t.Helper()
|
|
return &cmdutil.Factory{
|
|
Config: func() (*config.Config, error) { return cfg, nil },
|
|
Secrets: func() (secrets.Store, error) { return store, nil },
|
|
}
|
|
}
|
|
|
|
func isolateConfig(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
}
|
|
|
|
func TestLogout_CurrentContext(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
store := secrets.NewMemStore()
|
|
require.NoError(t, store.Set("prod", "access", "jwt-prod"))
|
|
require.NoError(t, store.Set("prod", "refresh", "rfr-prod"))
|
|
require.NoError(t, store.Set("staging", "api_key", "sk-staging"))
|
|
|
|
cfg := &config.Config{
|
|
CurrentContext: "prod",
|
|
Contexts: map[string]config.Context{
|
|
"prod": {Host: "https://prod", TokenRef: store.Ref("prod", "access"), RefreshRef: store.Ref("prod", "refresh")},
|
|
"staging": {Host: "https://staging", APIKeyRef: store.Ref("staging", "api_key")},
|
|
},
|
|
}
|
|
require.NoError(t, runLogout(&LogoutOptions{}, newLogoutFactory(t, cfg, store)))
|
|
|
|
assert.Empty(t, cfg.CurrentContext, "current_context should clear when removed")
|
|
assert.NotContains(t, cfg.Contexts, "prod")
|
|
assert.Contains(t, cfg.Contexts, "staging", "non-target context untouched")
|
|
|
|
// Secrets gone for the removed context, kept for the survivor.
|
|
if _, err := store.Get("prod", "access"); err == nil {
|
|
t.Error("prod access secret should be deleted")
|
|
}
|
|
if v, _ := store.Get("staging", "api_key"); v != "sk-staging" {
|
|
t.Errorf("staging secret unexpectedly cleared: %q", v)
|
|
}
|
|
}
|
|
|
|
func TestLogout_NamedContext(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
store := secrets.NewMemStore()
|
|
require.NoError(t, store.Set("staging", "api_key", "sk-staging"))
|
|
|
|
cfg := &config.Config{
|
|
CurrentContext: "prod",
|
|
Contexts: map[string]config.Context{
|
|
"prod": {Host: "https://prod", TokenRef: "tok"},
|
|
"staging": {Host: "https://staging", APIKeyRef: store.Ref("staging", "api_key")},
|
|
},
|
|
}
|
|
require.NoError(t, runLogout(&LogoutOptions{Name: "staging"}, newLogoutFactory(t, cfg, store)))
|
|
|
|
assert.Equal(t, "prod", cfg.CurrentContext, "current_context untouched when removing other")
|
|
assert.NotContains(t, cfg.Contexts, "staging")
|
|
assert.Contains(t, cfg.Contexts, "prod")
|
|
}
|
|
|
|
func TestLogout_All(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
store := secrets.NewMemStore()
|
|
cfg := &config.Config{
|
|
CurrentContext: "prod",
|
|
Contexts: map[string]config.Context{
|
|
"prod": {Host: "https://prod"},
|
|
"staging": {Host: "https://staging"},
|
|
},
|
|
}
|
|
require.NoError(t, runLogout(&LogoutOptions{All: true}, newLogoutFactory(t, cfg, store)))
|
|
|
|
assert.Empty(t, cfg.Contexts)
|
|
assert.Empty(t, cfg.CurrentContext)
|
|
}
|
|
|
|
func TestLogout_NoContexts(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
cfg := &config.Config{}
|
|
err := runLogout(&LogoutOptions{}, newLogoutFactory(t, cfg, secrets.NewMemStore()))
|
|
require.Error(t, err)
|
|
var typed *cmdutil.Error
|
|
require.ErrorAs(t, err, &typed)
|
|
assert.Equal(t, cmdutil.CodeAuthUnauthenticated, typed.Code)
|
|
}
|
|
|
|
func TestLogout_UnknownName(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
cfg := &config.Config{
|
|
CurrentContext: "prod",
|
|
Contexts: map[string]config.Context{"prod": {Host: "https://prod"}},
|
|
}
|
|
err := runLogout(&LogoutOptions{Name: "ghost"}, newLogoutFactory(t, cfg, secrets.NewMemStore()))
|
|
require.Error(t, err)
|
|
var typed *cmdutil.Error
|
|
require.ErrorAs(t, err, &typed)
|
|
assert.Equal(t, cmdutil.CodeLocalContextNotFound, typed.Code)
|
|
}
|
|
|
|
func TestLogout_NoCurrentNoFlag(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
cfg := &config.Config{
|
|
Contexts: map[string]config.Context{"prod": {Host: "https://prod"}},
|
|
}
|
|
err := runLogout(&LogoutOptions{}, newLogoutFactory(t, cfg, secrets.NewMemStore()))
|
|
require.Error(t, err)
|
|
var typed *cmdutil.Error
|
|
require.ErrorAs(t, err, &typed)
|
|
assert.Equal(t, cmdutil.CodeInputMissingFlag, typed.Code)
|
|
}
|
|
|
|
// TestLogout_Cobra exercises the cobra layer for the mutually-exclusive
|
|
// --name + --all flag pair.
|
|
func TestLogout_Cobra_FlagsMutuallyExclusive(t *testing.T) {
|
|
isolateConfig(t)
|
|
_, _ = iostreams.SetForTest(t)
|
|
cfg := &config.Config{Contexts: map[string]config.Context{"a": {}}}
|
|
cmd := NewCmdLogout(newLogoutFactory(t, cfg, secrets.NewMemStore()))
|
|
cmd.SetContext(context.Background())
|
|
cmd.SetArgs([]string{"--name", "a", "--all"})
|
|
cmd.SilenceErrors = true
|
|
cmd.SilenceUsage = true
|
|
require.Error(t, cmd.Execute())
|
|
}
|