Class: Aliyun::Oss::Authorization
- Inherits:
-
Object
- Object
- Aliyun::Oss::Authorization
- Defined in:
- lib/aliyun/oss/authorization.rb
Constant Summary collapse
- PROVIDER =
'OSS'
Class Method Summary collapse
- .concat_content_string(verb, time, options = {}) ⇒ Object
-
.get_authorization(access_key, secret_key, options = {}) ⇒ String
Get authorization key.
-
.get_base64_policy(policy) ⇒ String
Get base64 encoded string, used to fill policy field.
- .get_cononicalized_oss_headers(headers) ⇒ Object
- .get_cononicalized_resource(bucket, key, query) ⇒ Object
-
.get_policy_signature(secret_key, policy) ⇒ String
Get Signature for policy.
-
.get_temporary_signature(secret_key, expire_time, options = {}) ⇒ String
Get temporary Signature.
- .join_values(verb, time, headers, conon_headers, resource) ⇒ Object
- .join_with_conon_headers(verb, time, headers, c_headers, resource) ⇒ Object
- .join_without_conon_headers(verb, time, headers, resource) ⇒ Object
- .signature(secret_key, content_string) ⇒ Object
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, = {}) headers = .fetch(:headers, {}) conon_headers = get_cononicalized_oss_headers(headers) conon_resource = get_cononicalized_resource( *.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
70 71 72 73 74 |
# File 'lib/aliyun/oss/authorization.rb', line 70 def self.(access_key, secret_key, = {}) content_string = concat_content_string([:verb], [:date], ) 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
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
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
25 26 27 28 |
# File 'lib/aliyun/oss/authorization.rb', line 25 def self.get_temporary_signature(secret_key, expire_time, = {}) content_string = concat_content_string([:verb], expire_time, ) 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 |