7
0
gaore-common-sdk-go/sdk/auth/signer.go
liangzy 69c6020214 1.增加roa请求,
2.增加阿里(rali) 验签请求加密方式
2020-09-23 12:14:23 +08:00

57 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package auth
import (
"errors"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/signers"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
"net/http"
)
type Signer interface {
GetName() string
GetAccessKeyId() (string, error)
GetAccessKeyFrom() (string, error)
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:
signer = signers.NewAccessKeySigner(instance)
case *credentials.BaseCredential:
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential())
case *credentials.StdTokenCredential:
signer = signers.NewStsTokenSigner(instance)
case *credentials.AliAppcodeCredential:
signer = signers.NewAliAppcodeSigner(instance)
default:
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential")
}
return
}
func Sign(request requests.AcsRequest, signer Signer) (err error) {
switch signer.(type) {
case *signers.AliAppcodeSigner:
err = signRaliRequest(request, signer)
return
}
//TODO 根据rpc和roa两种风格签名自行选择
switch request.GetStyle() {
case requests.RPC:
err = signRpcRequest(request, signer)
case requests.ROA:
err = signRoaRequest(request, signer)
}
return
}
func UnSign(request *http.Request, signer Signer) (err error) {
//TODO 根据rpc和roa两种风格签名自行选择
err = unsignRpcRequest(request, signer)
return
}