feat: switch gitea login from tea to gt
This commit is contained in:
1 parent
9ebb35860c
commit
a1cfcc1de7
4 files changed
+186
-27
No files matched your search
@@ -4,7 +4,7 @@ Zero-localhost OAuth CLI for remote VMs authenticating against Gitea (`git.site.
|
||||
|
||||
## Commands
|
||||
|
||||
- `latha-auth gitea`: Authenticates with Gitea via PKCE + the `https://deploy.site.latha.org/oauth/callback` relay, configures `tea login` (`latha`), and sets up git credentials.
|
||||
- `latha-auth gitea`: Authenticates with Gitea via PKCE + the `https://deploy.site.latha.org/oauth/callback` relay, logs in with `gt` ([gitea/cli](https://gitea.com/gitea/cli)), and runs `gt auth setup-git` to configure git credentials.
|
||||
- `latha-auth k3s`: Runs Zitadel OAuth 2.0 Device Code flow for K3s Headlamp (`dash.latha.org`, `client_id=400047321930268787`) and saves the token to `~/.config/latha-auth/k3s-token.json` (`0600`).
|
||||
- `latha-auth oidc --client-id <id> [--scope "..."] [--out <path>]`: Generic Zitadel Device Code flow for any `auth.site.latha.org` service.
|
||||
- `latha-auth atproto --identifier <handle|did> [--scope "..."] [--out <path>]`: AT Protocol OAuth (PAR + PKCE + DPoP) as a public client, collecting the code via the same deploy relay. Saves the session — tokens plus the DPoP private key they are bound to — to `~/.config/latha-auth/atproto-session.json` (`0600`). The client_id defaults to the metadata JSON served by gitea-k3s-deployer at `https://deploy.site.latha.org/oauth/atproto-client-metadata.json` (source copy versioned in this repo at `oauth/atproto-client-metadata.json`; regenerate with `latha-auth atproto --print-client-metadata` after scope/redirect changes, or override with `--client-id` / `LATHA_ATPROTO_CLIENT_ID`).
|
||||
|
||||
@@ -86,10 +86,10 @@ func runGitea(ctx context.Context, args []string) error {
|
||||
clientID := fs.String("client-id", defaultClientID, "Gitea OAuth2 client ID (or LATHA_GITEA_CLIENT_ID)")
|
||||
giteaURL := fs.String("gitea-url", gitea.DefaultGiteaURL, "Gitea base URL")
|
||||
relayURL := fs.String("relay-url", gitea.DefaultRelayURL, "OAuth callback relay base URL")
|
||||
loginName := fs.String("name", gitea.DefaultTeaName, "tea login name")
|
||||
teaBin := fs.String("tea-bin", "", "Path to tea binary (optional)")
|
||||
gtBin := fs.String("gt-bin", "", "Path to gt binary (optional)")
|
||||
credentialStore := fs.String("credential-store", "auto", "Credential store for gt: auto, keyring, or file")
|
||||
timeout := fs.Duration("timeout", 5*time.Minute, "Authorization timeout")
|
||||
noTea := fs.Bool("no-tea", false, "Skip configuring tea/git credentials and print access token")
|
||||
noGT := fs.Bool("no-gt", false, "Skip configuring gt/git credentials and print access token")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -125,16 +125,16 @@ func runGitea(ctx context.Context, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if *noTea {
|
||||
if *noGT {
|
||||
fmt.Println(tok.AccessToken)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := gitea.ConfigureTeaAndGit(ctx, *teaBin, *loginName, *giteaURL, tok.AccessToken); err != nil {
|
||||
if err := gitea.ConfigureGTAndGit(ctx, *gtBin, *giteaURL, tok.AccessToken, *credentialStore); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Configured tea login %q (%s) and git credentials.\n", *loginName, *giteaURL)
|
||||
fmt.Printf("Configured gt login (%s) and git credentials.\n", *giteaURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+50
-20
@@ -18,7 +18,6 @@ const (
|
||||
DefaultGiteaURL = "https://git.site.latha.org"
|
||||
DefaultRelayURL = "https://deploy.site.latha.org"
|
||||
DefaultClientID = "98c0bb53-4019-4295-bed8-122ecbf9c31f"
|
||||
DefaultTeaName = "latha"
|
||||
)
|
||||
|
||||
type TokenResponse struct {
|
||||
@@ -166,42 +165,73 @@ func ExchangeCode(ctx context.Context, client *http.Client, giteaURL, clientID,
|
||||
return &tok, nil
|
||||
}
|
||||
|
||||
func FindTeaBinary(override string) (string, error) {
|
||||
// FindGTBinary locates the Gitea CLI (`gt`) executable from an explicit override, PATH, or ~/.local/bin/gt.
|
||||
func FindGTBinary(override string) (string, error) {
|
||||
if override != "" {
|
||||
return override, nil
|
||||
}
|
||||
if p, err := exec.LookPath("tea"); err == nil {
|
||||
if p, err := exec.LookPath("gt"); err == nil {
|
||||
return p, nil
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
candidate := filepath.Join(home, ".local", "bin", "tea")
|
||||
candidate := filepath.Join(home, ".local", "bin", "gt")
|
||||
if info, statErr := os.Stat(candidate); statErr == nil && !info.IsDir() {
|
||||
return candidate, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("tea binary not found in PATH or ~/.local/bin/tea")
|
||||
return "", fmt.Errorf("gt binary not found in PATH or ~/.local/bin/gt (install from https://gitea.com/gitea/cli)")
|
||||
}
|
||||
|
||||
func ConfigureTeaAndGit(ctx context.Context, teaBin, name, giteaURL, token string) error {
|
||||
bin, err := FindTeaBinary(teaBin)
|
||||
// ConfigureGTAndGit logs gt into giteaURL using token and configures Git's credential helper via gt.
|
||||
// credentialStore may be "", "auto", "keyring", or "file".
|
||||
func ConfigureGTAndGit(ctx context.Context, gtBin, giteaURL, token, credentialStore string) error {
|
||||
switch credentialStore {
|
||||
case "", "auto", "keyring", "file":
|
||||
default:
|
||||
return fmt.Errorf("invalid credential store %q (expected auto, keyring, or file)", credentialStore)
|
||||
}
|
||||
|
||||
bin, err := FindGTBinary(gtBin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Remove existing login with the same name if present so we can cleanly update it.
|
||||
delCmd := exec.CommandContext(ctx, bin, "login", "delete", name)
|
||||
_ = delCmd.Run()
|
||||
|
||||
addCmd := exec.CommandContext(ctx, bin,
|
||||
"login", "add",
|
||||
"--name", name,
|
||||
"--url", strings.TrimRight(giteaURL, "/"),
|
||||
"--token", token,
|
||||
"--helper",
|
||||
)
|
||||
out, err := addCmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("tea login add failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
baseURL := strings.TrimRight(giteaURL, "/")
|
||||
login := func(store string) ([]byte, error) {
|
||||
args := []string{"auth", "login", "--host", baseURL, "--with-token", "--yes", "--no-input"}
|
||||
if store != "" {
|
||||
args = append(args, "--credential-store", store)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, bin, args...)
|
||||
cmd.Stdin = strings.NewReader(token + "\n")
|
||||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
if credentialStore == "" || credentialStore == "auto" {
|
||||
out, err := login("")
|
||||
if err != nil {
|
||||
msg := strings.TrimSpace(string(out))
|
||||
if isKeyringUnavailable(msg) {
|
||||
out, err = login("file")
|
||||
msg = strings.TrimSpace(string(out))
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("gt auth login failed: %w: %s", err, msg)
|
||||
}
|
||||
}
|
||||
} else if out, err := login(credentialStore); err != nil {
|
||||
return fmt.Errorf("gt auth login failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
|
||||
setupCmd := exec.CommandContext(ctx, bin, "auth", "setup-git", "--host", baseURL, "--no-input")
|
||||
if out, err := setupCmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("gt auth setup-git failed: %w: %s", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isKeyringUnavailable(msg string) bool {
|
||||
return strings.Contains(msg, "secure credential storage unavailable") ||
|
||||
strings.Contains(msg, "keyring storage unavailable")
|
||||
}
|
||||
@@ -6,6 +6,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -101,3 +104,129 @@ func TestPollRelayAndExchangeCode(t *testing.T) {
|
||||
t.Fatalf("AccessToken = %q, want %q", tok.AccessToken, "tok-999")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindGTBinary(t *testing.T) {
|
||||
if got, err := FindGTBinary("/custom/gt"); err != nil || got != "/custom/gt" {
|
||||
t.Fatalf("override = (%q, %v), want (/custom/gt, nil)", got, err)
|
||||
}
|
||||
|
||||
tmp := t.TempDir()
|
||||
binDir := filepath.Join(tmp, "bin")
|
||||
if err := os.MkdirAll(binDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gtPath := filepath.Join(binDir, "gt")
|
||||
if err := os.WriteFile(gtPath, []byte("#!/bin/sh\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Setenv("PATH", binDir)
|
||||
t.Setenv("HOME", tmp)
|
||||
if got, err := FindGTBinary(""); err != nil || got != gtPath {
|
||||
t.Fatalf("PATH lookup = (%q, %v), want (%q, nil)", got, err, gtPath)
|
||||
}
|
||||
|
||||
t.Setenv("PATH", filepath.Join(tmp, "empty"))
|
||||
localBin := filepath.Join(tmp, ".local", "bin")
|
||||
if err := os.MkdirAll(localBin, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
localGT := filepath.Join(localBin, "gt")
|
||||
if err := os.WriteFile(localGT, []byte("#!/bin/sh\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := FindGTBinary(""); err != nil || got != localGT {
|
||||
t.Fatalf("~/.local/bin fallback = (%q, %v), want (%q, nil)", got, err, localGT)
|
||||
}
|
||||
}
|
||||
|
||||
func writeFakeGT(t *testing.T, logPath, scriptBody string) string {
|
||||
t.Helper()
|
||||
gtPath := filepath.Join(t.TempDir(), "gt")
|
||||
script := "#!/bin/sh\nset -eu\nLOG_FILE=" + logPath + "\n" + scriptBody
|
||||
if err := os.WriteFile(gtPath, []byte(script), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return gtPath
|
||||
}
|
||||
|
||||
func TestConfigureGTAndGit(t *testing.T) {
|
||||
t.Run("default auto succeeds with keyring", func(t *testing.T) {
|
||||
logPath := filepath.Join(t.TempDir(), "calls.log")
|
||||
gtPath := writeFakeGT(t, logPath, `
|
||||
if [ "$1" = "auth" ] && [ "$2" = "login" ]; then
|
||||
token=$(cat)
|
||||
printf "login|%s|%s\n" "$*" "$token" >> "$LOG_FILE"
|
||||
exit 0
|
||||
fi
|
||||
if [ "$1" = "auth" ] && [ "$2" = "setup-git" ]; then
|
||||
printf "setup-git|%s\n" "$*" >> "$LOG_FILE"
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
`)
|
||||
if err := ConfigureGTAndGit(context.Background(), gtPath, "https://git.site.latha.org/", "secret-tok", "auto"); err != nil {
|
||||
t.Fatalf("ConfigureGTAndGit: %v", err)
|
||||
}
|
||||
got, err := os.ReadFile(logPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "login|auth login --host https://git.site.latha.org --with-token --yes --no-input|secret-tok\n" +
|
||||
"setup-git|auth setup-git --host https://git.site.latha.org --no-input\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("calls:\n%s\nwant:\n%s", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("auto falls back to file store when keyring unavailable", func(t *testing.T) {
|
||||
logPath := filepath.Join(t.TempDir(), "calls.log")
|
||||
gtPath := writeFakeGT(t, logPath, `
|
||||
if [ "$1" = "auth" ] && [ "$2" = "login" ]; then
|
||||
token=$(cat)
|
||||
printf "login|%s|%s\n" "$*" "$token" >> "$LOG_FILE"
|
||||
case "$*" in
|
||||
*"--credential-store file"*) exit 0 ;;
|
||||
*) echo "error: secure credential storage unavailable; use --credential-store file" >&2; exit 1 ;;
|
||||
esac
|
||||
fi
|
||||
if [ "$1" = "auth" ] && [ "$2" = "setup-git" ]; then
|
||||
printf "setup-git|%s\n" "$*" >> "$LOG_FILE"
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
`)
|
||||
if err := ConfigureGTAndGit(context.Background(), gtPath, "https://git.site.latha.org", "secret-tok", ""); err != nil {
|
||||
t.Fatalf("ConfigureGTAndGit fallback: %v", err)
|
||||
}
|
||||
got, err := os.ReadFile(logPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "login|auth login --host https://git.site.latha.org --with-token --yes --no-input|secret-tok\n" +
|
||||
"login|auth login --host https://git.site.latha.org --with-token --yes --no-input --credential-store file|secret-tok\n" +
|
||||
"setup-git|auth setup-git --host https://git.site.latha.org --no-input\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("calls:\n%s\nwant:\n%s", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("explicit keyring does not fall back to file", func(t *testing.T) {
|
||||
logPath := filepath.Join(t.TempDir(), "calls.log")
|
||||
gtPath := writeFakeGT(t, logPath, `
|
||||
echo "error: secure credential storage unavailable" >&2
|
||||
exit 1
|
||||
`)
|
||||
err := ConfigureGTAndGit(context.Background(), gtPath, "https://git.site.latha.org", "secret-tok", "keyring")
|
||||
if err == nil || !strings.Contains(err.Error(), "gt auth login failed") {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid credential store", func(t *testing.T) {
|
||||
err := ConfigureGTAndGit(context.Background(), "/nonexistent/gt", "https://git.site.latha.org", "secret-tok", "bogus")
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid credential store") {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user