2020-08-04 00:51:19 +08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-08-06 10:35:24 +08:00
|
|
|
"bytes"
|
2020-08-06 15:41:02 +08:00
|
|
|
"errors"
|
2020-08-06 10:35:24 +08:00
|
|
|
"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"
|
2020-08-06 15:41:02 +08:00
|
|
|
"net/http"
|
2020-08-04 00:51:19 +08:00
|
|
|
"net/url"
|
2020-08-06 15:41:02 +08:00
|
|
|
"strconv"
|
2020-08-04 00:51:19 +08:00
|
|
|
"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-07 19:51:19 +08:00
|
|
|
debug("grsdk sign %s", 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
|
|
|
|
}
|
2020-08-06 15:41:02 +08:00
|
|
|
|
|
|
|
if strings.ToUpper(request.GetMethod()) == requests.POST {
|
|
|
|
for key, value := range request.GetFormParams() {
|
|
|
|
signParams[key] = value
|
|
|
|
}
|
2020-08-04 00:51:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-06 15:41:02 +08:00
|
|
|
|
|
|
|
func unsignRpcRequest(request *http.Request, signer Signer) (err error) {
|
|
|
|
signParams := make(map[string]string)
|
|
|
|
for key, value := range request.URL.Query() {
|
|
|
|
signParams[key] = value[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToUpper(request.Method) == requests.POST {
|
|
|
|
for key, value := range request.Form {
|
|
|
|
signParams[key] = value[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:41:13 +08:00
|
|
|
if accessKey, err := signer.GetAccessKeyId(); err != nil {
|
2020-08-06 16:44:20 +08:00
|
|
|
return err
|
2020-08-06 16:41:13 +08:00
|
|
|
} else if accessKey == "" {
|
|
|
|
return errors.New("access key is not allow empty")
|
|
|
|
} else if accessKey != signParams["access_key"] {
|
|
|
|
return errors.New("illegal access key")
|
|
|
|
}
|
|
|
|
|
2020-08-06 15:41:02 +08:00
|
|
|
signValue, ok := signParams["sign"]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("sign value is not exists")
|
2020-08-06 16:25:08 +08:00
|
|
|
} else {
|
|
|
|
delete(signParams, "sign")
|
2020-08-06 15:41:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.Method + "&%2F&" + stringToSign
|
2020-08-07 19:51:19 +08:00
|
|
|
debug("grsdk signstring %s", stringToSign)
|
2020-08-06 15:41:02 +08:00
|
|
|
|
|
|
|
if timestamp, err := strconv.ParseInt(signParams["access_time"], 10, 64); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if time.Unix(timestamp, 0).Before(time.Now().Add(-5 * time.Minute)) {
|
|
|
|
err = errors.New("sign timeout 5 minute")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if signer.Sign(stringToSign, "&") != signValue {
|
|
|
|
return errors.New("sign string is not correct")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|