signer_key_pair.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 signers
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  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. "github.com/jmespath/go-jmespath"
  23. "net/http"
  24. "strconv"
  25. )
  26. type SignerKeyPair struct {
  27. *credentialUpdater
  28. sessionCredential *SessionCredential
  29. credential *credentials.RsaKeyPairCredential
  30. commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
  31. }
  32. func NewSignerKeyPair(credential *credentials.RsaKeyPairCredential, commonApi func(*requests.CommonRequest, interface{}) (response *responses.CommonResponse, err error)) (signer *SignerKeyPair, err error) {
  33. signer = &SignerKeyPair{
  34. credential: credential,
  35. commonApi: commonApi,
  36. }
  37. signer.credentialUpdater = &credentialUpdater{
  38. credentialExpiration: credential.SessionExpiration,
  39. buildRequestMethod: signer.buildCommonRequest,
  40. responseCallBack: signer.refreshCredential,
  41. refreshApi: signer.refreshApi,
  42. }
  43. if credential.SessionExpiration > 0 {
  44. if credential.SessionExpiration >= 900 && credential.SessionExpiration <= 3600 {
  45. signer.credentialExpiration = credential.SessionExpiration
  46. } else {
  47. err = errors.NewClientError(errors.InvalidParamErrorCode, "Key Pair session duration should be in the range of 15min - 1Hr", nil)
  48. }
  49. } else {
  50. signer.credentialExpiration = defaultDurationSeconds
  51. }
  52. return
  53. }
  54. func (*SignerKeyPair) GetName() string {
  55. return "HMAC-SHA1"
  56. }
  57. func (*SignerKeyPair) GetType() string {
  58. return ""
  59. }
  60. func (*SignerKeyPair) GetVersion() string {
  61. return "1.0"
  62. }
  63. func (signer *SignerKeyPair) GetAccessKeyId() (accessKeyId string, err error) {
  64. if signer.sessionCredential == nil || signer.needUpdateCredential() {
  65. err = signer.updateCredential()
  66. }
  67. if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0) {
  68. return "", err
  69. }
  70. return signer.sessionCredential.AccessKeyId, err
  71. }
  72. func (signer *SignerKeyPair) GetExtraParam() map[string]string {
  73. if signer.sessionCredential == nil || signer.needUpdateCredential() {
  74. signer.updateCredential()
  75. }
  76. if signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0 {
  77. return make(map[string]string)
  78. }
  79. return make(map[string]string)
  80. }
  81. func (signer *SignerKeyPair) Sign(stringToSign, secretSuffix string) string {
  82. secret := signer.sessionCredential.AccessKeyId + secretSuffix
  83. return ShaHmac1(stringToSign, secret)
  84. }
  85. func (signer *SignerKeyPair) buildCommonRequest() (request *requests.CommonRequest, err error) {
  86. request = requests.NewCommonRequest()
  87. request.Product = "Sts"
  88. request.Version = "2015-04-01"
  89. request.ApiName = "GenerateSessionAccessKey"
  90. request.Scheme = requests.HTTPS
  91. request.QueryParams["PublicKeyId"] = signer.credential.PublicKeyId
  92. request.QueryParams["DurationSeconds"] = strconv.Itoa(signer.credentialExpiration)
  93. return
  94. }
  95. func (signerKeyPair *SignerKeyPair) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  96. signerV2, err := NewSignerV2(signerKeyPair.credential)
  97. return signerKeyPair.commonApi(request, signerV2)
  98. }
  99. func (signer *SignerKeyPair) refreshCredential(response *responses.CommonResponse) (err error) {
  100. if response.GetHttpStatus() != http.StatusOK {
  101. message := "refresh session AccessKey failed"
  102. err = errors.NewServerError(response.GetHttpStatus(), response.GetHttpContentString(), message)
  103. return
  104. }
  105. var data interface{}
  106. err = json.Unmarshal(response.GetHttpContentBytes(), &data)
  107. if err != nil {
  108. fmt.Println("refresh KeyPair err, json.Unmarshal fail", err)
  109. return
  110. }
  111. accessKeyId, err := jmespath.Search("SessionAccessKey.SessionAccessKeyId", data)
  112. if err != nil {
  113. fmt.Println("refresh KeyPair err, fail to get SessionAccessKeyId", err)
  114. return
  115. }
  116. accessKeySecret, err := jmespath.Search("SessionAccessKey.SessionAccessKeySecret", data)
  117. if err != nil {
  118. fmt.Println("refresh KeyPair err, fail to get SessionAccessKeySecret", err)
  119. return
  120. }
  121. if accessKeyId == nil || accessKeySecret == nil {
  122. return
  123. }
  124. signer.sessionCredential = &SessionCredential{
  125. AccessKeyId: accessKeyId.(string),
  126. AccessKeySecret: accessKeySecret.(string),
  127. }
  128. return
  129. }
  130. func (signer *SignerKeyPair) Shutdown() {
  131. }