Compare commits
3 Commits
f038071d28
...
c2428de635
Author | SHA1 | Date | |
---|---|---|---|
|
c2428de635 | ||
|
4a94aa8155 | ||
|
e7a20f306b |
15
sdk/auth/credentials/sts_token_credential.go
Normal file
15
sdk/auth/credentials/sts_token_credential.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package credentials
|
||||||
|
|
||||||
|
type StdTokenCredential struct {
|
||||||
|
AccessKeyId string
|
||||||
|
AccessKeySecret string
|
||||||
|
AccessKeyFrom string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStsTokenCredential(accessKeyId, accessKeySecret, accessFrom string) *StdTokenCredential {
|
||||||
|
return &StdTokenCredential{
|
||||||
|
AccessKeyId: accessKeyId,
|
||||||
|
AccessKeySecret: accessKeySecret,
|
||||||
|
AccessKeyFrom: accessFrom,
|
||||||
|
}
|
||||||
|
}
|
@ -26,6 +26,9 @@ func signRpcRequest(request requests.AcsRequest, signer Signer) (err error) {
|
|||||||
request.SetStringToSign(stringToSign)
|
request.SetStringToSign(stringToSign)
|
||||||
signature := signer.Sign(stringToSign, "&")
|
signature := signer.Sign(stringToSign, "&")
|
||||||
request.GetQueryParams()["sign"] = signature
|
request.GetQueryParams()["sign"] = signature
|
||||||
|
debug("GrSdk sign: %s", signature)
|
||||||
|
debug("GrSdk sign string: %s", stringToSign)
|
||||||
|
debug("GrSdk sign: \r\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +109,7 @@ func unsignRpcRequest(request *http.Request, signer Signer) (err error) {
|
|||||||
stringToSign = strings.Replace(stringToSign, "%7E", "~", -1)
|
stringToSign = strings.Replace(stringToSign, "%7E", "~", -1)
|
||||||
stringToSign = url.QueryEscape(stringToSign)
|
stringToSign = url.QueryEscape(stringToSign)
|
||||||
stringToSign = request.Method + "&%2F&" + stringToSign
|
stringToSign = request.Method + "&%2F&" + stringToSign
|
||||||
debug("grsdk singstring %s", stringToSign)
|
debug("GrSdk sign: %s", stringToSign)
|
||||||
|
|
||||||
if timestamp, err := strconv.ParseInt(signParams["access_time"], 10, 64); err != nil {
|
if timestamp, err := strconv.ParseInt(signParams["access_time"], 10, 64); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -22,6 +22,8 @@ func NewSignerWithCredential(credential Credential, commonApi func(request *requ
|
|||||||
signer = signers.NewAccessKeySigner(instance)
|
signer = signers.NewAccessKeySigner(instance)
|
||||||
case *credentials.BaseCredential:
|
case *credentials.BaseCredential:
|
||||||
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential())
|
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential())
|
||||||
|
case *credentials.StdTokenCredential:
|
||||||
|
signer = signers.NewStsTokenSigner(instance)
|
||||||
default:
|
default:
|
||||||
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential")
|
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential")
|
||||||
}
|
}
|
||||||
|
41
sdk/auth/signers/sts_token_signer.go
Normal file
41
sdk/auth/signers/sts_token_signer.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package signers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StsTokenSigner struct {
|
||||||
|
credential *credentials.StdTokenCredential
|
||||||
|
}
|
||||||
|
|
||||||
|
func (signer *StsTokenSigner) GetAccessKeyId() (string, error) {
|
||||||
|
return signer.credential.AccessKeyId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (signer *StsTokenSigner) GetAccessKeyFrom() (string, error) {
|
||||||
|
return signer.credential.AccessKeyFrom, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*StsTokenSigner) GetName() string {
|
||||||
|
return "MD5"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (signer *StsTokenSigner) Sign(stringToSign, secretSuffix string) string {
|
||||||
|
secret := signer.credential.AccessKeySecret + secretSuffix
|
||||||
|
return Md5(stringToSign, secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStsTokenSigner(credential *credentials.StdTokenCredential) *StsTokenSigner {
|
||||||
|
return &StsTokenSigner{
|
||||||
|
credential: credential,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Md5(source, secret string) string {
|
||||||
|
data := []byte(fmt.Sprintf("%s##%s", secret, source))
|
||||||
|
has := md5.Sum(data)
|
||||||
|
md5str := fmt.Sprintf("%x", has)
|
||||||
|
return md5str
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package sdk
|
package sdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -11,6 +12,7 @@ import (
|
|||||||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -91,6 +93,16 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyF
|
|||||||
return client.InitWithOptions(config, credential)
|
return client.InitWithOptions(config, credential)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) InitWithStsToken(accessKeyId, accessKeySecret, accessKeyFrom string) (err error) {
|
||||||
|
config := client.InitWithConfig()
|
||||||
|
credential := &credentials.StdTokenCredential{
|
||||||
|
AccessKeyId: accessKeyId,
|
||||||
|
AccessKeySecret: accessKeySecret,
|
||||||
|
AccessKeyFrom: accessKeyFrom,
|
||||||
|
}
|
||||||
|
return client.InitWithOptions(config, credential)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
@ -227,8 +239,10 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r
|
|||||||
client.httpClient.Transport = trans
|
client.httpClient.Transport = trans
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpResponse *http.Response
|
dump, err := httputil.DumpRequest(httpRequest, true)
|
||||||
|
debug("client %s", bytes.NewBuffer(dump).String())
|
||||||
|
|
||||||
|
var httpResponse *http.Response
|
||||||
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
|
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -285,8 +299,6 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
|
|||||||
userAgent := DefaultUserAgent
|
userAgent := DefaultUserAgent
|
||||||
httpRequest.Header.Set("User-Agent", userAgent)
|
httpRequest.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
debug("%s", request.GetStringToSign())
|
|
||||||
debug("%s", httpRequest.Method)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,8 +202,8 @@ func defaultBaseRequest() (request *baseRequest) {
|
|||||||
Method: GET,
|
Method: GET,
|
||||||
QueryParams: make(map[string]string),
|
QueryParams: make(map[string]string),
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"gr-sdk-client": "golang/1.14",
|
"Gr-Sdk-Client": "golang/1.14",
|
||||||
"gr-sdk-invoke-type": "normal",
|
"Gr-Sdk-Invoke-Type": "normal",
|
||||||
"Accept-Encoding": Json,
|
"Accept-Encoding": Json,
|
||||||
},
|
},
|
||||||
FormParams: make(map[string]string),
|
FormParams: make(map[string]string),
|
||||||
|
@ -7,10 +7,9 @@ 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 {
|
||||||
|
@ -7,15 +7,16 @@ import (
|
|||||||
|
|
||||||
type DemoTestRequest struct {
|
type DemoTestRequest struct {
|
||||||
*requests.RpcRequest
|
*requests.RpcRequest
|
||||||
Param1 string `position:"Query" field:"param_1" default:"" `
|
Param1 string `position:"Query" field:"param_1" default:"" `
|
||||||
Param2 int `position:"Query" field:"param_2" default:"10086" `
|
Param2 int `position:"Query" field:"param_2" default:"10086" `
|
||||||
Param3 bool `position:"Query" field:"param_3" default:"false" `
|
Param3 bool `position:"Query" field:"param_3" default:"false" `
|
||||||
|
ParamBody string `position:"Body" field:"param_body" default:"foobar" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDemoTestRequest() (req *DemoTestRequest) {
|
func CreateDemoTestRequest() (req *DemoTestRequest) {
|
||||||
req = &DemoTestRequest{RpcRequest: &requests.RpcRequest{}}
|
req = &DemoTestRequest{RpcRequest: &requests.RpcRequest{}}
|
||||||
req.InitWithApiInfo(HOST, VERSION, "/api/sms/Index")
|
req.InitWithApiInfo(HOST, VERSION, "/api/sms/Index")
|
||||||
req.Method = requests.GET
|
req.Method = requests.POST
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user