2022-07-24 00:13:56 +02:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
2022-07-24 22:02:55 +02:00
|
|
|
"fmt"
|
2022-07-24 00:13:56 +02:00
|
|
|
"os"
|
2022-07-24 22:02:55 +02:00
|
|
|
"strings"
|
|
|
|
|
2022-08-22 18:05:53 +02:00
|
|
|
"lbryio/wallet-sync-server/auth"
|
2022-07-24 00:13:56 +02:00
|
|
|
)
|
|
|
|
|
2022-07-29 19:37:41 +02:00
|
|
|
// NOTE for users: If you have weird characters in your email address, please
|
|
|
|
// remember to properly escape it as necessary when putting it in an
|
|
|
|
// environmental variable, lest you run commands you didn't mean to run.
|
|
|
|
//
|
|
|
|
// We'll replace this with a config file later.
|
2022-07-24 22:02:55 +02:00
|
|
|
const whitelistKey = "ACCOUNT_WHITELIST"
|
|
|
|
const verificationModeKey = "ACCOUNT_VERIFICATION_MODE"
|
2022-08-01 17:50:16 +02:00
|
|
|
const mailgunIsDomainEUKey = "MAILGUN_SENDING_DOMAIN_IS_EU"
|
2022-08-01 01:18:04 +02:00
|
|
|
const mailgunPrivateAPIKeyKey = "MAILGUN_PRIVATE_API_KEY"
|
2022-07-24 22:02:55 +02:00
|
|
|
|
2022-08-01 17:50:16 +02:00
|
|
|
// for the "from" address
|
|
|
|
const mailgunSendingDomainKey = "MAILGUN_SENDING_DOMAIN"
|
|
|
|
|
|
|
|
// for links in the emails
|
|
|
|
const mailgunServerDomainKey = "MAILGUN_SERVER_DOMAIN"
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
type AccountVerificationMode string
|
2022-07-24 22:02:55 +02:00
|
|
|
|
|
|
|
// Everyone can make an account. Only use for dev purposes.
|
2022-07-25 23:46:41 +02:00
|
|
|
const AccountVerificationModeAllowAll = AccountVerificationMode("AllowAll")
|
2022-07-24 22:02:55 +02:00
|
|
|
|
|
|
|
// Verify accounts via email. Good for big open servers.
|
2022-07-25 23:46:41 +02:00
|
|
|
const AccountVerificationModeEmailVerify = AccountVerificationMode("EmailVerify")
|
2022-07-24 22:02:55 +02:00
|
|
|
|
|
|
|
// Specific email accounts are automatically verified. Good for small
|
|
|
|
// self-hosting users.
|
2022-07-25 23:46:41 +02:00
|
|
|
const AccountVerificationModeWhitelist = AccountVerificationMode("Whitelist")
|
2022-07-24 22:02:55 +02:00
|
|
|
|
2022-07-24 00:13:56 +02:00
|
|
|
// For test stubs
|
|
|
|
type EnvInterface interface {
|
|
|
|
Getenv(key string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Env struct{}
|
|
|
|
|
|
|
|
func (e *Env) Getenv(key string) string {
|
|
|
|
return os.Getenv(key)
|
|
|
|
}
|
2022-07-24 22:02:55 +02:00
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
func GetAccountVerificationMode(e EnvInterface) (AccountVerificationMode, error) {
|
2022-07-24 22:02:55 +02:00
|
|
|
return getAccountVerificationMode(e.Getenv(verificationModeKey))
|
|
|
|
}
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
func GetAccountWhitelist(e EnvInterface, mode AccountVerificationMode) (emails []auth.Email, err error) {
|
2022-07-24 22:02:55 +02:00
|
|
|
return getAccountWhitelist(e.Getenv(whitelistKey), mode)
|
|
|
|
}
|
|
|
|
|
2022-08-01 17:50:16 +02:00
|
|
|
func GetMailgunConfigs(e EnvInterface, mode AccountVerificationMode) (sendingDomain string, serverDomain string, isDomainEU bool, privateAPIKey string, err error) {
|
|
|
|
return getMailgunConfigs(e.Getenv(mailgunSendingDomainKey), e.Getenv(mailgunServerDomainKey), e.Getenv(mailgunIsDomainEUKey), e.Getenv(mailgunPrivateAPIKeyKey), mode)
|
2022-08-01 01:18:04 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 22:02:55 +02:00
|
|
|
// Factor out the guts of the functions so we can test them by just passing in
|
|
|
|
// the env vars
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
func getAccountVerificationMode(modeStr string) (AccountVerificationMode, error) {
|
|
|
|
mode := AccountVerificationMode(modeStr)
|
2022-07-24 22:02:55 +02:00
|
|
|
switch mode {
|
|
|
|
case "":
|
|
|
|
// Whitelist is the least dangerous mode. If you forget to set any env
|
|
|
|
// vars, it effectively disables all account creation.
|
2022-07-25 23:46:41 +02:00
|
|
|
return AccountVerificationModeWhitelist, nil
|
|
|
|
case AccountVerificationModeAllowAll:
|
|
|
|
case AccountVerificationModeEmailVerify:
|
|
|
|
case AccountVerificationModeWhitelist:
|
2022-07-24 22:02:55 +02:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Invalid account verification mode in %s: %s", verificationModeKey, mode)
|
|
|
|
}
|
|
|
|
return mode, nil
|
|
|
|
}
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
func getAccountWhitelist(whitelist string, mode AccountVerificationMode) (emails []auth.Email, err error) {
|
2022-07-24 22:02:55 +02:00
|
|
|
if whitelist == "" {
|
|
|
|
return []auth.Email{}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-25 23:46:41 +02:00
|
|
|
if mode != AccountVerificationModeWhitelist {
|
2022-08-01 01:18:04 +02:00
|
|
|
return nil, fmt.Errorf("Do not specify %s in env if %s is not %s",
|
|
|
|
whitelistKey,
|
|
|
|
verificationModeKey,
|
|
|
|
AccountVerificationModeWhitelist,
|
|
|
|
)
|
2022-07-24 22:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rawEmails := strings.Split(whitelist, ",")
|
|
|
|
for _, rawEmail := range rawEmails {
|
|
|
|
// Give them a specific error here to let them know not to add spaces. It
|
|
|
|
// could be confusing otherwise to figure out what's invalid.
|
|
|
|
if strings.TrimSpace(rawEmail) != rawEmail {
|
|
|
|
return nil, fmt.Errorf("Emails in %s should be comma separated with no spaces.", whitelistKey)
|
|
|
|
}
|
|
|
|
email := auth.Email(rawEmail)
|
|
|
|
if !email.Validate() {
|
|
|
|
return nil, fmt.Errorf("Invalid email in %s: %s", whitelistKey, email)
|
|
|
|
}
|
|
|
|
emails = append(emails, email)
|
|
|
|
}
|
|
|
|
return emails, nil
|
|
|
|
}
|
2022-08-01 01:18:04 +02:00
|
|
|
|
2022-08-01 17:50:16 +02:00
|
|
|
func getMailgunConfigs(sendingDomain string, serverDomain string, isDomainEUStr string, privateAPIKey string, mode AccountVerificationMode) (string, string, bool, string, error) {
|
|
|
|
if mode != AccountVerificationModeEmailVerify && (sendingDomain != "" || serverDomain != "" || isDomainEUStr != "" || privateAPIKey != "") {
|
|
|
|
return "", "", false, "", fmt.Errorf("Do not specify %s, %s, %s or %s in env if %s is not %s",
|
|
|
|
mailgunSendingDomainKey,
|
|
|
|
mailgunServerDomainKey,
|
|
|
|
mailgunIsDomainEUKey,
|
2022-08-01 01:18:04 +02:00
|
|
|
mailgunPrivateAPIKeyKey,
|
|
|
|
verificationModeKey,
|
|
|
|
AccountVerificationModeEmailVerify,
|
|
|
|
)
|
|
|
|
}
|
2022-08-01 17:50:16 +02:00
|
|
|
if mode == AccountVerificationModeEmailVerify && (sendingDomain == "" || serverDomain == "" || privateAPIKey == "") {
|
|
|
|
return "", "", false, "", fmt.Errorf("Specify %s, %s and %s in env if %s is %s",
|
|
|
|
mailgunSendingDomainKey,
|
|
|
|
mailgunServerDomainKey,
|
2022-08-01 01:18:04 +02:00
|
|
|
mailgunPrivateAPIKeyKey,
|
|
|
|
verificationModeKey,
|
|
|
|
AccountVerificationModeEmailVerify,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-01 17:50:16 +02:00
|
|
|
if isDomainEUStr != "true" && isDomainEUStr != "false" && isDomainEUStr != "" {
|
|
|
|
return "", "", false, "", fmt.Errorf("%s must be 'true' or 'false'", mailgunIsDomainEUKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendingDomain, serverDomain, isDomainEUStr == "true", privateAPIKey, nil
|
2022-08-01 01:18:04 +02:00
|
|
|
}
|