2020-08-04 00:51:19 +08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-08-06 10:35:24 +08:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-08-04 00:51:19 +08:00
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
|
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-08-06 10:35:24 +08:00
|
|
|
"time"
|
2020-08-04 00:51:19 +08:00
|
|
|
)
|
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
func signRpcRequest(request requests.AcsRequest, signer Signer) (err error) {
|
|
|
|
err = completeRpcSignParams(request, signer)
|
2020-08-04 11:22:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 10:35:24 +08:00
|
|
|
if _, isContainsSign := request.GetQueryParams()["sign"]; isContainsSign {
|
|
|
|
delete(request.GetQueryParams(), "sign")
|
2020-08-04 11:22:37 +08:00
|
|
|
}
|
2020-08-06 10:35:24 +08:00
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
stringToSign := buildRpcStringToSign(request)
|
2020-08-04 11:22:37 +08:00
|
|
|
request.SetStringToSign(stringToSign)
|
|
|
|
signature := signer.Sign(stringToSign, "&")
|
2020-08-06 10:35:24 +08:00
|
|
|
request.GetQueryParams()["sign"] = signature
|
2020-08-04 11:22:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
func completeRpcSignParams(request requests.AcsRequest, signer Signer) (err error) {
|
|
|
|
|
|
|
|
var accessKeyFrom string
|
|
|
|
if accessKeyFrom, err = signer.GetAccessKeyFrom(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:35:24 +08:00
|
|
|
queryParams := request.GetQueryParams()
|
2020-08-06 12:15:51 +08:00
|
|
|
queryParams["access_time"] = fmt.Sprintf("%d", time.Now().Unix())
|
2020-08-06 10:35:24 +08:00
|
|
|
queryParams["access_key"], err = signer.GetAccessKeyId()
|
2020-08-06 12:15:51 +08:00
|
|
|
queryParams["access_from"] = accessKeyFrom
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 10:35:24 +08:00
|
|
|
|
|
|
|
request.GetHeaders()["Content-type"] = requests.Form
|
2020-08-06 12:15:51 +08:00
|
|
|
request.GetHeaders()["Gr-Sdk-From"] = accessKeyFrom
|
2020-08-06 10:35:24 +08:00
|
|
|
formString := utils.GetUrlFormedMap(request.GetFormParams())
|
|
|
|
request.SetContent(bytes.NewBufferString(formString).Bytes())
|
2020-08-04 11:22:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
|
2020-08-04 00:51:19 +08:00
|
|
|
signParams := make(map[string]string)
|
|
|
|
for key, value := range request.GetQueryParams() {
|
|
|
|
signParams[key] = value
|
|
|
|
}
|
|
|
|
for key, value := range request.GetFormParams() {
|
|
|
|
signParams[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
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.GetMethod() + "&%2F&" + stringToSign
|
|
|
|
return
|
|
|
|
}
|