99 lines
4.6 KiB
Go
99 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// defaultVerificationMailMinInterval is the shortest time between two
|
|
// verification messages to one address, counting the registration and
|
|
// every resend.
|
|
defaultVerificationMailMinInterval = time.Minute
|
|
// minVerificationMailMinInterval and maxVerificationMailMinInterval bound
|
|
// the setting: a shorter interval is no brake, a longer one is a
|
|
// lock-out that a person who mistyped cannot wait out.
|
|
minVerificationMailMinInterval = time.Second
|
|
maxVerificationMailMinInterval = time.Hour
|
|
// defaultVerificationMailDailyLimit is the number of verification
|
|
// messages to one address per 24 hours.
|
|
defaultVerificationMailDailyLimit = 5
|
|
maxVerificationMailDailyLimit = 50
|
|
// defaultIdentityClientHourlyLimit is the number of registrations and
|
|
// resends per client network address per hour. It is a brake on one
|
|
// source reaching many addresses; the per-address limits above protect
|
|
// one address from many sources.
|
|
defaultIdentityClientHourlyLimit = 30
|
|
maxIdentityClientHourlyLimit = 10000
|
|
// MinIdentityKeySecretBytes is the shortest accepted IDENTITY_KEY_SECRET.
|
|
MinIdentityKeySecretBytes = 32
|
|
// examplePlaceholderPrefix starts the placeholder values of the example
|
|
// environment file; a secret that keeps it is public.
|
|
examplePlaceholderPrefix = "replace-this"
|
|
)
|
|
|
|
// loadIdentity reads the registration settings. IDENTITY_KEY_SECRET is
|
|
// required: without it ValidateIdentity refuses the configuration, unless
|
|
// IDENTITY_ALLOW_EPHEMERAL_KEY is true, which lets a development stand draw a
|
|
// random secret for the life of the process and so forget idempotency records
|
|
// and rate limit counters on every restart.
|
|
//
|
|
// The three limits treat an empty value as unset, as the mail quota does:
|
|
// Compose passes an unset variable as an empty string.
|
|
func (config *Config) loadIdentity(lookup LookupEnv) error {
|
|
config.IdentityKeySecret = strings.TrimSpace(valueOrDefault(lookup, "IDENTITY_KEY_SECRET", ""))
|
|
var err error
|
|
config.IdentityAllowEphemeralKey, err = boolOrDefault(lookup, "IDENTITY_ALLOW_EPHEMERAL_KEY", false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
setOnly := func(key string) (string, bool) {
|
|
value, ok := lookup(key)
|
|
if strings.TrimSpace(value) == "" {
|
|
return "", false
|
|
}
|
|
return value, ok
|
|
}
|
|
config.VerificationMailMinInterval, err = durationOrDefault(setOnly, "VERIFICATION_MAIL_MIN_INTERVAL", defaultVerificationMailMinInterval)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
config.VerificationMailDailyLimit, err = intOrDefault(setOnly, "VERIFICATION_MAIL_DAILY_LIMIT", defaultVerificationMailDailyLimit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
config.IdentityClientHourlyLimit, err = intOrDefault(setOnly, "IDENTITY_CLIENT_HOURLY_LIMIT", defaultIdentityClientHourlyLimit)
|
|
return err
|
|
}
|
|
|
|
// ValidateIdentity checks the registration settings. Errors never include
|
|
// the secret.
|
|
func (config Config) ValidateIdentity() error {
|
|
if config.IdentityKeySecret == "" && !config.IdentityAllowEphemeralKey {
|
|
return errors.New("Config.ValidateIdentity - IDENTITY_KEY_SECRET is required (generate one with: openssl rand -base64 48); " +
|
|
"set IDENTITY_ALLOW_EPHEMERAL_KEY=true only on a development stand")
|
|
}
|
|
if strings.HasPrefix(config.IdentityKeySecret, examplePlaceholderPrefix) {
|
|
return errors.New("Config.ValidateIdentity - IDENTITY_KEY_SECRET is an example placeholder; generate a real one with: openssl rand -base64 48")
|
|
}
|
|
if config.IdentityKeySecret != "" && len(config.IdentityKeySecret) < MinIdentityKeySecretBytes {
|
|
return fmt.Errorf("Config.ValidateIdentity - IDENTITY_KEY_SECRET must have at least %d bytes", MinIdentityKeySecretBytes)
|
|
}
|
|
if config.VerificationMailMinInterval < minVerificationMailMinInterval || config.VerificationMailMinInterval > maxVerificationMailMinInterval {
|
|
return fmt.Errorf("Config.ValidateIdentity - VERIFICATION_MAIL_MIN_INTERVAL must be between %s and %s",
|
|
minVerificationMailMinInterval, maxVerificationMailMinInterval)
|
|
}
|
|
if config.VerificationMailDailyLimit < 1 || config.VerificationMailDailyLimit > maxVerificationMailDailyLimit {
|
|
return fmt.Errorf("Config.ValidateIdentity - VERIFICATION_MAIL_DAILY_LIMIT must be between 1 and %d", maxVerificationMailDailyLimit)
|
|
}
|
|
if config.IdentityClientHourlyLimit < 1 || config.IdentityClientHourlyLimit > maxIdentityClientHourlyLimit {
|
|
return fmt.Errorf("Config.ValidateIdentity - IDENTITY_CLIENT_HOURLY_LIMIT must be between 1 and %d", maxIdentityClientHourlyLimit)
|
|
}
|
|
if config.VerificationMailMinInterval >= 24*time.Hour {
|
|
return errors.New("Config.ValidateIdentity - VERIFICATION_MAIL_MIN_INTERVAL must be shorter than the daily window")
|
|
}
|
|
return nil
|
|
}
|