Files
urapt/cli/commands/auth.go
T
owen 981587e83d Initial release: self-hostable APT repository server and CLI
urapt is a self-hostable APT repository server with a companion CLI for
pushing and managing Debian .deb packages.

Server (urapt-server):
- REST API + APT endpoint, SQLite storage (pure-Go modernc driver, no CGO)
- .deb files stored content-addressed on disk, reference-counted for dedup
- Server-managed RSA-4096 OpenPGP signing key (ProtonMail/go-crypto)
- APT indices (Release/InRelease/Packages[.gz/.xz]) generated on demand
  from the DB, cached in memory, signed with the server key
- Full APT model: repositories -> distributions -> components -> architectures
- Bearer-token auth for REST; HTTP Basic auth for private-repo APT reads
- First registrant becomes admin; repo-scoped permissions
  (read/write/read-write/admin) plus owner and server-admin roles
- Multipart package push with control-field extraction, list/show/delete,
  pool serving, blob ref-count cleanup
- Audit log

CLI (urapt):
- register/login/logout/whoami, token management
- repo/distro/component/arch CRUD, member management
- push/pull/ls/show/rm for packages
- apt-config helper that emits apt setup commands (key, sources.list,
  auth.conf for private repos)

Packaging & docs:
- Dockerfile (multi-stage distroless), docker-compose.yml, sample config
- README quick start, architecture overview, config reference, security notes
- PLAN.md design blueprint, CHANGELOG.md, GPL-3.0 LICENSE
- GitHub Actions CI (test, lint, cross-build for linux/darwin amd64/arm64)
- Makefile release target producing static binaries + tarballs + checksums

Tests cover the data-access layer, auth/permission checks, APT index
generation, .deb parsing, GPG signing, the REST API, and the typed API
client. Verified end-to-end on a Raspberry Pi (arm64) pushing and installing
a real package.
2026-06-28 16:57:34 -05:00

255 lines
5.6 KiB
Go

package commands
import (
"fmt"
"github.com/spf13/cobra"
"urapt/cli/interact"
"urapt/cli/output"
)
func (r *Root) loginCmd() *cobra.Command {
var username, password string
cmd := &cobra.Command{
Use: "login [server]",
Short: "Log in to a urapt server and save an API token",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
server := r.flagServer
if len(args) == 1 {
server = args[0]
}
if server == "" {
return fmt.Errorf("server URL required: pass as an argument or --server")
}
if username == "" {
username = r.flagUser
}
if username == "" {
var err error
username, err = interact.ReadLine("Username: ")
if err != nil {
return err
}
}
pw, err := r.resolvePassword(password)
if err != nil {
return err
}
c, err := r.unauthClient(server)
if err != nil {
return err
}
user, token, err := c.Login(username, pw)
if err != nil {
return err
}
if err := r.saveProfile(server, user.Username, token); err != nil {
return err
}
if r.flagJSON {
output.JSON(user)
} else {
fmt.Printf("Logged in as %s (server: %s)\n", user.Username, server)
}
return nil
},
}
cmd.Flags().StringVarP(&username, "username", "u", "", "username")
cmd.Flags().StringVarP(&password, "password", "p", "", "password (prompts if omitted)")
return cmd
}
func (r *Root) logoutCmd() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Revoke the current API token and clear the saved profile",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := r.client()
if err != nil {
return err
}
if err := c.Logout(); err != nil {
return err
}
if err := r.clearProfile(); err != nil {
return err
}
fmt.Println("Logged out")
return nil
},
}
}
func (r *Root) whoamiCmd() *cobra.Command {
return &cobra.Command{
Use: "whoami",
Short: "Show the currently authenticated user",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := r.client()
if err != nil {
return err
}
user, err := c.Me()
if err != nil {
return err
}
if r.flagJSON {
output.JSON(user)
} else {
fmt.Printf("%s (admin: %v)\n", user.Username, user.IsAdmin)
}
return nil
},
}
}
func (r *Root) registerCmd() *cobra.Command {
var username, password string
cmd := &cobra.Command{
Use: "register [server]",
Short: "Create a new account on a urapt server (first account becomes admin)",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
server := r.flagServer
if len(args) == 1 {
server = args[0]
}
if server == "" {
return fmt.Errorf("server URL required: pass as an argument or --server")
}
if username == "" {
username = r.flagUser
}
if username == "" {
var err error
username, err = interact.ReadLine("Username: ")
if err != nil {
return err
}
}
pw, err := r.resolvePassword(password)
if err != nil {
return err
}
c, err := r.unauthClient(server)
if err != nil {
return err
}
user, token, err := c.Register(username, pw)
if err != nil {
return err
}
if err := r.saveProfile(server, user.Username, token); err != nil {
return err
}
if r.flagJSON {
output.JSON(user)
} else {
fmt.Printf("Registered and logged in as %s (admin: %v)\n", user.Username, user.IsAdmin)
}
return nil
},
}
cmd.Flags().StringVarP(&username, "username", "u", "", "username")
cmd.Flags().StringVarP(&password, "password", "p", "", "password (prompts if omitted)")
return cmd
}
func (r *Root) tokenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "token",
Short: "Manage API tokens",
}
cmd.AddCommand(
r.tokenCreateCmd(),
r.tokenListCmd(),
r.tokenRevokeCmd(),
)
return cmd
}
func (r *Root) tokenCreateCmd() *cobra.Command {
var name string
c := &cobra.Command{
Use: "create",
Short: "Create a new API token",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := r.client()
if err != nil {
return err
}
t, err := client.CreateToken(name)
if err != nil {
return err
}
if r.flagJSON {
output.JSON(t)
} else {
fmt.Printf("Token created (name: %s)\n", t.Name)
fmt.Printf(" %s\n", t.Token)
fmt.Println(" Save this token; it will not be shown again.")
}
return nil
},
}
c.Flags().StringVarP(&name, "name", "n", "default", "token label")
return c
}
func (r *Root) tokenListCmd() *cobra.Command {
c := &cobra.Command{
Use: "list",
Short: "List your API tokens",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := r.client()
if err != nil {
return err
}
tokens, err := client.ListTokens()
if err != nil {
return err
}
if r.flagJSON {
output.JSON(tokens)
return nil
}
if len(tokens) == 0 {
fmt.Println("No tokens.")
return nil
}
fmt.Printf("%-12s %-20s %-10s %s\n", "PREFIX", "NAME", "CREATED", "STATUS")
for _, t := range tokens {
status := "active"
if t.RevokedAt != nil {
status = "revoked"
}
fmt.Printf("%-12s %-20s %-10s %s\n", t.Prefix, t.Name, t.CreatedAt[:10], status)
}
return nil
},
}
return c
}
func (r *Root) tokenRevokeCmd() *cobra.Command {
c := &cobra.Command{
Use: "revoke <id>",
Short: "Revoke an API token by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := r.client()
if err != nil {
return err
}
if err := client.RevokeToken(args[0]); err != nil {
return err
}
fmt.Println("Token revoked")
return nil
},
}
return c
}