diff --git a/sdk/auth/http_build_param.go b/sdk/auth/http_build_param.go index c331384..a9ef168 100644 --- a/sdk/auth/http_build_param.go +++ b/sdk/auth/http_build_param.go @@ -7,7 +7,26 @@ import ( "strings" ) -func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) { +func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) { + err = completeHttpSignParams(request, signer) + if err != nil { + return + } + if _, isContainsSign := request.GetQueryParams()["Sign"]; isContainsSign { + delete(request.GetQueryParams(), "Sign") + } + stringToSign := buildHttpStringToSign(request) + request.SetStringToSign(stringToSign) + signature := signer.Sign(stringToSign, "&") + request.GetQueryParams()["Sign"] = signature + return +} + +func completeHttpSignParams(requests requests.AcsRequest, signer Signer) (err error) { + return +} + +func buildHttpStringToSign(request requests.AcsRequest) (stringToSign string) { signParams := make(map[string]string) for key, value := range request.GetQueryParams() { signParams[key] = value @@ -24,5 +43,3 @@ func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) { stringToSign = request.GetMethod() + "&%2F&" + stringToSign return } - - diff --git a/sdk/auth/signer.go b/sdk/auth/signer.go index 5c7f331..c1508e2 100644 --- a/sdk/auth/signer.go +++ b/sdk/auth/signer.go @@ -13,7 +13,6 @@ type Signer interface { Sign(stringToSign, secretSuffix string) string } - func NewSignerWithCredential(credential Credential, commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)) (signer Signer, err error) { switch instance := credential.(type) { case *credentials.AccessKeyCredential: @@ -24,9 +23,8 @@ func NewSignerWithCredential(credential Credential, commonApi func(request *requ return } - - -func Sign(request requests.AcsRequest, signer Signer, regionId string) (err error) { - - -} \ No newline at end of file +func Sign(request requests.AcsRequest, signer Signer) (err error) { + //TODO 根据rpc和roa两种风格签名,自行选择 + err = signHttpRequest(request, signer) + return +} diff --git a/sdk/client.go b/sdk/client.go index 043cfaf..f7f1e86 100644 --- a/sdk/client.go +++ b/sdk/client.go @@ -28,7 +28,7 @@ type Client struct { config *Config } -func (client *Client) InitWithOptions(host string, config *Config, creditial auth.Credential) (err error) { +func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { client.httpClient = &http.Client{} if config.Transport != nil { @@ -41,7 +41,6 @@ func (client *Client) InitWithOptions(host string, config *Config, creditial aut client.httpClient.Timeout = config.Timeout } - client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner) return nil @@ -55,7 +54,6 @@ func (client *Client) InitWithConfig() (config *Config) { } } - func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) { if signer, isSigner := signerInterface.(auth.Signer); isSigner { response = responses.NewCommonResponse() @@ -114,5 +112,5 @@ func (client *Client) DoAction(request requests.AcsRequest, response responses.A } func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) { - return nil + } diff --git a/sdk/requests/http_request.go b/sdk/requests/http_request.go index 53c93a6..44e87ad 100644 --- a/sdk/requests/http_request.go +++ b/sdk/requests/http_request.go @@ -3,3 +3,11 @@ package requests type HttpRequest struct { *baseRequest } + +func (request *HttpRequest) BuildUrl() string { + return "" +} + +func (request *HttpRequest) BuildQueries() string { + return "" +} diff --git a/sdk/requests/request.go b/sdk/requests/request.go index f720067..e6ae2cb 100644 --- a/sdk/requests/request.go +++ b/sdk/requests/request.go @@ -44,6 +44,14 @@ type AcsRequest interface { GetQueryParams() map[string]string GetFormParams() map[string]string GetMethod() string + GetScheme() string + GetDomain() string + + BuildUrl() string + BuildQueries() string + + SetScheme(scheme string) + SetDomain(host string) SetStringToSign(stringToSign string) } @@ -51,6 +59,7 @@ type baseRequest struct { Scheme string Method string Host string + Port string Domain string From string ReadTimeout time.Duration @@ -72,6 +81,22 @@ type baseRequest struct { stringToSign string } +func (request *baseRequest) SetScheme(scheme string) { + request.Scheme = scheme +} + +func (request *baseRequest) SetDomain(host string) { + request.Domain = host +} + +func (request *baseRequest) GetScheme() string { + return request.Scheme +} + +func (request *baseRequest) GetDomain() string { + return request.Domain +} + func (request *baseRequest) GetMethod() string { return request.Method } @@ -127,3 +152,7 @@ func defaultBaseRequest() (request *baseRequest) { } return } + +func InitParam(request AcsRequest) (err error) { + return nil +}