7
0

Compare commits

...

2 Commits

Author SHA1 Message Date
liangzy
9c2898517e init project 2020-08-06 16:04:44 +08:00
liangzy
d97f22ea0f init project 2020-08-06 15:41:02 +08:00
8 changed files with 142 additions and 13 deletions

View File

@ -2,10 +2,13 @@ package auth
import (
"bytes"
"errors"
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
@ -54,8 +57,11 @@ func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
for key, value := range request.GetQueryParams() {
signParams[key] = value
}
for key, value := range request.GetFormParams() {
signParams[key] = value
if strings.ToUpper(request.GetMethod()) == requests.POST {
for key, value := range request.GetFormParams() {
signParams[key] = value
}
}
stringToSign = utils.GetUrlFormedMap(signParams)
@ -66,3 +72,41 @@ func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
stringToSign = request.GetMethod() + "&%2F&" + stringToSign
return
}
func unsignRpcRequest(request *http.Request, signer Signer) (err error) {
signParams := make(map[string]string)
for key, value := range request.URL.Query() {
signParams[key] = value[0]
}
if strings.ToUpper(request.Method) == requests.POST {
for key, value := range request.Form {
signParams[key] = value[0]
}
}
signValue, ok := signParams["sign"]
if !ok {
return errors.New("sign value is not exists")
}
stringToSign := utils.GetUrlFormedMap(signParams)
stringToSign = strings.Replace(stringToSign, "+", "%20", -1)
stringToSign = strings.Replace(stringToSign, "*", "%2A", -1)
stringToSign = strings.Replace(stringToSign, "%7E", "~", -1)
stringToSign = url.QueryEscape(stringToSign)
stringToSign = request.Method + "&%2F&" + stringToSign
if timestamp, err := strconv.ParseInt(signParams["access_time"], 10, 64); err != nil {
return err
} else {
if time.Unix(timestamp, 0).Before(time.Now().Add(-5 * time.Minute)) {
err = errors.New("sign timeout 5 minute")
}
}
if signer.Sign(stringToSign, "&") != signValue {
return errors.New("sign string is not correct")
}
return
}

View File

@ -6,6 +6,7 @@ import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/signers"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
"net/http"
)
type Signer interface {
@ -32,3 +33,9 @@ func Sign(request requests.AcsRequest, signer Signer) (err error) {
err = signRpcRequest(request, signer)
return
}
func UnSign(request *http.Request, signer Signer) (err error) {
//TODO 根据rpc和roa两种风格签名自行选择
err = unsignRpcRequest(request, signer)
return
}

View File

@ -25,6 +25,12 @@ var defaultReadTimeout = 10 * time.Second
var defaultDomain = ".gaore.com"
var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)
var debug utils.Debug
func init() {
debug = utils.Init("sdk")
}
type Client struct {
Host string
httpClient *http.Client
@ -36,7 +42,6 @@ type Client struct {
httpProxy string
httpsProxy string
noProxy string
debug utils.Debug
}
func (client *Client) GetNoProxy() string {
@ -89,7 +94,6 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyF
func (client *Client) InitWithOptions(config *Config, credential auth.Credential) (err error) {
client.httpClient = &http.Client{}
client.config = config
client.debug = utils.Init("sdk", true)
if config.Transport != nil {
client.httpClient.Transport = config.Transport
@ -281,7 +285,8 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent)
client.debug("%s", request.GetStringToSign())
debug("%s", request.GetStringToSign())
debug("%s", httpRequest.Method)
return
}

View File

@ -3,6 +3,7 @@ package requests
import (
"errors"
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"math/cmplx"
"reflect"
@ -43,6 +44,12 @@ const (
HeaderSeparator = "\n"
)
var debug utils.Debug
func init() {
debug = utils.Init("request")
}
type AcsRequest interface {
GetReadTimeout() time.Duration
GetConnectTimeout() time.Duration
@ -222,6 +229,7 @@ func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position s
}
fieldDefault, _ := field.Tag.Lookup("default")
debug("%s %s %s", name, fieldPosition, fieldDefault)
if isContiansNameTag {
var value string
switch field.Type.Kind() {
@ -243,6 +251,10 @@ func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position s
value = fieldDefault
}
if value == "0" && fieldDefault != "" && fieldDefault != "0" {
value = fieldDefault
}
err = addParam(request, fieldPosition, name, value)
}
}

View File

@ -1,6 +1,10 @@
package utils
import "fmt"
import (
"fmt"
"os"
"strings"
)
type Debug func(format string, v ...interface{})
@ -8,7 +12,21 @@ var hookPrint = func(input string) {
fmt.Println(input)
}
func Init(flag string, enable bool) Debug {
var hookGetEnv = func() string {
return os.Getenv("DEBUG")
}
func Init(flag string) Debug {
enable := false
env := hookGetEnv()
parts := strings.Split(env, ",")
for _, part := range parts {
if part == flag {
enable = true
break
}
}
return func(format string, v ...interface{}) {
if enable {

View File

@ -3,7 +3,7 @@ package jedi
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
const (
HOST = "jedi.gaore.com"
HOST = "jedi"
VERSION = "2020-08-04"
)
@ -17,6 +17,12 @@ func (c *Client) SendSms(req *SendSmsRequest) (response *SendSmsResponse, err er
return
}
func (c *Client) Test(req *DemoTestRequest) (response *DemoTestResponse, err error) {
response = CreateDemoTestResponse()
err = c.DoAction(req, response)
return
}
func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) {
client = &Client{}
err = client.InitWithAccessKey(accesskey, secrect, source)

View File

@ -7,14 +7,15 @@ import (
type SendSmsRequest struct {
*requests.RpcRequest
User string `position:"Query" field:"user" default:"" `
Code string `position:"Query" field:"code" default:"" `
Params string `position:"Query" field:"params" default:"" `
User string `position:"Query" field:"user" default:"" `
Code string `position:"Query" field:"code" default:"" `
Params string `position:"Query" field:"params" default:"" `
TestBody string `position:"Body" field:"test_body" default:"2121" `
}
type SendSmsResponseData struct {
Account string `json:"account"`
Createtime int64 `json:"createtime"`
Account string `json:"account"`
Total int `json:"total"`
}
type SendSmsResponse struct {

View File

@ -0,0 +1,36 @@
package jedi
import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
)
type DemoTestRequest struct {
*requests.RpcRequest
Param1 string `position:"Query" field:"param_1" default:"" `
Param2 int `position:"Query" field:"param_2" default:"10086" `
Param3 bool `position:"Query" field:"param_3" default:"false" `
}
func CreateDemoTestRequest() (req *DemoTestRequest) {
req = &DemoTestRequest{RpcRequest: &requests.RpcRequest{}}
req.InitWithApiInfo(HOST, VERSION, "/api/sms/Index")
req.Method = requests.GET
return
}
type DemoTestResponse struct {
*responses.BaseResponse
Data DemoTestResponseData `json:"data"`
}
type DemoTestResponseData struct {
Account string `json:"account"`
Total int `json:"total"`
}
func CreateDemoTestResponse() *DemoTestResponse {
return &DemoTestResponse{
BaseResponse: &responses.BaseResponse{},
}
}