signer.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package auth
  15. import (
  16. "fmt"
  17. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/signers"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  22. "reflect"
  23. )
  24. type Signer interface {
  25. GetName() string
  26. GetType() string
  27. GetVersion() string
  28. GetAccessKeyId() string
  29. GetExtraParam() map[string]string
  30. Sign(stringToSign, secretSuffix string) string
  31. Shutdown()
  32. }
  33. func NewSignerWithCredential(credential Credential, commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)) (signer Signer, err error) {
  34. switch instance := credential.(type) {
  35. case *credentials.BaseCredential:
  36. {
  37. signer, err = signers.NewSignerV1(instance)
  38. }
  39. case *credentials.StsTokenCredential:
  40. {
  41. signer, err = signers.NewSignerSts(instance)
  42. }
  43. case *credentials.StsRoleArnCredential:
  44. {
  45. signer, err = signers.NewSignerStsAssumeRole(instance, commonApi)
  46. }
  47. case *credentials.RsaKeyPairCredential:
  48. {
  49. signer, err = signers.NewSignerKeyPair(instance, commonApi)
  50. }
  51. case *credentials.StsRoleNameOnEcsCredential:
  52. {
  53. signer, err = signers.NewSignereEcsInstance(instance, commonApi)
  54. }
  55. default:
  56. message := fmt.Sprintf(errors.UnsupportedCredentialErrorMessage, reflect.TypeOf(credential))
  57. err = errors.NewClientError(errors.UnsupportedCredentialErrorCode, message, nil)
  58. }
  59. return
  60. }
  61. func Sign(request requests.AcsRequest, signer Signer, regionId string) (err error) {
  62. switch request.GetStyle() {
  63. case requests.ROA:
  64. {
  65. signRoaRequest(request, signer, regionId)
  66. }
  67. case requests.RPC:
  68. {
  69. signRpcRequest(request, signer, regionId)
  70. }
  71. default:
  72. message := fmt.Sprintf(errors.UnknownRequestTypeErrorMessage, reflect.TypeOf(request))
  73. err = errors.NewClientError(errors.UnknownRequestTypeErrorCode, message, nil)
  74. }
  75. return
  76. }