7
0

init project

This commit is contained in:
liangzy 2020-08-06 10:35:24 +08:00
parent 13ab011852
commit 2807474003
6 changed files with 79 additions and 21 deletions

View File

@ -1,10 +1,13 @@
package auth package auth
import ( import (
"bytes"
"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/url" "net/url"
"strings" "strings"
"time"
) )
func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) { func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) {
@ -12,17 +15,25 @@ func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) {
if err != nil { if err != nil {
return return
} }
if _, isContainsSign := request.GetQueryParams()["Sign"]; isContainsSign { if _, isContainsSign := request.GetQueryParams()["sign"]; isContainsSign {
delete(request.GetQueryParams(), "Sign") delete(request.GetQueryParams(), "sign")
} }
stringToSign := buildHttpStringToSign(request) stringToSign := buildHttpStringToSign(request)
request.SetStringToSign(stringToSign) request.SetStringToSign(stringToSign)
signature := signer.Sign(stringToSign, "&") signature := signer.Sign(stringToSign, "&")
request.GetQueryParams()["Sign"] = signature request.GetQueryParams()["sign"] = signature
return return
} }
func completeHttpSignParams(requests requests.AcsRequest, signer Signer) (err error) { func completeHttpSignParams(request requests.AcsRequest, signer Signer) (err error) {
queryParams := request.GetQueryParams()
queryParams["timestamp"] = fmt.Sprintf("%d", time.Now().Unix())
queryParams["access_key"], err = signer.GetAccessKeyId()
request.GetHeaders()["Content-type"] = requests.Form
formString := utils.GetUrlFormedMap(request.GetFormParams())
request.SetContent(bytes.NewBufferString(formString).Bytes())
return return
} }

View File

@ -10,6 +10,7 @@ import (
type Signer interface { type Signer interface {
GetName() string GetName() string
GetAccessKeyId() (string, error)
Sign(stringToSign, secretSuffix string) string Sign(stringToSign, secretSuffix string) string
} }

View File

@ -11,23 +11,24 @@ type AccessKeySigner struct {
credential *credentials.AccessKeyCredential credential *credentials.AccessKeyCredential
} }
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner { func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
return &AccessKeySigner{ return signer.credential.AccessKeyId, nil
credential: credential,
}
} }
func (*AccessKeySigner) GetName() string { func (*AccessKeySigner) GetName() string {
return "HMAC-SHA1" return "HMAC-SHA1"
} }
func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string { func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.credential.AccessKeySecret + secretSuffix secret := signer.credential.AccessKeySecret + secretSuffix
return ShaHmac1(stringToSign, secret) return ShaHmac1(stringToSign, secret)
} }
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
return &AccessKeySigner{
credential: credential,
}
}
func ShaHmac1(source, secret string) string { func ShaHmac1(source, secret string) string {
key := []byte(secret) key := []byte(secret)
@ -37,5 +38,3 @@ func ShaHmac1(source, secret string) string {
signedString := base64.StdEncoding.EncodeToString(signedBytes) signedString := base64.StdEncoding.EncodeToString(signedBytes)
return signedString return signedString
} }

View File

@ -35,6 +35,7 @@ type Client struct {
httpProxy string httpProxy string
httpsProxy string httpsProxy string
noProxy string noProxy string
sourceFrom string
} }
func (client *Client) GetNoProxy() string { func (client *Client) GetNoProxy() string {
@ -80,13 +81,14 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source str
AccessKeyId: accessKeyId, AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret, AccessKeySecret: accessKeySecret,
} }
return client.InitWithOptions("", config, credential) return client.InitWithOptions(source, config, credential)
} }
func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { func (client *Client) InitWithOptions(source string, config *Config, credential auth.Credential) (err error) {
client.httpClient = &http.Client{} client.httpClient = &http.Client{}
client.config = config client.config = config
client.sourceFrom = source
if config.Transport != nil { if config.Transport != nil {
client.httpClient.Transport = config.Transport client.httpClient.Transport = config.Transport
@ -277,6 +279,7 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
} }
userAgent := DefaultUserAgent userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent) httpRequest.Header.Set("User-Agent", userAgent)
httpRequest.Header.Set("Gr-Sdk-From", client.sourceFrom)
return return
} }

View File

@ -1,7 +1,11 @@
package requests package requests
import ( import (
"errors"
"fmt"
"io" "io"
"log"
"reflect"
"time" "time"
) )
@ -12,6 +16,9 @@ const (
HTTP = "HTTP" HTTP = "HTTP"
HTTPS = "HTTPS" HTTPS = "HTTPS"
JSON = "JSON"
XML = "XML"
DefaultHttpPort = "80" DefaultHttpPort = "80"
GET = "GET" GET = "GET"
@ -55,6 +62,7 @@ type AcsRequest interface {
BuildQueries() string BuildQueries() string
SetScheme(scheme string) SetScheme(scheme string)
SetContent(content []byte)
SetDomain(host string) SetDomain(host string)
SetStringToSign(stringToSign string) SetStringToSign(stringToSign string)
GetBodyReader() io.Reader GetBodyReader() io.Reader
@ -90,6 +98,10 @@ type baseRequest struct {
stringToSign string stringToSign string
} }
func (request *baseRequest) SetContent(content []byte) {
request.Content = content
}
func (request *baseRequest) GetAcceptFormat() string { func (request *baseRequest) GetAcceptFormat() string {
return request.AcceptFormat return request.AcceptFormat
} }
@ -173,13 +185,13 @@ func (request *baseRequest) addFormParam(key, val string) {
func defaultBaseRequest() (request *baseRequest) { func defaultBaseRequest() (request *baseRequest) {
request = &baseRequest{ request = &baseRequest{
Scheme: HTTP, Scheme: HTTP,
AcceptFormat: "JSON", AcceptFormat: JSON,
Method: GET, Method: GET,
QueryParams: make(map[string]string), QueryParams: make(map[string]string),
Headers: map[string]string{ Headers: map[string]string{
"x-sdk-client": "golang/1.0.0", "gr-sdk-client": "golang/1.14",
"x-sdk-invoke-type": "normal", "gr-sdk-invoke-type": "normal",
"Accept-Encoding": "identity", "Accept-Encoding": Json,
}, },
FormParams: make(map[string]string), FormParams: make(map[string]string),
} }
@ -187,5 +199,33 @@ func defaultBaseRequest() (request *baseRequest) {
} }
func InitParam(request AcsRequest) (err error) { func InitParam(request AcsRequest) (err error) {
reflectValue := reflect.ValueOf(request).Elem()
reflectType := reflectValue.Type()
for i := 0; i < reflectType.NumField(); i++ {
field := reflectType.Field(i)
name, isContiansNameTag := field.Tag.Lookup("field")
fieldPosition, _ := field.Tag.Lookup("position")
log.Println(name, field)
if isContiansNameTag {
err = addParam(request, fieldPosition, name, reflectValue.Field(i).String())
}
}
return nil return nil
} }
func addParam(request AcsRequest, position, key, value string) (err error) {
if len(value) > 0 {
switch position {
case Header:
request.addHeaderParam(key, value)
case Query:
request.addQueryParam(key, value)
case Body:
request.addFormParam(key, value)
default:
errmsg := fmt.Sprintf("unsupport positions add param `%s`", position)
err = errors.New(errmsg)
}
}
return
}

View File

@ -7,9 +7,10 @@ import (
type SendSmsRequest struct { type SendSmsRequest struct {
*requests.HttpRequest *requests.HttpRequest
User string `position:"Query" name:"User" default:""` User string `position:"Query" field:"user" default:"-" `
Code string `position:"Query" name:"Code" default:"-"` Code string `position:"Query" field:"code" default:"-" `
Params string `position:"Query" name:"Prams" default:"-"` Params string `position:"Query" field:"params" default:"-" `
Test string `position:"Body" field:"test" default:"-" `
} }
type SendSmsResponseData struct { type SendSmsResponseData struct {
@ -26,7 +27,10 @@ func CreateSendSmsRequest() (req *SendSmsRequest) {
req = &SendSmsRequest{ req = &SendSmsRequest{
HttpRequest: &requests.HttpRequest{}, HttpRequest: &requests.HttpRequest{},
} }
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") req.InitWithApiInfo(HOST, VERSION, "/api/sms/send")
req.Method = requests.POST
req.Scheme = requests.HTTP
return return
} }