Add custom token support

This commit is contained in:
2026-09-13 01:01:48 +02:00
parent 1b77cdd165
commit 48229f15a2
12 changed files with 680 additions and 305 deletions
+119 -41
View File
@@ -1,23 +1,25 @@
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"time"
"send/internal/auth"
"send/internal/config"
"send/internal/store"
)
const tokenUsage = `send token - manage upload credentials
Usage:
send token add <name> [options]
send token add <name> [options] generate a token
send token add <name> --token - read a chosen one from stdin
send token list [options]
send token rm <name> [options]
@@ -29,8 +31,13 @@ Options:
`
func tokenCommand(args []string) error {
if len(args) == 0 {
return errors.New("token: expected add, list or rm")
// Asking for help is not a subcommand, and neither is asking for nothing.
if len(args) == 0 || isHelp(args[0]) {
tokenUsageTo(os.Stdout)
if len(args) == 0 {
return errors.New("token: expected add, list or rm")
}
return flag.ErrHelp
}
sub, rest := args[0], args[1:]
@@ -41,20 +48,9 @@ func tokenCommand(args []string) error {
name, rest = rest[0], rest[1:]
}
var (
dataDir, tokensPath string
maxSize, maxExpiry, defExpiry string
vanity, admin bool
)
fs := config.NewSet("send token", config.EnvPrefix)
var opts tokenOptions
fs := opts.register()
fs.SetOutput(os.Stderr)
fs.String(&dataDir, "data", "d", "./data", "DIR", "directory holding the data")
fs.String(&tokensPath, "tokens", "", "", "FILE", "token file location (default <data>/tokens.json)")
fs.String(&maxSize, "max-size", "s", "", "SIZE", "per-upload cap for this token; 'unlimited' to remove it")
fs.String(&maxExpiry, "max-expiry", "e", "", "DURATION", "longest lifetime this token may request; 'never' to remove the cap")
fs.String(&defExpiry, "default-expiry", "", "", "DURATION", "lifetime applied when this token does not ask for one")
fs.Bool(&vanity, "vanity", "", false, "allow this token to claim custom names")
fs.Bool(&admin, "admin", "", false, "allow this token to delete anyone's files")
if err := fs.Parse(rest); err != nil {
if errors.Is(err, flag.ErrHelp) {
@@ -63,25 +59,25 @@ func tokenCommand(args []string) error {
}
return err
}
if tokensPath == "" {
tokensPath = dataDir + "/tokens.json"
if opts.tokensPath == "" {
opts.tokensPath = opts.dataDir + "/tokens.json"
}
// Match the server's permissions for anything this command has to create.
setUmask()
file, err := auth.Load(tokensPath)
file, err := auth.Load(opts.tokensPath)
if err != nil {
return err
}
warnIfUnusedDataDir(dataDir, tokensPath)
warnIfUnusedDataDir(opts.dataDir, opts.tokensPath)
switch sub {
case "add":
if name == "" {
return errors.New("token add: a name is required")
}
return tokenAdd(file, name, maxSize, maxExpiry, defExpiry, vanity, admin)
return tokenAdd(file, name, opts)
case "list":
return tokenList(file)
case "rm", "remove", "delete":
@@ -112,33 +108,94 @@ func warnIfUnusedDataDir(dataDir, tokensPath string) {
dataDir, tokensPath, dataDir)
}
func tokenAdd(file *auth.File, name, maxSize, maxExpiry, defExpiry string, vanity, admin bool) error {
secret, err := store.NewSecret()
// tokenOptions are the flags every token subcommand shares.
type tokenOptions struct {
dataDir string
tokensPath string
maxSize string
maxExpiry string
defExpiry string
chosen string
vanity bool
admin bool
}
func (o *tokenOptions) register() *config.Set {
fs := config.NewSet("send token", config.EnvPrefix)
fs.String(&o.dataDir, "data", "d", "./data", "DIR", "directory holding the data")
fs.String(&o.tokensPath, "tokens", "", "", "FILE", "token file location (default <data>/tokens.json)")
fs.String(&o.maxSize, "max-size", "s", "", "SIZE", "per-upload cap for this token; 'unlimited' to remove it")
fs.String(&o.maxExpiry, "max-expiry", "e", "", "DURATION", "longest lifetime this token may request; 'never' to remove the cap")
fs.String(&o.defExpiry, "default-expiry", "", "", "DURATION", "lifetime applied when this token does not ask for one")
fs.String(&o.chosen, "token", "t", "", "VALUE",
"use this token instead of a generated one; \"-\" reads it from standard input")
fs.Bool(&o.vanity, "vanity", "", false, "allow this token to claim custom names")
fs.Bool(&o.admin, "admin", "", false, "allow this token to delete anyone's files")
return fs
}
// isHelp recognises the spellings people actually type.
func isHelp(arg string) bool {
switch arg {
case "help", "-h", "--help":
return true
}
return false
}
func tokenUsageTo(w io.Writer) {
var opts tokenOptions
opts.register().PrintUsage(w, tokenUsage)
}
func tokenAdd(file *auth.File, name string, opts tokenOptions) error {
chosen := opts.chosen
if chosen == "-" {
read, err := readSecret()
if err != nil {
return err
}
chosen = read
}
var (
t *auth.Token
secret string
err error
)
if chosen != "" {
t, err = auth.NewChosen(name, chosen)
secret = chosen
} else {
t, secret, err = auth.NewGenerated(name)
}
if err != nil {
return err
}
t := &auth.Token{
Name: name,
Hash: auth.HashSecret(secret),
AllowVanity: vanity,
Admin: admin,
Created: time.Now().UTC().Truncate(time.Second),
}
t.AllowVanity = opts.vanity
t.Admin = opts.admin
// Only options actually given are recorded; everything else stays absent
// so it keeps tracking the server's defaults.
if maxSize != "" {
t.MaxSize = &maxSize
if opts.maxSize != "" {
t.MaxSize = &opts.maxSize
}
if maxExpiry != "" {
t.MaxExpiry = &maxExpiry
if opts.maxExpiry != "" {
t.MaxExpiry = &opts.maxExpiry
}
if defExpiry != "" {
t.DefaultExpiry = &defExpiry
if opts.defExpiry != "" {
t.DefaultExpiry = &opts.defExpiry
}
if err := file.Add(t); err != nil {
return err
}
if chosen != "" {
fmt.Printf("Added token %q to %s\n\n", name, file.Path())
fmt.Println("It is stored under a slow key derivation, so a leak of the token")
fmt.Println("file does not hand over the passphrase itself. Guessing it online is")
fmt.Println("rate limited, but a weak choice is still a weak choice.")
return nil
}
fmt.Printf("Added token %q to %s\n\n", name, file.Path())
fmt.Printf(" %s\n\n", secret)
fmt.Println("This is the only time it is shown; only its hash is stored.")
@@ -146,6 +203,20 @@ func tokenAdd(file *auth.File, name, maxSize, maxExpiry, defExpiry string, vanit
return nil
}
// readSecret reads a token from standard input, so it need not appear in a
// shell history or in the process list.
func readSecret() (string, error) {
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
return "", err
}
secret := strings.TrimRight(line, "\r\n")
if secret == "" {
return "", errors.New("no token on standard input")
}
return secret, nil
}
func tokenList(file *auth.File) error {
tokens := file.List()
if len(tokens) == 0 {
@@ -153,10 +224,10 @@ func tokenList(file *auth.File) error {
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tMAX SIZE\tMAX EXPIRY\tDEFAULT\tVANITY\tADMIN\tCREATED")
fmt.Fprintln(w, "NAME\tKIND\tMAX SIZE\tMAX EXPIRY\tDEFAULT\tVANITY\tADMIN\tCREATED")
for _, t := range tokens {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
t.Name,
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
t.Name, kind(t),
inherited(t.MaxSize), inherited(t.MaxExpiry), inherited(t.DefaultExpiry),
yesNo(t.AllowVanity), yesNo(t.Admin),
t.Created.Format("2006-01-02"))
@@ -164,6 +235,13 @@ func tokenList(file *auth.File) error {
return w.Flush()
}
func kind(t *auth.Token) string {
if t.Chosen() {
return "chosen"
}
return "generated"
}
func inherited(s *string) string {
if s == nil {
return "(default)"