Repository: supabase/gotrue
Pull request #145 (2021-07-19 11:53:22)

Refactor: rename env vars
## What kind of change does this PR introduce?
Refactor EXTERNAL_EMAIL_DISABLED & EXTERNAL_PHONE_DISABLED config vars to EXTERNAL_EMAIL_ENABLED and EXTERNAL_PHONE_ENABLED.





Patch:
diff --git a/README.md b/README.md
index 8b65dbd8..adf556f7 100644
--- a/README.md
+++ b/README.md
@@ -34,10 +34,14 @@ the payload values can be trusted.
 
 When signup is disabled the only way to create new users is through invites. Defaults to `false`, all signups enabled.
 
-`GOTRUE_EXTERNAL_EMAIL_DISABLED` - `bool`
+`GOTRUE_EXTERNAL_EMAIL_ENABLED` - `bool`
 
 Use this to disable email signups (users can still use external oauth providers to sign up / sign in)
 
+`GOTRUE_EXTERNAL_PHONE_ENABLED` - `bool`
+
+Use this to disable phone signups (users can still use external oauth providers to sign up / sign in)
+
 `GOTRUE_RATE_LIMIT_HEADER` - `string`
 
 Header on which to rate limit the `/token` endpoint.
@@ -172,12 +176,6 @@ The default group to assign all new users to.
 
 ### External Authentication Providers
 
-`EXTERNAL_EMAIL_DISABLED` - `bool`
-If set to true, disables email signup.
-
-`EXTERNAL_PHONE_DISABLED` - `bool`
-If set to true, disables phone signup.
-
 We support `apple`, `azure`, `bitbucket`, `discord`, `facebook`, `github`, `gitlab`, `google`, `twitch` and `twitter` for external authentication.
 
 Use the names as the keys underneath `external` to configure each separately.
diff --git a/api/magic_link.go b/api/magic_link.go
index f260913f..93691b58 100644
--- a/api/magic_link.go
+++ b/api/magic_link.go
@@ -22,7 +22,7 @@ func (a *API) MagicLink(w http.ResponseWriter, r *http.Request) error {
 	ctx := r.Context()
 	config := a.getConfig(ctx)
 
-	if config.External.Email.Disabled {
+	if !config.External.Email.Enabled {
 		return badRequestError("Unsupported email provider")
 	}
 
diff --git a/api/middleware.go b/api/middleware.go
index 1b585d76..bd895da3 100644
--- a/api/middleware.go
+++ b/api/middleware.go
@@ -170,7 +170,7 @@ func (a *API) requireEmailProvider(w http.ResponseWriter, req *http.Request) (co
 	ctx := req.Context()
 	config := a.getConfig(ctx)
 
-	if config.External.Email.Disabled {
+	if !config.External.Email.Enabled {
 		return nil, badRequestError("Unsupported email provider")
 	}
 
diff --git a/api/otp.go b/api/otp.go
index 5896ca9a..d286755c 100644
--- a/api/otp.go
+++ b/api/otp.go
@@ -47,7 +47,7 @@ func (a *API) SmsOtp(w http.ResponseWriter, r *http.Request) error {
 	ctx := r.Context()
 	config := a.getConfig(ctx)
 
-	if config.External.Phone.Disabled {
+	if !config.External.Phone.Enabled {
 		return badRequestError("Unsupported phone provider")
 	}
 
diff --git a/api/settings.go b/api/settings.go
index 9275a525..a407e52d 100644
--- a/api/settings.go
+++ b/api/settings.go
@@ -46,8 +46,8 @@ func (a *API) Settings(w http.ResponseWriter, r *http.Request) error {
 			Facebook:  config.External.Facebook.Enabled,
 			Twitch:    config.External.Twitch.Enabled,
 			Twitter:   config.External.Twitter.Enabled,
-			Email:     !config.External.Email.Disabled,
-			Phone:     !config.External.Phone.Disabled,
+			Email:     config.External.Email.Enabled,
+			Phone:     config.External.Phone.Enabled,
 			SAML:      config.External.Saml.Enabled,
 		},
 		ExternalLabels: ProviderLabels{
diff --git a/api/signup.go b/api/signup.go
index bfe46d10..3bfbcdb8 100644
--- a/api/signup.go
+++ b/api/signup.go
@@ -61,7 +61,7 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
 
 	switch params.Provider {
 	case "email":
-		if config.External.Email.Disabled {
+		if !config.External.Email.Enabled {
 			return badRequestError("Unsupported email provider")
 		}
 		if err := a.validateEmail(ctx, params.Email); err != nil {
@@ -69,7 +69,7 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
 		}
 		user, err = models.FindUserByEmailAndAudience(a.db, instanceID, params.Email, params.Aud)
 	case "phone":
-		if config.External.Phone.Disabled {
+		if !config.External.Phone.Enabled {
 			return badRequestError("Unsupported phone provider")
 		}
 		params.Phone = a.formatPhoneNumber(params.Phone)
diff --git a/conf/configuration.go b/conf/configuration.go
index 0bce993e..09438b6d 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -21,7 +21,7 @@ type OAuthProviderConfiguration struct {
 }
 
 type EmailProviderConfiguration struct {
-	Disabled bool `json:"disabled"`
+	Enabled bool `json:"enabled" default:"true"`
 }
 
 type SamlProviderConfiguration struct {
@@ -113,7 +113,7 @@ type MailerConfiguration struct {
 }
 
 type PhoneProviderConfiguration struct {
-	Disabled bool `json:"disabled"`
+	Enabled bool `json:"enabled"`
 }
 
 type SmsProviderConfiguration struct {


Test patch:
diff --git a/api/admin_test.go b/api/admin_test.go
index 0fd81481..6ad02cd6 100644
--- a/api/admin_test.go
+++ b/api/admin_test.go
@@ -44,7 +44,7 @@ func TestAdmin(t *testing.T) {
 
 func (ts *AdminTestSuite) SetupTest() {
 	models.TruncateAll(ts.API.db)
-	ts.Config.External.Email.Disabled = false
+	ts.Config.External.Email.Enabled = true
 	ts.token = ts.makeSuperAdmin("test@example.com")
 }
 
@@ -447,7 +447,7 @@ func (ts *AdminTestSuite) TestAdminUserCreateWithDisabledEmailLogin() {
 
 	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ts.token))
 
-	ts.Config.External.Email.Disabled = true
+	ts.Config.External.Email.Enabled = false
 
 	ts.API.handler.ServeHTTP(w, req)
 	require.Equal(ts.T(), http.StatusBadRequest, w.Code)
diff --git a/api/api_test.go b/api/api_test.go
index 471db2db..a5522f60 100644
--- a/api/api_test.go
+++ b/api/api_test.go
@@ -6,11 +6,11 @@ import (
 	"testing"
 	"time"
 
+	"github.com/gofrs/uuid"
 	"github.com/netlify/gotrue/conf"
 	"github.com/netlify/gotrue/models"
 	"github.com/netlify/gotrue/storage"
 	"github.com/netlify/gotrue/storage/test"
-	"github.com/gofrs/uuid"
 	"github.com/stretchr/testify/require"
 )
 
@@ -96,7 +96,7 @@ func TestEmailEnabledByDefault(t *testing.T) {
 	api, _, err := setupAPIForTest()
 	require.NoError(t, err)
 
-	require.False(t, api.config.External.Email.Disabled)
+	require.True(t, api.config.External.Email.Enabled)
 }
 
 const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
diff --git a/api/instance_test.go b/api/instance_test.go
index 685ad9c8..17d720d2 100644
--- a/api/instance_test.go
+++ b/api/instance_test.go
@@ -144,7 +144,7 @@ func (ts *InstanceTestSuite) TestUpdate_DisableEmail() {
 		BaseConfig: &conf.Configuration{
 			External: conf.ProviderConfiguration{
 				Email: conf.EmailProviderConfiguration{
-					Disabled: false,
+					Enabled: true,
 				},
 			},
 		},
@@ -156,7 +156,7 @@ func (ts *InstanceTestSuite) TestUpdate_DisableEmail() {
 		"config": &conf.Configuration{
 			External: conf.ProviderConfiguration{
 				Email: conf.EmailProviderConfiguration{
-					Disabled: true,
+					Enabled: false,
 				},
 			},
 		},
@@ -172,7 +172,7 @@ func (ts *InstanceTestSuite) TestUpdate_DisableEmail() {
 
 	i, err := models.GetInstanceByUUID(ts.API.db, testUUID)
 	require.NoError(ts.T(), err)
-	require.True(ts.T(), i.BaseConfig.External.Email.Disabled)
+	require.False(ts.T(), i.BaseConfig.External.Email.Enabled)
 }
 
 func (ts *InstanceTestSuite) TestUpdate_PreserveSMTPConfig() {
diff --git a/api/settings_test.go b/api/settings_test.go
index 940117e7..663d4feb 100644
--- a/api/settings_test.go
+++ b/api/settings_test.go
@@ -38,7 +38,7 @@ func TestSettings_EmailDisabled(t *testing.T) {
 	api, config, instanceID, err := setupAPIForTestForInstance()
 	require.NoError(t, err)
 
-	config.External.Email.Disabled = true
+	config.External.Email.Enabled = false
 
 	// Setup request
 	req := httptest.NewRequest(http.MethodGet, "http://localhost/settings", nil)