mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
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:
4 files changed
+28
-6
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
Reference in new issue
Block a user