Skip to content

Commit 3b03472

Browse files
authored
chore: use retrieveRequestParams where possible (#1818)
## What kind of change does this PR introduce? * Move the reading the requestBody into the captcha middleware, which is in the api package so it can use `retrieveRequestParams` * Use `retrieveRequestParams` in the admin delete route ## What is the current behavior? Please link any relevant issues here. ## What is the new behavior? Feel free to include screenshots if it includes visual changes. ## Additional context Add any other context or screenshots.
1 parent 158e473 commit 3b03472

File tree

4 files changed

+21
-36
lines changed

4 files changed

+21
-36
lines changed

internal/api/admin.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"context"
5-
"encoding/json"
65
"net/http"
76
"time"
87

@@ -15,6 +14,7 @@ import (
1514
"github.com/supabase/auth/internal/models"
1615
"github.com/supabase/auth/internal/observability"
1716
"github.com/supabase/auth/internal/storage"
17+
"github.com/supabase/auth/internal/utilities"
1818
"golang.org/x/crypto/bcrypt"
1919
)
2020

@@ -512,21 +512,17 @@ func (a *API) adminUserDelete(w http.ResponseWriter, r *http.Request) error {
512512
user := getUser(ctx)
513513
adminUser := getAdminUser(ctx)
514514

515-
var err error
515+
// ShouldSoftDelete defaults to false
516516
params := &adminUserDeleteParams{}
517-
body, err := getBodyBytes(r)
518-
if err != nil {
519-
return internalServerError("Could not read body").WithInternalError(err)
520-
}
521-
if len(body) > 0 {
522-
if err := json.Unmarshal(body, params); err != nil {
523-
return badRequestError(ErrorCodeBadJSON, "Could not read params: %v", err)
517+
if body, _ := utilities.GetBodyBytes(r); len(body) != 0 {
518+
// we only want to parse the body if it's not empty
519+
// retrieveRequestParams will handle any errors with stream
520+
if err := retrieveRequestParams(r, params); err != nil {
521+
return err
524522
}
525-
} else {
526-
params.ShouldSoftDelete = false
527523
}
528524

529-
err = a.db.Transaction(func(tx *storage.Connection) error {
525+
err := a.db.Transaction(func(tx *storage.Connection) error {
530526
if terr := models.NewAuditLogEntry(r, tx, adminUser, models.UserDeletedAction, "", map[string]interface{}{
531527
"user_id": user.ID,
532528
"user_email": user.Email,

internal/api/helpers.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/supabase/auth/internal/conf"
1111
"github.com/supabase/auth/internal/models"
12+
"github.com/supabase/auth/internal/security"
1213
"github.com/supabase/auth/internal/utilities"
1314
)
1415

@@ -57,11 +58,6 @@ func isStringInSlice(checkValue string, list []string) bool {
5758
return false
5859
}
5960

60-
// getBodyBytes returns a byte array of the request's Body.
61-
func getBodyBytes(req *http.Request) ([]byte, error) {
62-
return utilities.GetBodyBytes(req)
63-
}
64-
6561
type RequestParams interface {
6662
AdminUserParams |
6763
CreateSSOProviderParams |
@@ -82,6 +78,8 @@ type RequestParams interface {
8278
VerifyFactorParams |
8379
VerifyParams |
8480
adminUserUpdateFactorParams |
81+
adminUserDeleteParams |
82+
security.GotrueRequest |
8583
ChallengeFactorParams |
8684
struct {
8785
Email string `json:"email"`
@@ -94,7 +92,7 @@ type RequestParams interface {
9492

9593
// retrieveRequestParams is a generic method that unmarshals the request body into the params struct provided
9694
func retrieveRequestParams[A RequestParams](r *http.Request, params *A) error {
97-
body, err := getBodyBytes(r)
95+
body, err := utilities.GetBodyBytes(r)
9896
if err != nil {
9997
return internalServerError("Could not read body into byte slice").WithInternalError(err)
10098
}

internal/api/middleware.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/supabase/auth/internal/models"
1616
"github.com/supabase/auth/internal/observability"
1717
"github.com/supabase/auth/internal/security"
18+
"github.com/supabase/auth/internal/utilities"
1819

1920
"github.com/didip/tollbooth/v5"
2021
"github.com/didip/tollbooth/v5/limiter"
@@ -116,12 +117,13 @@ func (a *API) verifyCaptcha(w http.ResponseWriter, req *http.Request) (context.C
116117
return ctx, nil
117118
}
118119

119-
verificationResult, err := security.VerifyRequest(req, strings.TrimSpace(config.Security.Captcha.Secret), config.Security.Captcha.Provider)
120-
if err != nil {
121-
if strings.Contains(err.Error(), "request body was not JSON") {
122-
return nil, badRequestError(ErrorCodeValidationFailed, "Request body for CAPTCHA verification was not a valid JSON object")
123-
}
120+
body := &security.GotrueRequest{}
121+
if err := retrieveRequestParams(req, body); err != nil {
122+
return nil, err
123+
}
124124

125+
verificationResult, err := security.VerifyRequest(body, utilities.GetIPAddress(req), strings.TrimSpace(config.Security.Captcha.Secret), config.Security.Captcha.Provider)
126+
if err != nil {
125127
return nil, internalServerError("captcha verification process failed").WithInternalError(err)
126128
}
127129

internal/security/captcha.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"fmt"
14+
1415
"github.com/pkg/errors"
1516
"github.com/supabase/auth/internal/utilities"
1617
)
@@ -45,25 +46,13 @@ func init() {
4546
Client = &http.Client{Timeout: defaultTimeout}
4647
}
4748

48-
func VerifyRequest(r *http.Request, secretKey, captchaProvider string) (VerificationResponse, error) {
49-
bodyBytes, err := utilities.GetBodyBytes(r)
50-
if err != nil {
51-
return VerificationResponse{}, err
52-
}
53-
54-
var requestBody GotrueRequest
55-
56-
if err := json.Unmarshal(bodyBytes, &requestBody); err != nil {
57-
return VerificationResponse{}, errors.Wrap(err, "request body was not JSON")
58-
}
59-
49+
func VerifyRequest(requestBody *GotrueRequest, clientIP, secretKey, captchaProvider string) (VerificationResponse, error) {
6050
captchaResponse := strings.TrimSpace(requestBody.Security.Token)
6151

6252
if captchaResponse == "" {
6353
return VerificationResponse{}, errors.New("no captcha response (captcha_token) found in request")
6454
}
6555

66-
clientIP := utilities.GetIPAddress(r)
6756
captchaURL, err := GetCaptchaURL(captchaProvider)
6857
if err != nil {
6958
return VerificationResponse{}, err

0 commit comments

Comments
 (0)