7
0

init project

This commit is contained in:
liangzy 2020-08-06 15:41:02 +08:00
parent 0feabe2c1f
commit d97f22ea0f
8 changed files with 109 additions and 11 deletions

View File

@ -2,10 +2,13 @@ package auth
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -54,8 +57,11 @@ func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
for key, value := range request.GetQueryParams() { for key, value := range request.GetQueryParams() {
signParams[key] = value 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) stringToSign = utils.GetUrlFormedMap(signParams)
@ -66,3 +72,41 @@ func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
stringToSign = request.GetMethod() + "&%2F&" + stringToSign stringToSign = request.GetMethod() + "&%2F&" + stringToSign
return 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/auth/signers"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
"net/http"
) )
type Signer interface { type Signer interface {
@ -32,3 +33,9 @@ func Sign(request requests.AcsRequest, signer Signer) (err error) {
err = signRpcRequest(request, signer) err = signRpcRequest(request, signer)
return 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 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 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 { type Client struct {
Host string Host string
httpClient *http.Client httpClient *http.Client
@ -36,7 +42,6 @@ type Client struct {
httpProxy string httpProxy string
httpsProxy string httpsProxy string
noProxy string noProxy string
debug utils.Debug
} }
func (client *Client) GetNoProxy() string { 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) { func (client *Client) InitWithOptions(config *Config, credential auth.Credential) (err error) {
client.httpClient = &http.Client{} client.httpClient = &http.Client{}
client.config = config client.config = config
client.debug = utils.Init("sdk", true)
if config.Transport != nil { if config.Transport != nil {
client.httpClient.Transport = config.Transport client.httpClient.Transport = config.Transport
@ -281,7 +285,8 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
userAgent := DefaultUserAgent userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent) httpRequest.Header.Set("User-Agent", userAgent)
client.debug("%s", request.GetStringToSign()) debug("%s", request.GetStringToSign())
debug("%s", httpRequest.Method)
return return
} }

View File

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

View File

@ -1,6 +1,10 @@
package utils package utils
import "fmt" import (
"fmt"
"os"
"strings"
)
type Debug func(format string, v ...interface{}) type Debug func(format string, v ...interface{})
@ -8,7 +12,21 @@ var hookPrint = func(input string) {
fmt.Println(input) 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{}) { return func(format string, v ...interface{}) {
if enable { if enable {

View File

@ -3,7 +3,7 @@ package jedi
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
const ( const (
HOST = "jedi.gaore.com" HOST = "jedi"
VERSION = "2020-08-04" VERSION = "2020-08-04"
) )

View File

@ -0,0 +1,15 @@
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
}
type DemoTestResponse struct {
*responses.BaseResponse
Data interface{} `json:"data"`
}

View File

@ -7,9 +7,10 @@ import (
type SendSmsRequest struct { type SendSmsRequest struct {
*requests.RpcRequest *requests.RpcRequest
User string `position:"Query" field:"user" default:"" ` User string `position:"Query" field:"user" default:"" `
Code string `position:"Query" field:"code" default:"" ` Code string `position:"Query" field:"code" default:"" `
Params string `position:"Query" field:"params" default:"" ` Params string `position:"Query" field:"params" default:"" `
TestBody string `position:"Body" field:"test_body" default:"2121" `
} }
type SendSmsResponseData struct { type SendSmsResponseData struct {