Module: Aliyun::Opensearch::ClientExt::Traffic::Header::Credentials

Included in:
Aliyun::Opensearch::ClientExt::Traffic::Header
Defined in:
lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb

Overview

HTTP请求header - 请求认证模块

Author:

Constant Summary collapse

SIGN_PREFIX =
'OPENSEARCH'

Instance Method Summary collapse

Instance Method Details

#authorizationObject



16
17
18
# File 'lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb', line 16

def authorization
  "#{SIGN_PREFIX} #{@access_key_id}:#{signature}"
end

#canonicalized_open_search_headersString

生成 CanonicalizedOpenSearchHeaders

Returns:

  • (String)

    CanonicalizedOpenSearchHeaders

See Also:

Author:



43
44
45
46
47
# File 'lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb', line 43

def canonicalized_open_search_headers
  @canonicalized_open_search_headers ||= self.x_opensearch_headers.to_a.sort { |x, y| x[0] <=> y[0] }.map do |e|
    "#{e[0].downcase}:#{e[1]}"
  end.join("\n")
end

#canonicalized_resourceString

生成 CanonicalizedResource

Returns:

  • (String)

    CanonicalizedResource

See Also:

Author:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb', line 58

def canonicalized_resource
  path = @path
  query = nil

  if @request_method == 'GET'
    # 按照阿里云文档生成query, see: https://help.aliyun.com/document_detail/54237.htm#section-ni9-hgi-tal
    query = @api_params.to_a.sort { |x, y| x[0] <=> y[0] }.map do |e|
      unless e[1].nil?
        [e[0], (e[1].to_s == '' ? nil : e[1].to_s)]
      end
    end.compact

    # encode_www_form方法文档中描述一些字符不会转移, 这里手动进行处理
    # This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP (ASCII space) to + and converts others to %XX.
    query = URI.encode_www_form(query).gsub('+', '%20').gsub('*', '%2A')
  end

  [path, query].compact.reject(&:empty?).join('?')
end

#signatureObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aliyun/opensearch/client_ext/traffic/header/credentials.rb', line 20

def signature
  @signature ||= begin
    sign_body = [@request_method, self.content_md5, self.content_type, self.date, canonicalized_open_search_headers, canonicalized_resource].join("\n")

    Base64.encode64(
      OpenSSL::HMAC.digest(
        OpenSSL::Digest.new('sha1'),
        @access_key_secret,
        sign_body
      )
    )
  end
end