2020-08-04 00:51:19 +08:00
|
|
|
|
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"
|
2020-08-06 15:41:02 +08:00
|
|
|
|
"net/http"
|
2020-08-04 00:51:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Signer interface {
|
|
|
|
|
GetName() string
|
2020-08-06 10:35:24 +08:00
|
|
|
|
GetAccessKeyId() (string, error)
|
2020-08-06 12:15:51 +08:00
|
|
|
|
GetAccessKeyFrom() (string, error)
|
2020-08-04 00:51:19 +08:00
|
|
|
|
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)
|
2020-08-05 17:01:10 +08:00
|
|
|
|
case *credentials.BaseCredential:
|
|
|
|
|
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential())
|
2020-08-08 15:09:58 +08:00
|
|
|
|
case *credentials.StdTokenCredential:
|
|
|
|
|
signer = signers.NewStsTokenSigner(instance)
|
2020-09-23 12:14:23 +08:00
|
|
|
|
case *credentials.AliAppcodeCredential:
|
|
|
|
|
signer = signers.NewAliAppcodeSigner(instance)
|
2020-08-04 00:51:19 +08:00
|
|
|
|
default:
|
|
|
|
|
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 11:22:37 +08:00
|
|
|
|
func Sign(request requests.AcsRequest, signer Signer) (err error) {
|
2020-09-23 12:14:23 +08:00
|
|
|
|
switch signer.(type) {
|
|
|
|
|
case *signers.AliAppcodeSigner:
|
|
|
|
|
err = signRaliRequest(request, signer)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 11:22:37 +08:00
|
|
|
|
//TODO 根据rpc和roa两种风格签名,自行选择
|
2020-09-23 12:14:23 +08:00
|
|
|
|
switch request.GetStyle() {
|
|
|
|
|
case requests.RPC:
|
|
|
|
|
err = signRpcRequest(request, signer)
|
|
|
|
|
case requests.ROA:
|
|
|
|
|
err = signRoaRequest(request, signer)
|
|
|
|
|
}
|
2020-08-04 11:22:37 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-08-06 15:41:02 +08:00
|
|
|
|
|
|
|
|
|
func UnSign(request *http.Request, signer Signer) (err error) {
|
|
|
|
|
//TODO 根据rpc和roa两种风格签名,自行选择
|
|
|
|
|
err = unsignRpcRequest(request, signer)
|
|
|
|
|
return
|
|
|
|
|
}
|