up add sms
This commit is contained in:
81
pkg/captcha/aliyun.go
Normal file
81
pkg/captcha/aliyun.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
captcha20230305 "github.com/alibabacloud-go/captcha-20230305/client"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"ycc-server/common/xerr"
|
||||
)
|
||||
|
||||
// Config 验证码配置
|
||||
type Config struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
EndpointURL string
|
||||
SceneID string
|
||||
}
|
||||
|
||||
// Verify 验证阿里云滑块验证码
|
||||
func Verify(cfg Config, captchaVerifyParam string) error {
|
||||
// 开发环境跳过验证
|
||||
if os.Getenv("ENV") == "development" {
|
||||
logx.Info("[Captcha] 开发环境,跳过验证码校验")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查参数
|
||||
if captchaVerifyParam == "" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "empty captchaVerifyParam")
|
||||
}
|
||||
|
||||
// 创建客户端配置
|
||||
clientCfg := &openapi.Config{
|
||||
AccessKeyId: tea.String(cfg.AccessKeyID),
|
||||
AccessKeySecret: tea.String(cfg.AccessKeySecret),
|
||||
}
|
||||
clientCfg.Endpoint = tea.String(cfg.EndpointURL)
|
||||
|
||||
// 创建客户端
|
||||
client, err := captcha20230305.NewClient(clientCfg)
|
||||
if err != nil {
|
||||
logx.Errorf("[Captcha] 创建阿里云验证码客户端失败: %+v", err)
|
||||
// 客户端创建失败时,为了不影响业务可用性,记录日志但视为通过
|
||||
// 可根据风险偏好调整此策略
|
||||
return nil
|
||||
}
|
||||
|
||||
// 构建验证请求
|
||||
req := &captcha20230305.VerifyIntelligentCaptchaRequest{
|
||||
SceneId: tea.String(cfg.SceneID),
|
||||
CaptchaVerifyParam: tea.String(captchaVerifyParam),
|
||||
}
|
||||
|
||||
// 调用验证接口
|
||||
resp, err := client.VerifyIntelligentCaptcha(req)
|
||||
if err != nil {
|
||||
logx.Errorf("[Captcha] 调用阿里云验证码接口失败: %+v", err)
|
||||
// 接口调用失败时,为了不影响业务可用性,记录日志但视为通过
|
||||
// 可根据风险偏好调整此策略
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查验证结果
|
||||
if resp == nil || resp.Body == nil || resp.Body.Result == nil {
|
||||
logx.Errorf("[Captcha] 阿里云验证码响应异常: resp=%+v", resp)
|
||||
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "invalid response")
|
||||
}
|
||||
|
||||
if tea.BoolValue(resp.Body.Result.VerifyResult) {
|
||||
logx.Info("[Captcha] 验证码校验通过")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证失败
|
||||
logx.Errorf("[Captcha] 验证码校验失败: code=%s", tea.StringValue(resp.Body.Result.VerifyCode))
|
||||
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "verify failed: %s", tea.StringValue(resp.Body.Result.VerifyCode))
|
||||
}
|
||||
36
pkg/captcha/encrypt_scene.go
Normal file
36
pkg/captcha/encrypt_scene.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"ycc-server/pkg/lzkit/crypto"
|
||||
)
|
||||
|
||||
// GenerateEncryptedSceneID 生成加密的场景ID
|
||||
// 格式: sceneId×tamp&expireTime -> AES-256-CBC + PKCS7 -> Base64(IV + ciphertext)
|
||||
func GenerateEncryptedSceneID(sceneId, ekey string, expireSeconds int) (string, error) {
|
||||
// 参数校验
|
||||
if expireSeconds <= 0 || expireSeconds > 86400 {
|
||||
expireSeconds = 3600 // 默认1小时
|
||||
}
|
||||
|
||||
// 构建明文: sceneId×tamp&expireTime
|
||||
ts := time.Now().Unix()
|
||||
plaintext := fmt.Sprintf("%s&%d&%d", sceneId, ts, expireSeconds)
|
||||
|
||||
// 解码 ekey(Base64 -> 32字节密钥)
|
||||
keyBytes, err := base64.StdEncoding.DecodeString(ekey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decode ekey error: %w", err)
|
||||
}
|
||||
|
||||
// 验证密钥长度(AES-256 需要 32 字节)
|
||||
if len(keyBytes) != 32 {
|
||||
return "", fmt.Errorf("invalid ekey length, need 32 bytes after base64 decode, got %d", len(keyBytes))
|
||||
}
|
||||
|
||||
// 使用 AES 加密
|
||||
return crypto.AesEncrypt([]byte(plaintext), keyBytes)
|
||||
}
|
||||
Reference in New Issue
Block a user