fix: include cmd/latha-auth/main.go by anchoring .gitignore

This commit is contained in:
nandi committed 2026-11-17 21:38:31 +00:00
1 parent 82d6862a0e
commit 78160b453f
2 files changed
+210 -1

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
latha-auth /latha-auth
*.test *.test
.DS_Store .DS_Store
+209
View File
@@ -0,0 +1,209 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.site.latha.org/experiments/latha-auth/internal/gitea"
"git.site.latha.org/experiments/latha-auth/internal/oidc"
"git.site.latha.org/experiments/latha-auth/internal/pkce"
)
const version = "0.1.0"
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(2)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
var err error
switch os.Args[1] {
case "gitea":
err = runGitea(ctx, os.Args[2:])
case "k3s":
err = runK3s(ctx, os.Args[2:])
case "oidc":
err = runOIDC(ctx, os.Args[2:])
case "version", "--version", "-v":
fmt.Printf("latha-auth %s\n", version)
return
case "help", "--help", "-h":
printUsage()
return
default:
fmt.Fprintf(os.Stderr, "unknown subcommand %q\n\n", os.Args[1])
printUsage()
os.Exit(2)
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, `latha-auth - Zero-localhost OAuth CLI for remote VMs
Usage:
latha-auth <command> [flags]
Commands:
gitea Authenticate with Gitea (git.site.latha.org) via PKCE + relay
k3s Authenticate with K3s Headlamp (dash.latha.org) via Zitadel Device Code
oidc Authenticate with any Zitadel service (auth.site.latha.org) via Device Code
version Print version
Run 'latha-auth <command> --help' for command-specific flags.
`)
}
func runGitea(ctx context.Context, args []string) error {
defaultClientID := os.Getenv("LATHA_GITEA_CLIENT_ID")
if defaultClientID == "" {
defaultClientID = gitea.DefaultClientID
}
fs := flag.NewFlagSet("gitea", flag.ContinueOnError)
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)")
timeout := fs.Duration("timeout", 5*time.Minute, "Authorization timeout")
noTea := fs.Bool("no-tea", false, "Skip configuring tea/git credentials and print access token")
if err := fs.Parse(args); err != nil {
return err
}
verifier, err := pkce.GenerateVerifier()
if err != nil {
return err
}
state, err := pkce.GenerateState()
if err != nil {
return err
}
challenge := pkce.ChallengeS256(verifier)
redirectURI := gitea.RedirectURI(*relayURL)
authURL := gitea.BuildAuthorizeURL(*giteaURL, *clientID, redirectURI, state, challenge)
fmt.Println("Open this URL in your browser to authorize Gitea:")
fmt.Println()
fmt.Printf(" %s\n\n", authURL)
fmt.Println("Waiting for authorization callback...")
pollCtx, cancel := context.WithTimeout(ctx, *timeout)
defer cancel()
httpClient := &http.Client{Timeout: 15 * time.Second}
code, err := gitea.PollRelay(pollCtx, httpClient, *relayURL, state, 2*time.Second)
if err != nil {
return err
}
tok, err := gitea.ExchangeCode(pollCtx, httpClient, *giteaURL, *clientID, redirectURI, code, verifier)
if err != nil {
return err
}
if *noTea {
fmt.Println(tok.AccessToken)
return nil
}
if err := gitea.ConfigureTeaAndGit(ctx, *teaBin, *loginName, *giteaURL, tok.AccessToken); err != nil {
return err
}
fmt.Printf("Configured tea login %q (%s) and git credentials.\n", *loginName, *giteaURL)
return nil
}
func runK3s(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("k3s", flag.ContinueOnError)
clientID := fs.String("client-id", oidc.DefaultK3sClientID, "Zitadel OAuth2 client ID")
scope := fs.String("scope", oidc.DefaultK3sScope, "OAuth2 scope")
outPath := fs.String("out", "~/.config/latha-auth/k3s-token.json", "Output token JSON path")
authURL := fs.String("auth-url", oidc.DefaultAuthURL, "Zitadel base URL")
timeout := fs.Duration("timeout", 5*time.Minute, "Device authorization timeout")
if err := fs.Parse(args); err != nil {
return err
}
return runDeviceFlow(ctx, *authURL, *clientID, *scope, *outPath, *timeout)
}
func runOIDC(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("oidc", flag.ContinueOnError)
clientID := fs.String("client-id", "", "Zitadel OAuth2 client ID (required)")
scope := fs.String("scope", "openid profile email", "OAuth2 scope")
outPath := fs.String("out", "", "Output token JSON path (prints JSON to stdout if empty)")
authURL := fs.String("auth-url", oidc.DefaultAuthURL, "Zitadel base URL")
timeout := fs.Duration("timeout", 5*time.Minute, "Device authorization timeout")
if err := fs.Parse(args); err != nil {
return err
}
if *clientID == "" {
return fmt.Errorf("--client-id is required")
}
return runDeviceFlow(ctx, *authURL, *clientID, *scope, *outPath, *timeout)
}
func runDeviceFlow(ctx context.Context, authURL, clientID, scope, outPath string, timeout time.Duration) error {
httpClient := &http.Client{Timeout: 15 * time.Second}
da, err := oidc.RequestDeviceCode(ctx, httpClient, authURL, clientID, scope)
if err != nil {
return err
}
if da.VerificationURIComplete != "" {
fmt.Println("Open this URL in your browser to authorize:")
fmt.Println()
fmt.Printf(" %s\n\n", da.VerificationURIComplete)
if da.UserCode != "" {
fmt.Printf("User code: %s\n", da.UserCode)
}
} else {
fmt.Printf("Open %s and enter code: %s\n", da.VerificationURI, da.UserCode)
}
fmt.Println("Waiting for authorization...")
interval := time.Duration(da.Interval) * time.Second
if interval <= 0 {
interval = 5 * time.Second
}
pollCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
tok, err := oidc.PollDeviceToken(pollCtx, httpClient, authURL, clientID, da.DeviceCode, interval)
if err != nil {
return err
}
if outPath == "" {
b, err := json.MarshalIndent(tok, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
if err := oidc.SaveTokenFile(outPath, tok); err != nil {
return err
}
resolved, _ := oidc.ExpandPath(outPath)
fmt.Printf("Saved token to %s (0600).\n", resolved)
return nil
}