github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/hcaptcha/hcaptcha.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package hcaptcha
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/gitbundle/modules/setting"
    12  
    13  	"go.jolheiser.com/hcaptcha"
    14  )
    15  
    16  // Verify calls hCaptcha API to verify token
    17  func Verify(ctx context.Context, response string) (bool, error) {
    18  	client, err := hcaptcha.New(setting.Service.HcaptchaSecret, hcaptcha.WithContext(ctx))
    19  	if err != nil {
    20  		return false, err
    21  	}
    22  
    23  	resp, err := client.Verify(response, hcaptcha.PostOptions{
    24  		Sitekey: setting.Service.HcaptchaSitekey,
    25  	})
    26  	if err != nil {
    27  		return false, err
    28  	}
    29  
    30  	var respErr error
    31  	if len(resp.ErrorCodes) > 0 {
    32  		respErr = resp.ErrorCodes[0]
    33  	}
    34  	return resp.Success, respErr
    35  }