feat: integrate OA activation workflows and business migration
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package oa
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/v3/errors"
|
||||
"kra/internal/biz/business"
|
||||
"kra/internal/config"
|
||||
)
|
||||
|
||||
type activationGateway struct {
|
||||
config *config.Store
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewActivationGateway(store *config.Store) business.ActivationGateway {
|
||||
return &activationGateway{config: store, client: &http.Client{Timeout: 15 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}
|
||||
}
|
||||
func (g *activationGateway) Apply(ctx context.Context, a business.ActivationApplication) error {
|
||||
cfg := g.config.Snapshot()
|
||||
if cfg == nil || cfg.BusinessOA == nil || cfg.BusinessOA.URL == "" || cfg.BusinessOA.Key == "" {
|
||||
return business.ErrActivationSource
|
||||
}
|
||||
body, err := json.Marshal(struct {
|
||||
UserID string `json:"userId"`
|
||||
PoolID string `json:"poolId"`
|
||||
BusierID string `json:"busierId"`
|
||||
SourceDB string `json:"sourceDb"`
|
||||
}{strconv.FormatUint(uint64(a.UserID), 10), strconv.FormatUint(a.PoolID, 10), strconv.FormatUint(a.BusierID, 10), a.SourceDB})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(cfg.BusinessOA.URL, "/")+"/internal/business/activation/apply", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return business.ErrActivationSource
|
||||
}
|
||||
stamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
mac := hmac.New(sha256.New, []byte(cfg.BusinessOA.Key))
|
||||
mac.Write([]byte(req.Method + "\n" + req.URL.EscapedPath() + "\n" + stamp + "\n"))
|
||||
mac.Write(body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Business-Timestamp", stamp)
|
||||
req.Header.Set("X-Business-Signature", hex.EncodeToString(mac.Sum(nil)))
|
||||
res, err := g.client.Do(req)
|
||||
if err != nil {
|
||||
return business.ErrActivationSource
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return business.ErrActivationSource
|
||||
}
|
||||
var result struct {
|
||||
Code *int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(res.Body, 65536)).Decode(&result); err != nil || result.Code == nil {
|
||||
return business.ErrActivationSource
|
||||
}
|
||||
if *result.Code != 0 {
|
||||
return errors.BadRequest("SW_ACTIVATION_APPLY", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package oa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"kra/internal/biz/business"
|
||||
"kra/internal/config"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestActivationGatewayPreservesIDsAndSignsOAPath(t *testing.T) {
|
||||
key := "test-key"
|
||||
calls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
mac := hmac.New(sha256.New, []byte(key))
|
||||
mac.Write([]byte(r.Method + "\n" + r.URL.EscapedPath() + "\n" + r.Header.Get("X-Business-Timestamp") + "\n"))
|
||||
mac.Write(body)
|
||||
if hex.EncodeToString(mac.Sum(nil)) != r.Header.Get("X-Business-Signature") || r.URL.Path != "/oa/internal/business/activation/apply" {
|
||||
t.Error("invalid signature/path")
|
||||
}
|
||||
var values map[string]string
|
||||
if err := json.Unmarshal(body, &values); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if values["poolId"] != "9007199254740993" || values["busierId"] != "42" || values["userId"] != "7" {
|
||||
t.Errorf("identity precision: %v", values)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
io.WriteString(w, `{"code":0,"msg":"申请成功"}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
store := config.NewStore(&config.Config{BusinessOA: &config.BusinessOA{URL: server.URL + "/oa/", Key: key}})
|
||||
if err := NewActivationGateway(store).Apply(context.Background(), business.ActivationApplication{UserID: 7, PoolID: 9007199254740993, BusierID: 42, SourceDB: "lx"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store.Replace(&config.Config{})
|
||||
if err := NewActivationGateway(store).Apply(context.Background(), business.ActivationApplication{}); err != business.ErrActivationSource {
|
||||
t.Fatal("missing configuration must fail closed")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatal("unexpected request")
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"kra/internal/integration/email"
|
||||
"kra/internal/integration/gameplatform"
|
||||
mqintegration "kra/internal/integration/mq"
|
||||
"kra/internal/integration/oa"
|
||||
paymentintegration "kra/internal/integration/payment"
|
||||
"kra/internal/integration/sms"
|
||||
"kra/internal/integration/storage"
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
)
|
||||
|
||||
var ProviderSet = wire.NewSet(
|
||||
oa.NewActivationGateway,
|
||||
gameplatform.NewPasswords,
|
||||
gameplatform.NewPasswordEncoder,
|
||||
gameplatform.NewPasswordSMS,
|
||||
|
||||
Reference in New Issue
Block a user