Skip to content

Commit

Permalink
feat: add captcha to verify and token endpoints (supabase#520)
Browse files Browse the repository at this point in the history
* fix: add captcha to verify and token endpoints

* don't enable captcha on refresh token grant_type

* refactor: rename hcaptcha_token to captcha_token for generalizability
  • Loading branch information
kangmingtay authored and LashaJini committed Nov 13, 2024
1 parent dab2dd5 commit c4935c1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
tollbooth.NewLimiter(api.config.RateLimitTokenRefresh/(60*5), &limiter.ExpirableOptions{
DefaultExpirationTTL: time.Hour,
}).SetBurst(30),
)).Post("/token", api.Token)
)).With(api.verifyCaptcha).Post("/token", api.Token)

r.With(api.limitHandler(
// Allow requests at the specified rate per 5 minutes.
Expand All @@ -136,7 +136,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
}).SetBurst(30),
)).Route("/verify", func(r *router) {
r.Get("/", api.Verify)
r.Post("/", api.Verify)
r.With(api.verifyCaptcha).Post("/", api.Verify)
})

r.With(api.requireAuthentication).Post("/logout", api.Logout)
Expand Down
6 changes: 3 additions & 3 deletions api/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (ts *MiddlewareTestSuite) TestVerifyCaptchaValid() {
"email": "test@example.com",
"password": "secret",
"gotrue_meta_security": map[string]interface{}{
"hcaptcha_token": HCaptchaResponse,
"captcha_token": HCaptchaResponse,
},
}))

Expand All @@ -75,7 +75,7 @@ func (ts *MiddlewareTestSuite) TestVerifyCaptchaValid() {
"email": "test@example.com",
"password": "secret",
"gotrue_meta_security": map[string]interface{}{
"hcaptcha_token": HCaptchaResponse,
"captcha_token": HCaptchaResponse,
},
}))

Expand Down Expand Up @@ -129,7 +129,7 @@ func (ts *MiddlewareTestSuite) TestVerifyCaptchaInvalid() {
"email": "test@example.com",
"password": "secret",
"gotrue_meta_security": map[string]interface{}{
"hcaptcha_token": HCaptchaResponse,
"captcha_token": HCaptchaResponse,
},
}))
req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer)
Expand Down
6 changes: 5 additions & 1 deletion security/hcaptcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type GotrueRequest struct {
}

type GotrueSecurity struct {
Token string `json:"hcaptcha_token"`
Token string `json:"captcha_token"`
}

type VerificationResponse struct {
Expand Down Expand Up @@ -56,6 +56,10 @@ func init() {
}

func VerifyRequest(r *http.Request, secretKey string) (VerificationResult, error) {
if r.FormValue("grant_type") == "refresh_token" {
// captcha shouldn't be enabled on requests to refresh the token
return SuccessfullyVerified, nil
}
res := GotrueRequest{}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down

0 comments on commit c4935c1

Please sign in to comment.