Class: Aliyun::Oss::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/oss/authorization.rb

Constant Summary collapse

PROVIDER =
'OSS'

Class Method Summary collapse

Class Method Details

.concat_content_string(verb, time, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/aliyun/oss/authorization.rb', line 76

def self.concat_content_string(verb, time, options = {})
  headers = options.fetch(:headers, {})

  conon_headers = get_cononicalized_oss_headers(headers)
  conon_resource = get_cononicalized_resource(
    *options.values_at(:bucket, :key, :query)
  )

  join_values(verb.upcase, time, headers, conon_headers, conon_resource)
end

.get_authorization(access_key, secret_key, options = {}) ⇒ String

Get authorization key

Parameters:

  • access_key (String)

    Access Key

  • secret_key (String)

    Secret Key

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :verb (String)

    VERB, request method

  • :date (String)

    Request Time in formate: ‘%a, %d %b %Y %H:%M:%S GMT’

  • :bucket (String)

    Bucket Name

  • :key (String)

    Object Name

  • :query (Hash)

    Query key-value pair

  • :headers (Hash)

    Headers

Returns:

  • (String)

    the authorization string

See Also:



70
71
72
73
74
# File 'lib/aliyun/oss/authorization.rb', line 70

def self.get_authorization(access_key, secret_key, options = {})
  content_string = concat_content_string(options[:verb], options[:date], options)
  signature_string = signature(secret_key, content_string)
  "#{PROVIDER} #{access_key}:#{signature_string.strip}"
end

.get_base64_policy(policy) ⇒ String

Get base64 encoded string, used to fill policy field

Parameters:

  • policy (Hash)

    Policy Detail

Returns:

  • (String)

See Also:



37
38
39
# File 'lib/aliyun/oss/authorization.rb', line 37

def self.get_base64_policy(policy)
  Base64.encode64(JSON.generate(policy).force_encoding('utf-8')).delete("\n")
end

.get_cononicalized_oss_headers(headers) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/aliyun/oss/authorization.rb', line 127

def self.get_cononicalized_oss_headers(headers)
  oss_headers = (headers || {}).select do |key, _|
    key.to_s.downcase.start_with?('x-oss-')
  end
  return if oss_headers.empty?

  oss_headers.keys.sort.map do |key|
    "#{key.downcase}:#{oss_headers[key]}"
  end.join("\n")
end

.get_cononicalized_resource(bucket, key, query) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/aliyun/oss/authorization.rb', line 138

def self.get_cononicalized_resource(bucket, key, query)
  cononicalized_resource = '/'
  cononicalized_resource += "#{bucket}/" if bucket
  cononicalized_resource += key if key
  return cononicalized_resource if query.nil? || query.empty?

  query_str = query.keys.sort.map { |k| "#{k}=#{query[k]}" }.join('&')
  cononicalized_resource + '?' + query_str
end

.get_policy_signature(secret_key, policy) ⇒ String

Get Signature for policy

Parameters:

  • secret_key (String)

    Secret Key

  • policy (Hash)

    Policy Detail

Returns:

  • (String)

See Also:



49
50
51
# File 'lib/aliyun/oss/authorization.rb', line 49

def self.get_policy_signature(secret_key, policy)
  signature(secret_key, get_base64_policy(policy)).strip
end

.get_temporary_signature(secret_key, expire_time, options = {}) ⇒ String

Get temporary Signature

Parameters:

  • secret_key (String)

    Secret Key

  • expire_time (Integer)

    the number of seconds since January 1, 1970 UTC. used to specified expired time

  • options (Hash) (defaults to: {})

    other options

Options Hash (options):

  • :key (String)

    the object name

  • :bucket (String)

    bucket name

  • :verb, (String)

    Request Method

  • :query (Hash)

    Query Params

  • :headers (Hash)

    Headers Params

Returns:

  • (String)

See Also:



25
26
27
28
# File 'lib/aliyun/oss/authorization.rb', line 25

def self.get_temporary_signature(secret_key, expire_time, options = {})
  content_string = concat_content_string(options[:verb], expire_time, options)
  URI.escape(signature(secret_key, content_string).strip)
end

.join_values(verb, time, headers, conon_headers, resource) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/aliyun/oss/authorization.rb', line 87

def self.join_values(verb, time, headers, conon_headers, resource)
  if conon_headers
    join_with_conon_headers(verb, time, headers, conon_headers, resource)
  else
    join_without_conon_headers(verb, time, headers, resource)
  end
end

.join_with_conon_headers(verb, time, headers, c_headers, resource) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/aliyun/oss/authorization.rb', line 95

def self.join_with_conon_headers(verb, time, headers, c_headers, resource)
  [
    verb,
    headers['Content-MD5'],
    headers['Content-Type'],
    time,
    c_headers,
    resource
  ].join("\n")
end

.join_without_conon_headers(verb, time, headers, resource) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/aliyun/oss/authorization.rb', line 106

def self.join_without_conon_headers(verb, time, headers, resource)
  [
    verb,
    headers['Content-MD5'],
    headers['Content-Type'],
    time,
    resource
  ].join("\n")
end

.signature(secret_key, content_string) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/aliyun/oss/authorization.rb', line 116

def self.signature(secret_key, content_string)
  utf8_string = content_string.force_encoding('utf-8')
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::SHA1.new,
      secret_key,
      utf8_string
    )
  )
end