init project
This commit is contained in:
parent
13ab011852
commit
2807474003
@ -1,10 +1,13 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
|
||||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 {
|
||||
return
|
||||
}
|
||||
if _, isContainsSign := request.GetQueryParams()["Sign"]; isContainsSign {
|
||||
delete(request.GetQueryParams(), "Sign")
|
||||
if _, isContainsSign := request.GetQueryParams()["sign"]; isContainsSign {
|
||||
delete(request.GetQueryParams(), "sign")
|
||||
}
|
||||
|
||||
stringToSign := buildHttpStringToSign(request)
|
||||
request.SetStringToSign(stringToSign)
|
||||
signature := signer.Sign(stringToSign, "&")
|
||||
request.GetQueryParams()["Sign"] = signature
|
||||
request.GetQueryParams()["sign"] = signature
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
type Signer interface {
|
||||
GetName() string
|
||||
GetAccessKeyId() (string, error)
|
||||
Sign(stringToSign, secretSuffix string) string
|
||||
}
|
||||
|
||||
|
@ -11,23 +11,24 @@ type AccessKeySigner struct {
|
||||
credential *credentials.AccessKeyCredential
|
||||
}
|
||||
|
||||
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
|
||||
return &AccessKeySigner{
|
||||
credential: credential,
|
||||
func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
|
||||
return signer.credential.AccessKeyId, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (*AccessKeySigner) GetName() string {
|
||||
return "HMAC-SHA1"
|
||||
}
|
||||
|
||||
|
||||
func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string {
|
||||
secret := signer.credential.AccessKeySecret + secretSuffix
|
||||
return ShaHmac1(stringToSign, secret)
|
||||
}
|
||||
|
||||
func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner {
|
||||
return &AccessKeySigner{
|
||||
credential: credential,
|
||||
}
|
||||
}
|
||||
|
||||
func ShaHmac1(source, secret string) string {
|
||||
key := []byte(secret)
|
||||
@ -37,5 +38,3 @@ func ShaHmac1(source, secret string) string {
|
||||
signedString := base64.StdEncoding.EncodeToString(signedBytes)
|
||||
return signedString
|
||||
}
|
||||
|
||||
|
||||
|
@ -35,6 +35,7 @@ type Client struct {
|
||||
httpProxy string
|
||||
httpsProxy string
|
||||
noProxy string
|
||||
sourceFrom string
|
||||
}
|
||||
|
||||
func (client *Client) GetNoProxy() string {
|
||||
@ -80,13 +81,14 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source str
|
||||
AccessKeyId: accessKeyId,
|
||||
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.config = config
|
||||
client.sourceFrom = source
|
||||
|
||||
if config.Transport != nil {
|
||||
client.httpClient.Transport = config.Transport
|
||||
@ -277,6 +279,7 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
|
||||
}
|
||||
userAgent := DefaultUserAgent
|
||||
httpRequest.Header.Set("User-Agent", userAgent)
|
||||
httpRequest.Header.Set("Gr-Sdk-From", client.sourceFrom)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,11 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -12,6 +16,9 @@ const (
|
||||
HTTP = "HTTP"
|
||||
HTTPS = "HTTPS"
|
||||
|
||||
JSON = "JSON"
|
||||
XML = "XML"
|
||||
|
||||
DefaultHttpPort = "80"
|
||||
|
||||
GET = "GET"
|
||||
@ -55,6 +62,7 @@ type AcsRequest interface {
|
||||
BuildQueries() string
|
||||
|
||||
SetScheme(scheme string)
|
||||
SetContent(content []byte)
|
||||
SetDomain(host string)
|
||||
SetStringToSign(stringToSign string)
|
||||
GetBodyReader() io.Reader
|
||||
@ -90,6 +98,10 @@ type baseRequest struct {
|
||||
stringToSign string
|
||||
}
|
||||
|
||||
func (request *baseRequest) SetContent(content []byte) {
|
||||
request.Content = content
|
||||
}
|
||||
|
||||
func (request *baseRequest) GetAcceptFormat() string {
|
||||
return request.AcceptFormat
|
||||
}
|
||||
@ -173,13 +185,13 @@ func (request *baseRequest) addFormParam(key, val string) {
|
||||
func defaultBaseRequest() (request *baseRequest) {
|
||||
request = &baseRequest{
|
||||
Scheme: HTTP,
|
||||
AcceptFormat: "JSON",
|
||||
AcceptFormat: JSON,
|
||||
Method: GET,
|
||||
QueryParams: make(map[string]string),
|
||||
Headers: map[string]string{
|
||||
"x-sdk-client": "golang/1.0.0",
|
||||
"x-sdk-invoke-type": "normal",
|
||||
"Accept-Encoding": "identity",
|
||||
"gr-sdk-client": "golang/1.14",
|
||||
"gr-sdk-invoke-type": "normal",
|
||||
"Accept-Encoding": Json,
|
||||
},
|
||||
FormParams: make(map[string]string),
|
||||
}
|
||||
@ -187,5 +199,33 @@ func defaultBaseRequest() (request *baseRequest) {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -7,9 +7,10 @@ import (
|
||||
|
||||
type SendSmsRequest struct {
|
||||
*requests.HttpRequest
|
||||
User string `position:"Query" name:"User" default:""`
|
||||
Code string `position:"Query" name:"Code" default:"-"`
|
||||
Params string `position:"Query" name:"Prams" default:"-"`
|
||||
User string `position:"Query" field:"user" default:"-" `
|
||||
Code string `position:"Query" field:"code" default:"-" `
|
||||
Params string `position:"Query" field:"params" default:"-" `
|
||||
Test string `position:"Body" field:"test" default:"-" `
|
||||
}
|
||||
|
||||
type SendSmsResponseData struct {
|
||||
@ -26,7 +27,10 @@ func CreateSendSmsRequest() (req *SendSmsRequest) {
|
||||
req = &SendSmsRequest{
|
||||
HttpRequest: &requests.HttpRequest{},
|
||||
}
|
||||
|
||||
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send")
|
||||
req.Method = requests.POST
|
||||
req.Scheme = requests.HTTP
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user