feat: 接入消息加密与实名认证能力
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
cloudauth "github.com/alibabacloud-go/cloudauth-20190307/v3/client"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/dara"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
RegionID string
|
||||
Endpoint string
|
||||
TimeoutMS int
|
||||
}
|
||||
|
||||
type ID2MetaResult struct {
|
||||
Passed bool
|
||||
BizCode string
|
||||
RequestID string
|
||||
ProviderCode string
|
||||
ProviderMessage string
|
||||
ProviderStatusCode int32
|
||||
Latency time.Duration
|
||||
}
|
||||
|
||||
type ID2MetaClient struct {
|
||||
client *cloudauth.Client
|
||||
timeoutMS int
|
||||
}
|
||||
|
||||
func NewID2MetaClient(config Config) (*ID2MetaClient, error) {
|
||||
if strings.TrimSpace(config.AccessKeyID) == "" || strings.TrimSpace(config.AccessKeySecret) == "" {
|
||||
return nil, errors.New("missing aliyun realname credentials")
|
||||
}
|
||||
if strings.TrimSpace(config.RegionID) == "" {
|
||||
config.RegionID = "cn-hangzhou"
|
||||
}
|
||||
clientConfig := &openapi.Config{
|
||||
AccessKeyId: dara.String(strings.TrimSpace(config.AccessKeyID)),
|
||||
AccessKeySecret: dara.String(strings.TrimSpace(config.AccessKeySecret)),
|
||||
RegionId: dara.String(strings.TrimSpace(config.RegionID)),
|
||||
Protocol: dara.String("https"),
|
||||
}
|
||||
if endpoint := strings.TrimSpace(config.Endpoint); endpoint != "" {
|
||||
clientConfig.Endpoint = dara.String(endpoint)
|
||||
}
|
||||
client, err := cloudauth.NewClient(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.TimeoutMS <= 0 {
|
||||
config.TimeoutMS = 1500
|
||||
}
|
||||
return &ID2MetaClient{client: client, timeoutMS: config.TimeoutMS}, nil
|
||||
}
|
||||
|
||||
func (c *ID2MetaClient) Verify(name, idNumber string) (*ID2MetaResult, error) {
|
||||
if c == nil || c.client == nil {
|
||||
return nil, errors.New("nil aliyun realname client")
|
||||
}
|
||||
name = strings.TrimSpace(name)
|
||||
idNumber = strings.TrimSpace(idNumber)
|
||||
if name == "" || idNumber == "" {
|
||||
return nil, errors.New("missing realname identity fields")
|
||||
}
|
||||
started := time.Now()
|
||||
resp, err := c.client.Id2MetaVerifyWithOptions(&cloudauth.Id2MetaVerifyRequest{
|
||||
UserName: dara.String(name),
|
||||
IdentifyNum: dara.String(idNumber),
|
||||
ParamType: dara.String("normal"),
|
||||
}, (&util.RuntimeOptions{}).SetConnectTimeout(c.timeoutMS).SetReadTimeout(c.timeoutMS).SetAutoretry(false))
|
||||
latency := time.Since(started)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &ID2MetaResult{Latency: latency}
|
||||
if resp != nil {
|
||||
result.ProviderStatusCode = dara.Int32Value(resp.StatusCode)
|
||||
}
|
||||
if resp == nil || resp.Body == nil {
|
||||
return result, errors.New("empty aliyun realname response")
|
||||
}
|
||||
result.RequestID = dara.StringValue(resp.Body.RequestId)
|
||||
result.ProviderCode = dara.StringValue(resp.Body.Code)
|
||||
result.ProviderMessage = dara.StringValue(resp.Body.Message)
|
||||
if resp.Body.ResultObject != nil {
|
||||
result.BizCode = strings.TrimSpace(dara.StringValue(resp.Body.ResultObject.BizCode))
|
||||
}
|
||||
if result.ProviderCode != "" && result.ProviderCode != "200" {
|
||||
return result, errors.New("aliyun realname response code " + result.ProviderCode)
|
||||
}
|
||||
if result.BizCode == "" {
|
||||
return result, errors.New("empty aliyun realname biz code")
|
||||
}
|
||||
result.Passed = result.BizCode == "1"
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ParseTimeoutMS(value string, fallback int) int {
|
||||
parsed, err := strconv.Atoi(strings.TrimSpace(value))
|
||||
if err != nil || parsed <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
Reference in New Issue
Block a user