|
8 | 8 | "github.com/gofrs/uuid" |
9 | 9 | "github.com/stretchr/testify/require" |
10 | 10 | "github.com/supabase/auth/internal/models" |
| 11 | + "github.com/supabase/auth/internal/security" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | // TestDiscoverableAuthenticationHappyPath tests the full discoverable credential authentication flow. |
@@ -201,6 +202,78 @@ func (ts *PasskeyTestSuite) TestAuthenticationPasskeyDisabled() { |
201 | 202 | ts.Equal(http.StatusNotFound, w.Code) |
202 | 203 | } |
203 | 204 |
|
| 205 | +// TestAuthenticationOptionsCaptchaRequired tests that CAPTCHA enabled + no token → 400. |
| 206 | +func (ts *PasskeyTestSuite) TestAuthenticationOptionsCaptchaRequired() { |
| 207 | + ts.Config.Security.Captcha.Enabled = true |
| 208 | + ts.Config.Security.Captcha.Provider = "hcaptcha" |
| 209 | + ts.Config.Security.Captcha.Secret = "test-secret" |
| 210 | + |
| 211 | + // No captcha_token in request body |
| 212 | + w := ts.makeRequest(http.MethodPost, "http://localhost/passkeys/authentication/options", map[string]any{}) |
| 213 | + ts.Equal(http.StatusBadRequest, w.Code) |
| 214 | + |
| 215 | + var errResp map[string]any |
| 216 | + require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&errResp)) |
| 217 | + ts.Equal("captcha_failed", errResp["error_code"]) |
| 218 | +} |
| 219 | + |
| 220 | +// TestAuthenticationOptionsCaptchaValid tests that CAPTCHA enabled + valid token → 200. |
| 221 | +func (ts *PasskeyTestSuite) TestAuthenticationOptionsCaptchaValid() { |
| 222 | + ts.Config.Security.Captcha.Enabled = true |
| 223 | + ts.Config.Security.Captcha.Provider = "hcaptcha" |
| 224 | + ts.Config.Security.Captcha.Secret = "test-secret" |
| 225 | + |
| 226 | + ts.CaptchaVerifier.Result = &security.VerificationResponse{Success: true} |
| 227 | + ts.CaptchaVerifier.Err = nil |
| 228 | + |
| 229 | + w := ts.makeRequest(http.MethodPost, "http://localhost/passkeys/authentication/options", map[string]any{ |
| 230 | + "gotrue_meta_security": map[string]any{ |
| 231 | + "captcha_token": "valid-token", |
| 232 | + }, |
| 233 | + }) |
| 234 | + ts.Equal(http.StatusOK, w.Code) |
| 235 | + |
| 236 | + var optionsResp PasskeyAuthenticationOptionsResponse |
| 237 | + require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&optionsResp)) |
| 238 | + ts.NotEmpty(optionsResp.ChallengeID) |
| 239 | +} |
| 240 | + |
| 241 | +// TestAuthenticationOptionsCaptchaInvalid tests that CAPTCHA enabled + mock failure → 400. |
| 242 | +func (ts *PasskeyTestSuite) TestAuthenticationOptionsCaptchaInvalid() { |
| 243 | + ts.Config.Security.Captcha.Enabled = true |
| 244 | + ts.Config.Security.Captcha.Provider = "hcaptcha" |
| 245 | + ts.Config.Security.Captcha.Secret = "test-secret" |
| 246 | + |
| 247 | + ts.CaptchaVerifier.Result = &security.VerificationResponse{ |
| 248 | + Success: false, |
| 249 | + ErrorCodes: []string{"invalid-input-response"}, |
| 250 | + } |
| 251 | + ts.CaptchaVerifier.Err = nil |
| 252 | + |
| 253 | + w := ts.makeRequest(http.MethodPost, "http://localhost/passkeys/authentication/options", map[string]any{ |
| 254 | + "gotrue_meta_security": map[string]any{ |
| 255 | + "captcha_token": "bad-token", |
| 256 | + }, |
| 257 | + }) |
| 258 | + ts.Equal(http.StatusBadRequest, w.Code) |
| 259 | + |
| 260 | + var errResp map[string]any |
| 261 | + require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&errResp)) |
| 262 | + ts.Equal("captcha_failed", errResp["error_code"]) |
| 263 | +} |
| 264 | + |
| 265 | +// TestAuthenticationOptionsCaptchaDisabled tests that CAPTCHA disabled → 200 without token. |
| 266 | +func (ts *PasskeyTestSuite) TestAuthenticationOptionsCaptchaDisabled() { |
| 267 | + ts.Config.Security.Captcha.Enabled = false |
| 268 | + |
| 269 | + w := ts.makeRequest(http.MethodPost, "http://localhost/passkeys/authentication/options", nil) |
| 270 | + ts.Equal(http.StatusOK, w.Code) |
| 271 | + |
| 272 | + var optionsResp PasskeyAuthenticationOptionsResponse |
| 273 | + require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&optionsResp)) |
| 274 | + ts.NotEmpty(optionsResp.ChallengeID) |
| 275 | +} |
| 276 | + |
204 | 277 | // registerPasskey is a test helper that registers a passkey for the test user |
205 | 278 | // and returns the authenticator (with stored credential) for later assertion. |
206 | 279 | func (ts *PasskeyTestSuite) registerPasskey() (*virtualAuthenticator, *PasskeyMetadataResponse) { |
|
0 commit comments