fix(auth): print only a token that belongs to the selected account

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-18 17:15:52 +02:00
1 parent 4d6e68ed85
commit 219798732d
3 files changed
+17 -2

No files matched your search

+10
View File
@@ -801,6 +801,16 @@ $ tea auth token --host gitea.com --user alice`,
if instance, err := settings.ResolveHost(a.tokenInstance(), s.Hosts.Hosts); err != nil || instance != t.Host { if instance, err := settings.ResolveHost(a.tokenInstance(), s.Hosts.Hosts); err != nil || instance != t.Host {
token = "" token = ""
} }
if token != "" && t.User != "" {
// Like connect, print the environment token only for the account it authenticates.
actual, err := services.CurrentAccount(cmd.Context(), a.deps.Client(t.Host, token, t.host.Insecure))
if err != nil {
return err
}
if !services.MatchAccount(t.User, actual) {
return clierr.Wrap(clierr.Authentication, errEnvironmentAccount)
}
}
if token == "" { if token == "" {
account, exists := t.host.Users[t.User] account, exists := t.host.Users[t.User]
switch { switch {
+3 -1
View File
@@ -742,7 +742,9 @@ func TestAuthSwitchSelectsInstanceAndAccount(t *testing.T) {
} }
func TestAuthTokenPrintsTheCredential(t *testing.T) { func TestAuthTokenPrintsTheCredential(t *testing.T) {
const instance = "https://example.org" server := newAuthenticatedServer(http.NotFoundHandler())
defer server.Close()
instance := server.URL
deps, out, _ := testDeps(t) deps, out, _ := testDeps(t)
configureAccounts(t, deps, instance, "alice", "alice") configureAccounts(t, deps, instance, "alice", "alice")
require.NoError(t, Execute(context.Background(), deps, []string{"auth", "token"})) require.NoError(t, Execute(context.Background(), deps, []string{"auth", "token"}))
+4 -1
View File
@@ -37,6 +37,9 @@ type target struct {
var errRepositoryRequired = errors.New("repository required: use --repo owner/name") var errRepositoryRequired = errors.New("repository required: use --repo owner/name")
// errEnvironmentAccount must not reveal which account the GITEA_TOKEN belongs to.
var errEnvironmentAccount = errors.New("GITEA_TOKEN does not belong to the selected account; supply its matching --user or remove the environment token to use stored credentials")
func (a *application) resolve(ctx context.Context, requireRepo bool) (target, error) { func (a *application) resolve(ctx context.Context, requireRepo bool) (target, error) {
return a.resolveTarget(ctx, requireRepo, true) return a.resolveTarget(ctx, requireRepo, true)
} }
@@ -185,7 +188,7 @@ func (a *application) connect(ctx context.Context, req targetRequest) (target, e
} }
// The message must not reveal which account the token belongs to. // The message must not reveal which account the token belongs to.
if !services.MatchAccount(t.User, actual) { if !services.MatchAccount(t.User, actual) {
return t, clierr.Wrap(clierr.Authentication, errors.New("GITEA_TOKEN does not belong to the selected account; supply its matching --user or remove the environment token to use stored credentials")) return t, clierr.Wrap(clierr.Authentication, errEnvironmentAccount)
} }
if t.User == "" { if t.User == "" {
t.UserSource = "GITEA_TOKEN authenticated account" t.UserSource = "GITEA_TOKEN authenticated account"