Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

API: Add recaptcha validation for signup and forget password api#374

Merged
nxtcoder17 merged 1 commit into
release-v1.0.8from
impl/recaptcha
Sep 30, 2024
Merged

API: Add recaptcha validation for signup and forget password api#374
nxtcoder17 merged 1 commit into
release-v1.0.8from
impl/recaptcha

Conversation

@nxtcoder19
Copy link
Copy Markdown
Contributor

@nxtcoder19 nxtcoder19 commented Sep 30, 2024

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:

  • Introduce reCAPTCHA validation for the SignUp and RequestResetPassword functionalities to enhance security against automated requests.

Enhancements:

  • Add a new method verifyCaptcha to handle reCAPTCHA token verification using Google Cloud's reCAPTCHA Enterprise API.

Build:

  • Update go.mod to include the cloud.google.com/go/recaptchaenterprise/v2 package and other related dependencies for reCAPTCHA integration.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Sep 30, 2024

Reviewer's Guide by Sourcery

This 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 Diagrams

SignUp Process with reCAPTCHA

sequenceDiagram
    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
Loading

RequestResetPassword Process with reCAPTCHA

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Implement reCAPTCHA validation
  • Add verifyCaptcha method to validate reCAPTCHA tokens
  • Update SignUp function to include CAPTCHA validation
  • Update RequestResetPassword function to include CAPTCHA validation
apps/auth/internal/domain/impl.go
Update API resolvers to include CAPTCHA token
  • Modify AuthSignup resolver to accept captchaToken parameter
  • Modify AuthRequestResetPassword resolver to accept captchaToken parameter
apps/auth/internal/app/graph/schema.resolvers.go
Add reCAPTCHA Enterprise dependency
  • Add cloud.google.com/go/recaptchaenterprise/v2 to go.mod
  • Include related Google Cloud dependencies
go.mod
Update environment variables
  • Add GoogleCloudProjectId environment variable
  • Add RecaptchaSiteKey environment variable
  • Add GoogleApplicationCredentials environment variable
apps/auth/internal/env/env.go
Update domain interface
  • Modify SignUp method signature to include captchaToken parameter
  • Modify RequestResetPassword method signature to include captchaToken parameter
apps/auth/internal/domain/domain.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nxtcoder19 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding unit tests for the verifyCaptcha function 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
	}

@nxtcoder17 nxtcoder17 merged commit ba59ee1 into release-v1.0.8 Sep 30, 2024
@nxtcoder17 nxtcoder17 deleted the impl/recaptcha branch September 30, 2024 13:38
abdheshnayak pushed a commit that referenced this pull request Nov 5, 2024
API: Add recaptcha validation for signup and forget password api
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants