Repository: supabase/gotrue
Pull request #640 (2022-08-28 07:34:03)

chore: removes stray todo comment
Removes stray TODO comment

J0: @kangmingtay sounds good -- let me update the PR to remove the todo then -- think the other tasks have been addressed by other PRs

Patch:
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
deleted file mode 100644
index d157515b..00000000
--- a/.github/CODEOWNERS
+++ /dev/null
@@ -1,1 +0,0 @@
-*   @netlify/opensource @netlify/backend
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d918fe07..2d08ff59 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -4,7 +4,9 @@ on:
   workflow_run:
     workflows: [Test]
     types: [completed]
-    branches: [master]
+    branches:
+      - master
+      - mfa
 
 jobs:
   release:
diff --git a/.releaserc b/.releaserc
index 32f0e451..f75b8204 100644
--- a/.releaserc
+++ b/.releaserc
@@ -1,6 +1,10 @@
 {
   "branches": [
-    "master"
+    "master",
+    {
+        "name: "mfa",
+        "prerelease": true
+    }
   ],
   "plugins": [
     "@semantic-release/commit-analyzer",
diff --git a/Makefile b/Makefile
index 68e6a061..48c885f0 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ build: deps ## Build the binary.
 dev-deps: deps ## Install developer dependencies
 	@go install github.com/gobuffalo/pop/soda@latest
 	@go install github.com/securego/gosec/v2/cmd/gosec@latest
+	@go install honnef.co/go/tools/cmd/staticcheck@latest
 
 deps: ## Install dependencies.
 	@go mod download
@@ -33,7 +34,17 @@ vet: # Vet the code
 	go vet $(CHECK_FILES)
 
 sec: # Check for security vulnerabilities
-	gosec -tests -exclude=G104 $(CHECK_FILES)
+	gosec -quiet $(CHECK_FILES)
+	gosec -quiet -tests -exclude=G104 $(CHECK_FILES)
+
+unused: # Look for unused code
+	@echo "Unused code:"
+	staticcheck -checks U1000 $(CHECK_FILES)
+	
+	@echo
+	
+	@echo "Code used only in _test.go (do move it in those files):"
+	staticcheck -checks U1000 -tests=false $(CHECK_FILES)
 
 dev: ## Run the development containers
 	docker-compose -f $(DEV_DOCKER_COMPOSE) up
diff --git a/api/api.go b/api/api.go
index ad9cce9f..626e34f3 100644
--- a/api/api.go
+++ b/api/api.go
@@ -2,6 +2,7 @@ package api
 
 import (
 	"context"
+	"errors"
 	"net/http"
 	"os"
 	"os/signal"
@@ -51,7 +52,10 @@ func (a *API) ListenAndServe(hostAndPort string) {
 		waitForTermination(log, done)
 		ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
 		defer cancel()
-		server.Shutdown(ctx)
+		err := server.Shutdown(ctx)
+		if err != nil && !errors.Is(err, context.Canceled) && errors.Is(err, context.DeadlineExceeded) {
+			log.WithError(err).Error("Shutdown failed")
+		}
 	}()
 
 	if err := server.ListenAndServe(); err != http.ErrServerClosed {
@@ -85,10 +89,6 @@ func (a *API) deprecationNotices(ctx context.Context) {
 		log.Warn("DEPRECATION NOTICE: GOTRUE_JWT_ADMIN_GROUP_NAME not supported by Supabase's GoTrue, will be removed soon")
 	}
 
-	if len(config.JWT.AdminRoles) > 0 {
-		log.Warn("DEPRECATION NOTICE: GOTRUE_JWT_ADMIN_ROLES not supported by Supabase's GoTrue, will be removed soon")
-	}
-
 	if config.JWT.DefaultGroupName != "" {
 		log.Warn("DEPRECATION NOTICE: GOTRUE_JWT_DEFAULT_GROUP_NAME not supported by Supabase's GoTrue, will be removed soon")
 	}
diff --git a/api/context.go b/api/context.go
index 1543b5ba..394e701e 100644
--- a/api/context.go
+++ b/api/context.go
@@ -99,16 +99,6 @@ func withSignature(ctx context.Context, id string) context.Context {
 	return context.WithValue(ctx, signatureKey, id)
 }
 
-// getSignature reads the request ID from the context.
-func getSignature(ctx context.Context) string {
-	obj := ctx.Value(signatureKey)
-	if obj == nil {
-		return ""
-	}
-
-	return obj.(string)
-}
-
 func withInviteToken(ctx context.Context, token string) context.Context {
 	return context.WithValue(ctx, inviteTokenKey, token)
 }
@@ -150,11 +140,6 @@ func getExternalReferrer(ctx context.Context) string {
 	return obj.(string)
 }
 
-// withFunctionHooks adds the provided function hooks to the context.
-func withFunctionHooks(ctx context.Context, hooks map[string][]string) context.Context {
-	return context.WithValue(ctx, functionHooksKey, hooks)
-}
-
 // getFunctionHooks reads the request ID from the context.
 func getFunctionHooks(ctx context.Context) map[string][]string {
 	obj := ctx.Value(functionHooksKey)
diff --git a/api/errors.go b/api/errors.go
index e3e619c9..ed5f24f5 100644
--- a/api/errors.go
+++ b/api/errors.go
@@ -98,10 +98,6 @@ func notFoundError(fmtString string, args ...interface{}) *HTTPError {
 	return httpError(http.StatusNotFound, fmtString, args...)
 }
 
-func acceptedTokenError(fmtString string, args ...interface{}) *HTTPError {
-	return httpError(http.StatusAccepted, fmtString, args...)
-}
-
 func expiredTokenError(fmtString string, args ...interface{}) *HTTPError {
 	return httpError(http.StatusUnauthorized, fmtString, args...)
 }
diff --git a/api/helpers.go b/api/helpers.go
index 55324f45..aaa5ca13 100644
--- a/api/helpers.go
+++ b/api/helpers.go
@@ -176,24 +176,6 @@ func isPrivateIP(ip net.IP) bool {
 	return false
 }
 
-func removeLocalhostFromPrivateIPBlock() *net.IPNet {
-	_, localhost, _ := net.ParseCIDR("127.0.0.0/8")
-
-	var localhostIndex int
-	for i := 0; i < len(privateIPBlocks); i++ {
-		if privateIPBlocks[i] == localhost {
-			localhostIndex = i
-		}
-	}
-	privateIPBlocks = append(privateIPBlocks[:localhostIndex], privateIPBlocks[localhostIndex+1:]...)
-
-	return localhost
-}
-
-func unshiftPrivateIPBlock(address *net.IPNet) {
-	privateIPBlocks = append([]*net.IPNet{address}, privateIPBlocks...)
-}
-
 type noLocalTransport struct {
 	inner  http.RoundTripper
 	errlog logrus.FieldLogger
diff --git a/api/hooks.go b/api/hooks.go
index 3ab7a825..66b34999 100644
--- a/api/hooks.go
+++ b/api/hooks.go
@@ -48,7 +48,6 @@ type Webhook struct {
 	jwtSecret string
 	claims    jwt.Claims
 	payload   []byte
-	headers   map[string]string
 }
 
 type WebhookResponse struct {
@@ -148,7 +147,9 @@ func (w *Webhook) generateSignature() (string, error) {
 
 func closeBody(rsp *http.Response) {
 	if rsp != nil && rsp.Body != nil {
-		rsp.Body.Close()
+		if err := rsp.Body.Close(); err != nil {
+			logrus.WithError(err).Warn("body close in hooks failed")
+		}
 	}
 }
 
@@ -231,11 +232,9 @@ func triggerHook(ctx context.Context, hookURL *url.URL, secret string, conn *sto
 	w.URL = hookURL.String()
 
 	body, err := w.trigger()
-	defer func() {
-		if body != nil {
-			body.Close()
-		}
-	}()
+	if body != nil {
+		defer body.Close()
+	}
 	if err == nil && body != nil {
 		webhookRsp := &WebhookResponse{}
 		decoder := json.NewDecoder(body)
diff --git a/api/mail.go b/api/mail.go
index cace797f..f2fbff9f 100644
--- a/api/mail.go
+++ b/api/mail.go
@@ -19,7 +19,6 @@ import (
 
 var (
 	MaxFrequencyLimitError error = errors.New("Frequency limit reached")
-	configFile                   = ""
 )
 
 type GenerateLinkParams struct {
diff --git a/api/middleware.go b/api/middleware.go
index a7122dee..69c76701 100644
--- a/api/middleware.go
+++ b/api/middleware.go
@@ -1,10 +1,8 @@
 package api
 
 import (
-	"bytes"
 	"context"
 	"encoding/json"
-	"io/ioutil"
 	"net/http"
 	"strings"
 	"time"
@@ -74,15 +72,13 @@ func (a *API) limitEmailSentHandler() middlewareHandler {
 		if config.External.Email.Enabled && !config.Mailer.Autoconfirm {
 			if req.Method == "PUT" || req.Method == "POST" {
 				res := make(map[string]interface{})
-				bodyBytes, err := ioutil.ReadAll(req.Body)
+
+				bodyBytes, err := getBodyBytes(req)
 				if err != nil {
 					return c, internalServerError("Error invalid request body").WithInternalError(err)
 				}
-				req.Body.Close()
-				req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
 
-				jsonDecoder := json.NewDecoder(bytes.NewBuffer(bodyBytes))
-				if err := jsonDecoder.Decode(&res); err != nil {
+				if err := json.Unmarshal(bodyBytes, &res); err != nil {
 					return c, badRequestError("Error invalid request body").WithInternalError(err)
 				}
 
diff --git a/api/provider/azure.go b/api/provider/azure.go
index 760660df..547bf239 100644
--- a/api/provider/azure.go
+++ b/api/provider/azure.go
@@ -25,12 +25,6 @@ type azureUser struct {
 	Sub   string `json:"sub"`
 }
 
-type azureEmail struct {
-	Email    string `json:"email"`
-	Primary  bool   `json:"is_primary"`
-	Verified bool   `json:"is_confirmed"`
-}
-
 // NewAzureProvider creates a Azure account provider.
 func NewAzureProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) {
 	if err := ext.Validate(); err != nil {
diff --git a/api/provider/notion.go b/api/provider/notion.go
index db8f84d0..c7179a65 100644
--- a/api/provider/notion.go
+++ b/api/provider/notion.go
@@ -79,18 +79,23 @@ func (g notionProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*Us
 
 	client := &http.Client{Timeout: defaultTimeout}
 	resp, err := client.Do(req)
-
 	if err != nil {
 		return nil, err
 	}
+	defer resp.Body.Close()
 
 	if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
 		return nil, fmt.Errorf("a %v error occurred with retrieving user from notion", resp.StatusCode)
 	}
 
-	body, _ := ioutil.ReadAll(resp.Body)
-	json.Unmarshal(body, &u)
-	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+	err = json.Unmarshal(body, &u)
+	if err != nil {
+		return nil, err
+	}
 
 	if u.Bot.Owner.User.Person.Email == "" {
 		return nil, errors.New("unable to find email with notion provider")
diff --git a/api/provider/twitch.go b/api/provider/twitch.go
index 517109c7..712f7d01 100644
--- a/api/provider/twitch.go
+++ b/api/provider/twitch.go
@@ -94,18 +94,24 @@ func (t twitchProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*Us
 
 	client := &http.Client{Timeout: defaultTimeout}
 	resp, err := client.Do(req)
-
 	if err != nil {
 		return nil, err
 	}
+	defer resp.Body.Close()
 
 	if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
 		return nil, fmt.Errorf("a %v error occurred with retrieving user from twitch", resp.StatusCode)
 	}
 
-	body, _ := ioutil.ReadAll(resp.Body)
-	json.Unmarshal(body, &u)
-	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	err = json.Unmarshal(body, &u)
+	if err != nil {
+		return nil, err
+	}
 
 	if len(u.Data) == 0 {
 		return nil, errors.New("unable to find user with twitch provider")
diff --git a/api/token.go b/api/token.go
index c6f36768..b92aedb1 100644
--- a/api/token.go
+++ b/api/token.go
@@ -59,18 +59,7 @@ type IdTokenGrantParams struct {
 	Issuer   string `json:"issuer"`
 }
 
-type tokenType string
-
-const (
-	confirmationToken       tokenType = "confirmation_token"
-	recoveryToken           tokenType = "recovery_token"
-	emailChangeTokenNew     tokenType = "email_change_token_new"
-	emailChangeTokenCurrent tokenType = "email_change_token_current"
-	reauthenticationToken   tokenType = "reauthentication_token"
-)
-
 const useCookieHeader = "x-use-cookie"
-const useSessionCookie = "session"
 const InvalidLoginMessage = "Invalid login credentials"
 
 func (p *IdTokenGrantParams) getVerifier(ctx context.Context, config *conf.GlobalConfiguration) (*oidc.IDTokenVerifier, error) {
diff --git a/mailer/mailer.go b/mailer/mailer.go
index 0d63b9da..266944f3 100644
--- a/mailer/mailer.go
+++ b/mailer/mailer.go
@@ -2,7 +2,6 @@ package mailer
 
 import (
 	"net/url"
-	"regexp"
 
 	"github.com/netlify/gotrue/conf"
 	"github.com/netlify/gotrue/models"
@@ -79,9 +78,3 @@ func getSiteURL(referrerURL, siteURL, filepath, fragment string) (string, error)
 	site.RawQuery = fragment
 	return site.String(), nil
 }
-
-var urlRegexp = regexp.MustCompile(`^https?://[^/]+`)
-
-func enforceRelativeURL(url string) string {
-	return urlRegexp.ReplaceAllString(url, "")
-}
diff --git a/mailer/noop.go b/mailer/noop.go
index b35a7af1..203c19af 100644
--- a/mailer/noop.go
+++ b/mailer/noop.go
@@ -2,46 +2,8 @@ package mailer
 
 import (
 	"errors"
-
-	"github.com/netlify/gotrue/models"
 )
 
-type noopMailer struct {
-	Mailer MailClient
-}
-
-func (m noopMailer) ValidateEmail(email string) error {
-	return nil
-}
-
-func (m *noopMailer) InviteMail(user *models.User, referrerURL string) error {
-	return nil
-}
-
-func (m *noopMailer) ConfirmationMail(user *models.User, referrerURL string) error {
-	return nil
-}
-
-func (m noopMailer) RecoveryMail(user *models.User, referrerURL string) error {
-	return nil
-}
-
-func (m noopMailer) MagicLinkMail(user *models.User, referrerURL string) error {
-	return nil
-}
-
-func (m *noopMailer) EmailChangeMail(user *models.User, referrerURL string) error {
-	return nil
-}
-
-func (m noopMailer) Send(user *models.User, subject, body string, data map[string]interface{}) error {
-	return nil
-}
-
-func (m noopMailer) GetEmailActionLink(user *models.User, actionType, referrerURL string) (string, error) {
-	return "", nil
-}
-
 type noopMailClient struct{}
 
 func (m *noopMailClient) Mail(to, subjectTemplate, templateURL, defaultTemplate string, templateData map[string]interface{}) error {
diff --git a/mailer/template.go b/mailer/template.go
index c60dc86e..9555bfe3 100644
--- a/mailer/template.go
+++ b/mailer/template.go
@@ -2,7 +2,6 @@ package mailer
 
 import (
 	"fmt"
-	"strings"
 
 	"github.com/badoux/checkmail"
 	"github.com/netlify/gotrue/conf"
@@ -329,17 +328,3 @@ func (m TemplateMailer) GetEmailActionLink(user *models.User, actionType, referr
 	}
 	return url, nil
 }
-
-// formatEmailOtp separates the otp into chunks of 5 with "-" as the separator
-func formatEmailOtp(otp string) string {
-	chunkSize := 5
-	var chunks []string
-	for i := 0; i < len(otp); i += chunkSize {
-		if i+chunkSize >= len(otp) {
-			chunks = append(chunks, otp[i:])
-		} else {
-			chunks = append(chunks, otp[i:i+chunkSize])
-		}
-	}
-	return strings.Join(chunks, "-")
-}
diff --git a/security/hcaptcha.go b/security/hcaptcha.go
index 64da884d..c584f77e 100644
--- a/security/hcaptcha.go
+++ b/security/hcaptcha.go
@@ -65,7 +65,10 @@ func VerifyRequest(r *http.Request, secretKey string) (VerificationResult, error
 	if err != nil {
 		return UserRequestFailed, err
 	}
-	r.Body.Close()
+	if err := r.Body.Close(); err != nil {
+		return UserRequestFailed, err
+	}
+
 	// re-init body so downstream route handlers don't get borked
 	r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
 


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 0923cedb..6331afc0 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -3,7 +3,10 @@ name: Test
 on:
   pull_request:
   push:
-    branches: [master]
+    branches:
+      - master
+      - mfa
+      - saml
     tags: ['*']
 
 jobs:
@@ -47,11 +50,16 @@ jobs:
         run: |
           set -x
           go vet ./...
+      - name: Check for unused code
+        run: |
+          set -x
+          go install honnef.co/go/tools/cmd/staticcheck@latest
+          make unused
       - name: Check gosec
         run: |
           set -x
           go install github.com/securego/gosec/v2/cmd/gosec@latest
-          gosec -tests -exclude=G104 ./... # exclude unhandled errors for the time being
+          make sec
       - name: Init Database
         run: psql -f hack/init_postgres.sql postgresql://postgres:root@localhost:5432/postgres
       - name: Run migrations
diff --git a/api/external_test.go b/api/external_test.go
index b582b2ba..daeadcb9 100644
--- a/api/external_test.go
+++ b/api/external_test.go
@@ -44,7 +44,6 @@ func (ts *ExternalTestSuite) createUser(providerId string, email string, name st
 		require.NoError(ts.T(), ts.API.db.Destroy(u), "Error deleting user")
 	}
 
-	// TODO: [Joel] -- refactor to take in phone
 	u, err := models.NewUser("", email, "test", ts.Config.JWT.Aud, map[string]interface{}{"provider_id": providerId, "full_name": name, "avatar_url": avatar})
 
 	if confirmationToken != "" {
diff --git a/api/helpers_test.go b/api/helpers_test.go
new file mode 100644
index 00000000..54f7beae
--- /dev/null
+++ b/api/helpers_test.go
@@ -0,0 +1,23 @@
+package api
+
+import (
+	"net"
+)
+
+func removeLocalhostFromPrivateIPBlock() *net.IPNet {
+	_, localhost, _ := net.ParseCIDR("127.0.0.0/8")
+
+	var localhostIndex int
+	for i := 0; i < len(privateIPBlocks); i++ {
+		if privateIPBlocks[i] == localhost {
+			localhostIndex = i
+		}
+	}
+	privateIPBlocks = append(privateIPBlocks[:localhostIndex], privateIPBlocks[localhostIndex+1:]...)
+
+	return localhost
+}
+
+func unshiftPrivateIPBlock(address *net.IPNet) {
+	privateIPBlocks = append([]*net.IPNet{address}, privateIPBlocks...)
+}
diff --git a/api/hook_test.go b/api/hook_test.go
index 6542143f..a000117c 100644
--- a/api/hook_test.go
+++ b/api/hook_test.go
@@ -17,6 +17,11 @@ import (
 	"github.com/stretchr/testify/require"
 )
 
+// withFunctionHooks adds the provided function hooks to the context.
+func withFunctionHooks(ctx context.Context, hooks map[string][]string) context.Context {
+	return context.WithValue(ctx, functionHooksKey, hooks)
+}
+
 func TestSignupHookSendInstanceID(t *testing.T) {
 	globalConfig, err := conf.LoadGlobal(apiTestConfig)
 	require.NoError(t, err)
diff --git a/mailer/mailer_test.go b/mailer/mailer_test.go
index a8a74ade..2cd565a1 100644
--- a/mailer/mailer_test.go
+++ b/mailer/mailer_test.go
@@ -1,11 +1,18 @@
 package mailer
 
 import (
+	"regexp"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 )
 
+var urlRegexp = regexp.MustCompile(`^https?://[^/]+`)
+
+func enforceRelativeURL(url string) string {
+	return urlRegexp.ReplaceAllString(url, "")
+}
+
 func TestGetSiteURL(t *testing.T) {
 	cases := []struct {
 		ReferrerURL string