Skip to content

Commit

Permalink
feat: promote code format
Browse files Browse the repository at this point in the history
  • Loading branch information
dacongda committed Jan 14, 2025
1 parent fe2cfb3 commit ec3de64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion service/cucloud/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CuCloud

[Cu Cloud](https://www.cucloud.cn) is the cloud service brand of China Unicom
[CUCloud](https://www.cucloud.cn) is the cloud service brand of China Unicom

## Usage

Expand Down
30 changes: 25 additions & 5 deletions service/cucloud/cucloud.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2025 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cucloud

import (
Expand Down Expand Up @@ -67,14 +81,20 @@ func (c *CuCloud) Send(ctx context.Context, subject, content string) error {
reqBody["templateName"] = ""
reqBody["cloudRegionCode"] = "cn-langfang-2"

signVal := c.generateRequestSign(reqHeader, reqBody)
signVal, err := c.generateRequestSign(reqHeader, reqBody)
if err != nil {
return err
}
reqHeader["sign"] = signVal
reqHeader["Content-Type"] = "application/json"
reqHeader["Account-Id"] = c.AccountId
reqHeader["User-Id"] = c.AccountId
reqHeader["Region-Code"] = c.CloudRegionCode

bodyJson, _ := json.Marshal(reqBody)
bodyJson, err := json.Marshal(reqBody)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://gateway.cucloud.cn/smn/SMNService/api/message/notify", bytes.NewReader(bodyJson))
if err != nil {
return err
Expand Down Expand Up @@ -111,7 +131,7 @@ func (c *CuCloud) Send(ctx context.Context, subject, content string) error {
return nil
}

func (c *CuCloud) generateRequestSign(header map[string]string, body map[string]string) string {
func (c *CuCloud) generateRequestSign(header map[string]string, body map[string]string) (string, error) {
mac := hmac.New(sha256.New, []byte(c.SecretKey))
reqSignMap := make(map[string]string)
maps.Copy(reqSignMap, header)
Expand All @@ -128,12 +148,12 @@ func (c *CuCloud) generateRequestSign(header map[string]string, body map[string]
for _, k := range keys {
vJson, err := json.Marshal(reqSignMap[k])
if err != nil {
panic(err)
return "", err
}
signRawString += k + "=" + string(vJson) + "&"
}

signRawString = signRawString[:len(signRawString)-1]
mac.Write([]byte(signRawString))
return hex.EncodeToString(mac.Sum(nil))
return hex.EncodeToString(mac.Sum(nil)), nil
}

0 comments on commit ec3de64

Please sign in to comment.