signer.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) (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.StsCredential:
  40. {
  41. signer, err = signers.NewSignerSts(instance)
  42. }
  43. case *credentials.StsAssumeRoleCredential:
  44. {
  45. signer, err = signers.NewSignerStsAssumeRole(instance, commonApi)
  46. }
  47. default:
  48. message := fmt.Sprintf(errors.UnsupportedCredentialMessage, reflect.TypeOf(credential))
  49. err = errors.NewClientError(errors.UnsupportedCredentialCode, message, nil)
  50. }
  51. return
  52. }
  53. func Sign(request requests.AcsRequest, signer Signer, regionId string) (err error) {
  54. switch instance := request.(type) {
  55. case *requests.RoaRequest:
  56. {
  57. signRoaRequest(instance, signer, regionId)
  58. }
  59. case *requests.RpcRequest:
  60. {
  61. signRpcRequest(instance, signer, regionId)
  62. }
  63. case *requests.CommonRequest:
  64. {
  65. err = Sign(instance.Ontology, signer, regionId)
  66. }
  67. default:
  68. message := fmt.Sprintf(errors.UnknownRequestTypeMessage, reflect.TypeOf(request))
  69. err = errors.NewClientError(errors.UnknownRequestTypeCode, message, nil)
  70. }
  71. return
  72. }