env: rename things, export type

This commit is contained in:
Daniel Krol 2022-07-25 17:46:41 -04:00
parent f792ba5846
commit 832778ffd1
2 changed files with 24 additions and 26 deletions

30
env/env.go vendored
View file

@ -11,19 +11,17 @@ import (
const whitelistKey = "ACCOUNT_WHITELIST"
const verificationModeKey = "ACCOUNT_VERIFICATION_MODE"
// Not exported, so that GetAccountVerificationMode is the only way to get one
// of these values from outside of this package.
type accountVerificationMode string
type AccountVerificationMode string
// Everyone can make an account. Only use for dev purposes.
const ModeAllowAll = accountVerificationMode("AllowAll")
const AccountVerificationModeAllowAll = AccountVerificationMode("AllowAll")
// Verify accounts via email. Good for big open servers.
const ModeEmailVerify = accountVerificationMode("EmailVerify")
const AccountVerificationModeEmailVerify = AccountVerificationMode("EmailVerify")
// Specific email accounts are automatically verified. Good for small
// self-hosting users.
const ModeWhitelist = accountVerificationMode("Whitelist")
const AccountVerificationModeWhitelist = AccountVerificationMode("Whitelist")
// For test stubs
type EnvInterface interface {
@ -36,39 +34,39 @@ func (e *Env) Getenv(key string) string {
return os.Getenv(key)
}
func GetAccountVerificationMode(e EnvInterface) (accountVerificationMode, error) {
func GetAccountVerificationMode(e EnvInterface) (AccountVerificationMode, error) {
return getAccountVerificationMode(e.Getenv(verificationModeKey))
}
func GetAccountWhitelist(e EnvInterface, mode accountVerificationMode) (emails []auth.Email, err error) {
func GetAccountWhitelist(e EnvInterface, mode AccountVerificationMode) (emails []auth.Email, err error) {
return getAccountWhitelist(e.Getenv(whitelistKey), mode)
}
// Factor out the guts of the functions so we can test them by just passing in
// the env vars
func getAccountVerificationMode(modeStr string) (accountVerificationMode, error) {
mode := accountVerificationMode(modeStr)
func getAccountVerificationMode(modeStr string) (AccountVerificationMode, error) {
mode := AccountVerificationMode(modeStr)
switch mode {
case "":
// Whitelist is the least dangerous mode. If you forget to set any env
// vars, it effectively disables all account creation.
return ModeWhitelist, nil
case ModeAllowAll:
case ModeEmailVerify:
case ModeWhitelist:
return AccountVerificationModeWhitelist, nil
case AccountVerificationModeAllowAll:
case AccountVerificationModeEmailVerify:
case AccountVerificationModeWhitelist:
default:
return "", fmt.Errorf("Invalid account verification mode in %s: %s", verificationModeKey, mode)
}
return mode, nil
}
func getAccountWhitelist(whitelist string, mode accountVerificationMode) (emails []auth.Email, err error) {
func getAccountWhitelist(whitelist string, mode AccountVerificationMode) (emails []auth.Email, err error) {
if whitelist == "" {
return []auth.Email{}, nil
}
if mode != ModeWhitelist {
if mode != AccountVerificationModeWhitelist {
return nil, fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist")
}

20
env/env_test.go vendored
View file

@ -13,32 +13,32 @@ func TestAccountVerificationMode(t *testing.T) {
name string
modeStr string
expectedMode accountVerificationMode
expectedMode AccountVerificationMode
expectErr bool
}{
{
name: "allow all",
modeStr: "AllowAll",
expectedMode: ModeAllowAll,
expectedMode: AccountVerificationModeAllowAll,
},
{
name: "email verify",
modeStr: "EmailVerify",
expectedMode: ModeEmailVerify,
expectedMode: AccountVerificationModeEmailVerify,
},
{
name: "whitelist",
modeStr: "Whitelist",
expectedMode: ModeWhitelist,
expectedMode: AccountVerificationModeWhitelist,
},
{
name: "blank",
modeStr: "",
expectedMode: ModeWhitelist,
expectedMode: AccountVerificationModeWhitelist,
},
{
name: "invalid",
@ -70,33 +70,33 @@ func TestAccountWhitelist(t *testing.T) {
whitelist string
expectedEmails []auth.Email
expectedErr error
mode accountVerificationMode
mode AccountVerificationMode
}{
{
name: "empty",
mode: ModeWhitelist,
mode: AccountVerificationModeWhitelist,
whitelist: "",
expectedEmails: []auth.Email{},
},
{
name: "invalid mode",
mode: ModeEmailVerify,
mode: AccountVerificationModeEmailVerify,
whitelist: "test1@example.com,test2@example.com",
expectedErr: fmt.Errorf("Do not specify ACCOUNT_WHITELIST in env if ACCOUNT_VERIFICATION_MODE is not Whitelist"),
},
{
name: "spaces in email",
mode: ModeWhitelist,
mode: AccountVerificationModeWhitelist,
whitelist: "test1@example.com ,test2@example.com",
expectedErr: fmt.Errorf("Emails in ACCOUNT_WHITELIST should be comma separated with no spaces."),
},
{
name: "invalid email",
mode: ModeWhitelist,
mode: AccountVerificationModeWhitelist,
whitelist: "test1@example.com,test2-example.com",
expectedErr: fmt.Errorf("Invalid email in ACCOUNT_WHITELIST: test2-example.com"),
},