API: Add recaptcha validation for signup and forget password api#374
Conversation
Reviewer's Guide by SourceryThis pull request adds reCAPTCHA validation for the signup and forget password API endpoints. It introduces a new method for verifying CAPTCHA tokens, modifies existing functions to include CAPTCHA validation, and updates the necessary dependencies and environment variables. Sequence DiagramsSignUp Process with reCAPTCHAsequenceDiagram
participant C as Client
participant A as API
participant R as reCAPTCHA
participant D as Domain
C->>A: SignUp(name, email, password, captchaToken)
A->>D: SignUp(name, email, password, captchaToken)
D->>R: verifyCaptcha(captchaToken)
R-->>D: Validation Result
alt CAPTCHA Valid
D->>D: Create User
D-->>A: AuthSession
A-->>C: Session
else CAPTCHA Invalid
D-->>A: Error
A-->>C: Error
end
RequestResetPassword Process with reCAPTCHAsequenceDiagram
participant C as Client
participant A as API
participant R as reCAPTCHA
participant D as Domain
C->>A: RequestResetPassword(email, captchaToken)
A->>D: RequestResetPassword(email, captchaToken)
D->>R: verifyCaptcha(captchaToken)
R-->>D: Validation Result
alt CAPTCHA Valid
D->>D: Generate Reset Token
D-->>A: Success
A-->>C: Success
else CAPTCHA Invalid
D-->>A: Error
A-->>C: Error
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey @nxtcoder19 - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding unit tests for the
verifyCaptchafunction and integration tests for the modified signup and password reset flows to ensure the new security measure works as expected. - To improve performance, consider implementing a short-term caching mechanism for successful CAPTCHA validations to reduce the number of API calls to the reCAPTCHA service.
- Please add documentation for the new environment variables (GOOGLE_CLOUD_PROJECT_ID, RECAPTCHA_SITE_KEY, GOOGLE_APPLICATION_CREDENTIALS) and include setup instructions for the reCAPTCHA integration in the project documentation.
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟡 Security: 1 issue found
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| } | ||
|
|
||
| func (d *domainI) SignUp(ctx context.Context, name string, email string, password string) (*common.AuthSession, error) { | ||
| func (d *domainI) verifyCaptcha(ctx context.Context, token string) (bool, error) { |
There was a problem hiding this comment.
suggestion (performance): Consider reusing the reCAPTCHA client instead of creating a new one for each verification.
Creating a new client for each verification may impact performance. Consider initializing the client in the domain struct constructor and reusing it across calls.
type domainI struct {
// ... other fields ...
recaptchaClient *recaptchaenterprise.Client
}
func NewDomainI(ctx context.Context) (*domainI, error) {
client, err := recaptchaenterprise.NewClient(ctx)
if err != nil {
return nil, err
}
return &domainI{recaptchaClient: client}, nil
}
func (d *domainI) verifyCaptcha(ctx context.Context, token string) (bool, error) {
| return true, nil | ||
| } | ||
|
|
||
| func (d *domainI) SignUp(ctx context.Context, name string, email string, password string, captchaToken string) (*common.AuthSession, error) { |
There was a problem hiding this comment.
🚨 suggestion (security): Implement rate limiting for the SignUp function to prevent brute force attacks.
While CAPTCHA helps, adding rate limiting would provide an additional layer of protection against brute force attacks on the signup process.
func (d *domainI) SignUp(ctx context.Context, name string, email string, password string, captchaToken string) (*common.AuthSession, error) {
if err := d.rateLimiter.Allow(ctx, "signup_"+name); err != nil {
return nil, fmt.Errorf("rate limit exceeded: %w", err)
}
| } | ||
|
|
||
| func (d *domainI) RequestResetPassword(ctx context.Context, email string) (bool, error) { | ||
| func (d *domainI) RequestResetPassword(ctx context.Context, email string, captchaToken string) (bool, error) { |
There was a problem hiding this comment.
suggestion: Standardize error handling for CAPTCHA verification across functions.
The error handling for CAPTCHA verification differs between SignUp and RequestResetPassword. Consider standardizing this for consistency and better debugging.
func (d *domainI) RequestResetPassword(ctx context.Context, email string, captchaToken string) (bool, error) {
if err := d.verifyCaptcha(ctx, captchaToken); err != nil {
return false, fmt.Errorf("captcha verification failed: %w", err)
}
5a812b9 to
b27891f
Compare
API: Add recaptcha validation for signup and forget password api
Summary by Sourcery
Add reCAPTCHA validation to the SignUp and RequestResetPassword APIs to prevent automated abuse. Implement a new method for verifying reCAPTCHA tokens using Google Cloud's reCAPTCHA Enterprise API and update the GraphQL resolvers to include captchaToken parameters. Update environment configuration to include necessary Google Cloud credentials.
New Features:
Enhancements:
Build: