fix(auth): resolve a bare GITEA_TOKEN host in completion and the Git helper

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-12 20:31:09 +02:00
1 parent c22e8550da
commit 048bb291fc
4 files changed
+28 -6

No files matched your search

+11
View File
@@ -328,3 +328,14 @@ func TestCompletionInstallHelp(t *testing.T) {
require.NoError(t, Execute(context.Background(), deps, []string{"completion", "bash", "--help"}))
require.Contains(t, out.String(), "$ eval \"$(tea completion bash)\"\n")
}
func TestCompletionTargetAcceptsABareTokenHost(t *testing.T) {
deps, _, _ := testDeps(t)
instance := "http://127.0.0.1:3999"
require.NoError(t, testManager(t, deps).Register(instance, "alice", "file", "token", auth.HostOptions{}, auth.Credential{AccessToken: "stored-token"}))
deps.Getenv = envFunc(map[string]string{"TEA_HOST": "127.0.0.1:3999", "GITEA_TOKEN": "environment-token"})
a := &application{deps: deps}
resolved, err := a.completionTarget(context.Background(), false)
require.NoError(t, err)
require.Equal(t, instance, resolved.Host)
}
+1 -1
View File
@@ -303,7 +303,7 @@ func (a *application) completionTarget(ctx context.Context, requireRepo bool) (t
var signer *auth.HTTPSign
token := a.deps.Getenv("GITEA_TOKEN")
if token != "" {
if instance, err := settings.NormalizeHost(a.tokenInstance()); err != nil || instance != t.Host {
if instance, err := settings.ResolveHost(a.tokenInstance(), t.hosts()); err != nil || instance != t.Host {
return t, errors.New("GITEA_TOKEN belongs to another instance")
}
} else {
+4 -5
View File
@@ -40,6 +40,9 @@ func (a *application) gitCredentialCommand() *cobra.Command {
return err
}
environmentHost := a.tokenInstance()
if resolved, err := settings.ResolveHost(environmentHost, snapshot.Hosts.Hosts); environmentHost != "" && err == nil {
environmentHost = resolved
}
instance, err := auth.MatchGitInstance(request, snapshot.Hosts.Hosts, a.options.host, environmentHost)
if err != nil {
return clierr.Wrap(clierr.Usage, err)
@@ -64,11 +67,7 @@ func (a *application) gitCredentialCommand() *cobra.Command {
}
credential := auth.Credential{}
token := a.deps.Getenv("GITEA_TOKEN")
environmentMatches := false
if environmentHost != "" {
normalized, err := settings.NormalizeHost(environmentHost)
environmentMatches = err == nil && normalized == instance
}
environmentMatches := environmentHost != "" && environmentHost == instance
if token != "" && environmentMatches {
actual, err := services.CurrentAccount(cmd.Context(), a.deps.Client(instance, token, host.Insecure))
if err != nil {
+12
View File
@@ -84,3 +84,15 @@ func TestGitCredentialEnvironmentTokenIsScopedAndNeverPersisted(t *testing.T) {
require.Empty(t, out.String())
}
}
func TestGitCredentialEnvironmentTokenAcceptsABareHost(t *testing.T) {
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Fatalf("unexpected request %s", r.URL.Path) }))
defer server.Close()
deps, out, _ := testDeps(t)
require.NoError(t, testManager(t, deps).Register(server.URL, "alice", "file", "token", auth.HostOptions{}, auth.Credential{AccessToken: "stored-token"}))
host := strings.TrimPrefix(server.URL, "http://")
deps.Getenv = envFunc(map[string]string{"TEA_HOST": host, "GITEA_TOKEN": "environment-token"})
deps.Streams.In = strings.NewReader("protocol=http\nhost=" + host + "\npath=alice/project.git\n\n")
require.NoError(t, Execute(context.Background(), deps, []string{"auth", "git-credential", "get"}))
require.Equal(t, "username=alice\npassword=environment-token\n\n", out.String())
}