Class: TencentCloud::Common::Http::Sign
- Inherits:
-
Object
- Object
- TencentCloud::Common::Http::Sign
- Defined in:
- lib/tencent_cloud/common/http/sign.rb
Constant Summary collapse
- SIGNED_HEADERS =
%w[host content-type].freeze
- ALGORITHM =
'TC3-HMAC-SHA256'
Instance Attribute Summary collapse
-
#credential ⇒ Object
Returns the value of attribute credential.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #canonical_headers ⇒ Object
-
#canonical_querystring ⇒ Object
For POST method, this is a fixed value.
- #canonical_uri ⇒ Object
- #hashed_request_payload ⇒ Object
- #http_request_method ⇒ Object
-
#initialize(credential, klass, request) ⇒ Sign
constructor
TencentCloud::Common::Http::Sign.new(credential, klass, request).
- #sign_tc3 ⇒ Object
- #signed_headers ⇒ Object
Constructor Details
#initialize(credential, klass, request) ⇒ Sign
TencentCloud::Common::Http::Sign.new(credential, klass, request)
18 19 20 21 22 23 24 25 26 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 18 def initialize(credential, klass, request) unless credential.is_a?(TencentCloud::Common::Credential) raise Exception::TencentCloudSDKException, "InvalidCredential, #{credential} is not a TencentCloud::Common::Credential" end self.credential = credential self.request = request self.klass = klass end |
Instance Attribute Details
#credential ⇒ Object
Returns the value of attribute credential.
12 13 14 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 12 def credential @credential end |
#klass ⇒ Object
Returns the value of attribute klass.
12 13 14 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 12 def klass @klass end |
#request ⇒ Object
Returns the value of attribute request.
12 13 14 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 12 def request @request end |
Instance Method Details
#canonical_headers ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 86 def canonical_headers headers_to_sign = [] request.[:headers].each do |k, v| headers_to_sign << "#{k.to_s.downcase}:#{v}\n" if SIGNED_HEADERS.include?(k) end headers_to_sign.compact.sort.join('') end |
#canonical_querystring ⇒ Object
For POST method, this is a fixed value
99 100 101 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 99 def canonical_querystring '' end |
#canonical_uri ⇒ Object
80 81 82 83 84 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 80 def canonical_uri uri = URI(request.base_url) url_path = CGI.escape(uri.path) url_path == '' ? '/' : url_path end |
#hashed_request_payload ⇒ Object
71 72 73 74 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 71 def hashed_request_payload payload = request.[:body] Digest::SHA256.hexdigest payload end |
#http_request_method ⇒ Object
76 77 78 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 76 def http_request_method (request.[:method] || 'get').upcase end |
#sign_tc3 ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 28 def sign_tc3 # ************* 步骤 1:拼接规范请求串 ************* time = Time.now.utc = time.to_i date = time.strftime('%F') # TODO: DEBUG # timestamp = 1551113065 # date = Time.at(timestamp).utc.strftime('%Y-%m-%d') canonical_request = [ http_request_method, canonical_uri, canonical_querystring, canonical_headers, signed_headers, hashed_request_payload ].join("\n") # ************* 步骤 2:拼接待签名字符串 ************* credential_scope = "#{date}/#{klass::SERVICE_NAME}/tc3_request" hashed_request = Digest::SHA256.hexdigest(canonical_request) string_to_sign = [ ALGORITHM, .to_s, credential_scope, hashed_request ].join("\n") # ************* 步骤 3:计算签名 ************* digest = OpenSSL::Digest.new('sha256') secret_date = OpenSSL::HMAC.digest(digest, 'TC3' + credential.secret_key, date) secret_service = OpenSSL::HMAC.digest(digest, secret_date, klass::SERVICE_NAME) secret_signing = OpenSSL::HMAC.digest(digest, secret_service, 'tc3_request') signature = OpenSSL::HMAC.hexdigest(digest, secret_signing, string_to_sign) # ************* 步骤 4:拼接 Authorization ************* = ALGORITHM + ' ' \ "Credential=#{credential.secret_id}/#{credential_scope}" + ', ' \ 'SignedHeaders=' + signed_headers + ', ' \ 'Signature=' + signature end |
#signed_headers ⇒ Object
94 95 96 |
# File 'lib/tencent_cloud/common/http/sign.rb', line 94 def signed_headers SIGNED_HEADERS.map(&:downcase).sort.join(';') end |