fadd
This commit is contained in:
53
pkg/captcha/aliyun.go
Normal file
53
pkg/captcha/aliyun.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
|
||||
"github.com/alibabacloud-go/captcha-20230305/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
EndpointURL string
|
||||
SceneID string
|
||||
}
|
||||
|
||||
func Verify(cfg Config, captchaVerifyParam string) error {
|
||||
if os.Getenv("ENV") == "development" {
|
||||
return nil
|
||||
}
|
||||
if captchaVerifyParam == "" {
|
||||
return errors.New("图形验证码校验失败")
|
||||
}
|
||||
|
||||
clientCfg := &openapi.Config{
|
||||
AccessKeyId: tea.String(cfg.AccessKeyID),
|
||||
AccessKeySecret: tea.String(cfg.AccessKeySecret),
|
||||
Endpoint: tea.String(cfg.EndpointURL),
|
||||
}
|
||||
captchaClient, err := client.NewClient(clientCfg)
|
||||
if err != nil {
|
||||
return errors.New("图形验证码校验失败")
|
||||
}
|
||||
|
||||
req := &client.VerifyIntelligentCaptchaRequest{
|
||||
SceneId: tea.String(cfg.SceneID),
|
||||
CaptchaVerifyParam: tea.String(captchaVerifyParam),
|
||||
}
|
||||
|
||||
resp, err := captchaClient.VerifyIntelligentCaptcha(req)
|
||||
if err != nil {
|
||||
return errors.New("图形验证码校验失败")
|
||||
}
|
||||
|
||||
if resp.Body == nil || !tea.BoolValue(resp.Body.Result.VerifyResult) {
|
||||
return errors.New("图形验证码校验失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
35
pkg/captcha/encrypt_scene.go
Normal file
35
pkg/captcha/encrypt_scene.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
lzcrypto "tydata-server/pkg/lzkit/crypto"
|
||||
)
|
||||
|
||||
type EncryptedSceneID struct {
|
||||
SceneID string
|
||||
Timestamp int64
|
||||
ExpireTime int
|
||||
EncryptedID string
|
||||
}
|
||||
|
||||
func GenerateEncryptedSceneID(sceneId, ekey string, expireSeconds int) (string, error) {
|
||||
if expireSeconds <= 0 || expireSeconds > 86400 {
|
||||
expireSeconds = 3600
|
||||
}
|
||||
|
||||
ts := time.Now().Unix()
|
||||
plaintext := fmt.Sprintf("%s&%d&%d", sceneId, ts, expireSeconds)
|
||||
|
||||
keyBytes, err := base64.StdEncoding.DecodeString(ekey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decode ekey error: %w", err)
|
||||
}
|
||||
if len(keyBytes) != 32 {
|
||||
return "", fmt.Errorf("invalid ekey length, need 32 bytes after base64 decode, got %d", len(keyBytes))
|
||||
}
|
||||
|
||||
return lzcrypto.AesEncrypt([]byte(plaintext), keyBytes)
|
||||
}
|
||||
Reference in New Issue
Block a user