cmb
This commit is contained in:
parent
d551c83294
commit
e9e86d3fc5
|
|
@ -0,0 +1,10 @@
|
||||||
|
package mixrepos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
v1 "voucher/api/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CmbMixRepo interface {
|
||||||
|
BuildRequest(ctx context.Context, bizJsonStr string) (*v1.CmbRequest, error)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package mixrepoimpl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
v1 "voucher/api/v1"
|
||||||
|
"voucher/internal/biz/mixrepos"
|
||||||
|
"voucher/internal/conf"
|
||||||
|
"voucher/internal/pkg/cmb"
|
||||||
|
"voucher/internal/pkg/helper"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CmbMixRepoImpl struct {
|
||||||
|
bc *conf.Bootstrap
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCmbMixRepoImpl(bc *conf.Bootstrap) mixrepos.CmbMixRepo {
|
||||||
|
return &CmbMixRepoImpl{bc: bc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CmbMixRepoImpl) BuildRequest(_ context.Context, bizJsonStr string) (*v1.CmbRequest, error) {
|
||||||
|
// 我们的sm2 公钥加密
|
||||||
|
// 请求到我们这边 使用 我们的私钥解密
|
||||||
|
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
date := time.Now().Format("20060102150405")
|
||||||
|
|
||||||
|
reply := &v1.CmbRequest{
|
||||||
|
Mid: s.bc.Cmb.Mid,
|
||||||
|
Aid: s.bc.Cmb.Aid,
|
||||||
|
Date: date,
|
||||||
|
Random: string(cmb.RandomBytes(16)),
|
||||||
|
KeyAlias: s.bc.Cmb.KeyAlias,
|
||||||
|
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
|
||||||
|
EncryptBody: encryptBody,
|
||||||
|
Sign: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
kvRows := helper.SortStructFieldsByKey(reply)
|
||||||
|
|
||||||
|
var strToBeSigned strings.Builder
|
||||||
|
for _, kv := range kvRows {
|
||||||
|
if kv.Key == "sign" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if kv.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
str := fmt.Sprintf("/voucher/cmb/v1/order?%s", strings.TrimRight(strToBeSigned.String(), "&"))
|
||||||
|
|
||||||
|
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reply.Sign = sing
|
||||||
|
|
||||||
|
return reply, nil
|
||||||
|
}
|
||||||
|
|
@ -8,4 +8,5 @@ import (
|
||||||
var ProviderMixRepoImplSet = wire.NewSet(
|
var ProviderMixRepoImplSet = wire.NewSet(
|
||||||
NewGenerateMixRepoImpl,
|
NewGenerateMixRepoImpl,
|
||||||
NewMQSendMixRepoImpl,
|
NewMQSendMixRepoImpl,
|
||||||
|
NewCmbMixRepoImpl,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,8 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/go-kratos/kratos/v2/transport/http"
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
v1 "voucher/api/v1"
|
v1 "voucher/api/v1"
|
||||||
"voucher/internal/pkg/cmb"
|
|
||||||
"voucher/internal/pkg/helper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
||||||
|
|
@ -25,47 +20,11 @@ func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
||||||
|
|
||||||
bizJsonStr := string(bizJsonBytes)
|
bizJsonStr := string(bizJsonBytes)
|
||||||
|
|
||||||
// 我们的sm2 公钥加密
|
reply, err := s.CmbMixRepo.BuildRequest(ctx, bizJsonStr)
|
||||||
// 请求到我们这边 使用 我们的私钥解密
|
|
||||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
date := time.Now().Format("20060102150405")
|
|
||||||
|
|
||||||
reply := &v1.CmbRequest{
|
|
||||||
Mid: s.bc.Cmb.Mid,
|
|
||||||
Aid: s.bc.Cmb.Aid,
|
|
||||||
Date: date,
|
|
||||||
Random: string(cmb.RandomBytes(16)),
|
|
||||||
KeyAlias: s.bc.Cmb.KeyAlias,
|
|
||||||
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
|
|
||||||
EncryptBody: encryptBody,
|
|
||||||
Sign: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
kvRows := helper.SortStructFieldsByKey(reply)
|
|
||||||
|
|
||||||
var strToBeSigned strings.Builder
|
|
||||||
for _, kv := range kvRows {
|
|
||||||
if kv.Key == "sign" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if kv.Value == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value))
|
|
||||||
}
|
|
||||||
|
|
||||||
str := fmt.Sprintf("/voucher/cmb/v1/order?%s", strings.TrimRight(strToBeSigned.String(), "&"))
|
|
||||||
|
|
||||||
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
reply.Sign = sing
|
|
||||||
|
|
||||||
return ctx.JSON(200, reply)
|
return ctx.JSON(200, reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,24 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"voucher/internal/biz"
|
"voucher/internal/biz"
|
||||||
|
"voucher/internal/biz/mixrepos"
|
||||||
"voucher/internal/conf"
|
"voucher/internal/conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VoucherService struct {
|
type VoucherService struct {
|
||||||
bc *conf.Bootstrap
|
bc *conf.Bootstrap
|
||||||
VoucherBiz *biz.VoucherBiz
|
VoucherBiz *biz.VoucherBiz
|
||||||
|
CmbMixRepo mixrepos.CmbMixRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVoucherService(
|
func NewVoucherService(
|
||||||
bc *conf.Bootstrap,
|
bc *conf.Bootstrap,
|
||||||
VoucherBiz *biz.VoucherBiz,
|
VoucherBiz *biz.VoucherBiz,
|
||||||
|
CmbMixRepo mixrepos.CmbMixRepo,
|
||||||
) *VoucherService {
|
) *VoucherService {
|
||||||
return &VoucherService{
|
return &VoucherService{
|
||||||
bc: bc,
|
bc: bc,
|
||||||
VoucherBiz: VoucherBiz,
|
VoucherBiz: VoucherBiz,
|
||||||
|
CmbMixRepo: CmbMixRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue