Class: TencentCloud::CaptchaClient
- Inherits:
-
Object
- Object
- TencentCloud::CaptchaClient
- Defined in:
- lib/tencent_cloud/captcha/v20190722/captcha_client.rb
Constant Summary collapse
- API_VERSION =
'2019-07-22'
- Action =
'DescribeCaptchaResult'
- CaptchaType =
9
- REQUEST_PATH =
'/'
- JSON_CONTENT =
'application/json'
- MULTIPART_CONTENT =
'multipart/form-data'
- FORM_URLENCODED_CONTENT =
'application/x-www-form-urlencoded'
- ENDPOINT =
'captcha.tencentcloudapi.com'
Instance Method Summary collapse
- #build_req_with_v3_signature(action, params, req, options = {}) ⇒ Object
- #format_params(prefix = nil, params) ⇒ Object
- #http_request(data) ⇒ Object
-
#initialize(secret_id, secret_key) ⇒ CaptchaClient
constructor
A new instance of CaptchaClient.
- #sign_v3(content_type, endpoint, method, uri, query, payload, timestamp, secret_id, secret_key) ⇒ Object
Constructor Details
#initialize(secret_id, secret_key) ⇒ CaptchaClient
Returns a new instance of CaptchaClient.
16 17 18 19 20 21 22 |
# File 'lib/tencent_cloud/captcha/v20190722/captcha_client.rb', line 16 def initialize(secret_id, secret_key) @api_version = '2019-07-22' @secret_id = secret_id @secret_key = secret_key @token = nil @region = nil end |
Instance Method Details
#build_req_with_v3_signature(action, params, req, options = {}) ⇒ Object
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 |
# File 'lib/tencent_cloud/captcha/v20190722/captcha_client.rb', line 33 def build_req_with_v3_signature(action, params, req, = {}) content_type = JSON_CONTENT = Time.current.to_i header = {} header['Content-Type'] = content_type header['Host'] = ENDPOINT header['X-TC-Action'] = action header['X-TC-Timestamp'] = header['X-TC-Version'] = @api_version header['X-TC-Region'] = @region header['X-TC-Token'] = @token if @token req.body = JSON.generate(params, { 'ascii_only' => true, 'space' => ' ' }) payload = req.body canonical_querystring = '' hashed_payload = Digest::SHA256.hexdigest(payload) = sign_v3(content_type, ENDPOINT, 'POST', '/', canonical_querystring, hashed_payload, header['X-TC-Timestamp'], @secret_id, @secret_key) header['Authorization'] = header.each do |k, v| req[k] = v end req end |
#format_params(prefix = nil, params) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tencent_cloud/captcha/v20190722/captcha_client.rb', line 89 def format_params(prefix = nil, params) d = {} case params when Hash params.each do |k, v| key = prefix ? "#{prefix}.#{k}" : k.to_s d.update(format_params(key, v)) end when Array params.each_with_index do |v, i| key = prefix ? "#{prefix}.#{i}" : i.to_s d.update(format_params(key, v)) end else d[prefix] = params end d end |
#http_request(data) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/tencent_cloud/captcha/v20190722/captcha_client.rb', line 24 def http_request(data) uri = URI.parse("https://#{ENDPOINT}") request = Net::HTTP::Post.new(uri.to_s) request = build_req_with_v3_signature(Action, data, request, = {}) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request request end end |
#sign_v3(content_type, endpoint, method, uri, query, payload, timestamp, secret_id, secret_key) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tencent_cloud/captcha/v20190722/captcha_client.rb', line 58 def sign_v3(content_type, endpoint, method, uri, query, payload, , secret_id, secret_key) canonical_headers = "content-type:#{content_type}\nhost:#{endpoint}\n" signed_headers = 'content-type;host' canonical_request = [ method, uri, query, canonical_headers, signed_headers, payload, ].join("\n") service = endpoint.split('.')[0] date = Time.at().utc.strftime('%Y-%m-%d') credential_scope = date + '/' + service + '/' + 'tc3_request' algorithm = 'TC3-HMAC-SHA256' hashed_request_payload = Digest::SHA256.hexdigest(canonical_request) string_to_sign = [ algorithm, , credential_scope, hashed_request_payload, ].join("\n") digest = OpenSSL::Digest.new('sha256') secret_date = OpenSSL::HMAC.digest(digest, 'TC3' + secret_key, date) secret_service = OpenSSL::HMAC.digest(digest, secret_date, service) secret_signing = OpenSSL::HMAC.digest(digest, secret_service, 'tc3_request') signature = OpenSSL::HMAC.hexdigest(digest, secret_signing, string_to_sign) "#{algorithm} Credential=#{secret_id}/#{credential_scope}, SignedHeaders=#{signed_headers}, Signature=#{signature}" end |