Class: Libgss::HttpClientWithSignatureKey::Signature
- Inherits:
-
Object
- Object
- Libgss::HttpClientWithSignatureKey::Signature
- Defined in:
- lib/libgss/http_client_with_signature_key.rb
Constant Summary collapse
- RESERVED_CHARACTERS =
/[^a-zA-Z0-9\-\.\_\~]/
Class Method Summary collapse
Instance Method Summary collapse
- #digest ⇒ Object
- #escape(value) ⇒ Object
-
#initialize(req_hash, options) ⇒ Signature
constructor
A new instance of Signature.
- #normalize(params) ⇒ Object
- #normalized_parameters ⇒ Object
- #normalized_uri ⇒ Object
- #parameters ⇒ Object
- #parameters_for_signature ⇒ Object
- #secret ⇒ Object
- #sign ⇒ Object
- #signature_base_string ⇒ Object
Constructor Details
#initialize(req_hash, options) ⇒ Signature
Returns a new instance of Signature.
134 135 136 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 134 def initialize(req_hash, ) @request, @options = req_hash, end |
Class Method Details
.sign(req_hash, options) ⇒ Object
129 130 131 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 129 def sign(req_hash, ) new(req_hash, ).sign end |
Instance Method Details
#digest ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 145 def digest s = signature_base_string # base_stringを計算 digest_class = ::Digest::SHA1 # http://doc.ruby-lang.org/ja/1.9.3/class/Digest=3a=3aSHA1.html # digest_class.digest(s) # Digestクラスを使って計算 # http://doc.ruby-lang.org/ja/1.9.3/library/digest=2fhmac.html # http://doc.ruby-lang.org/ja/1.9.3/class/Digest=3a=3aHMAC.html ::Digest::HMAC.digest(s, secret, digest_class) end |
#escape(value) ⇒ Object
193 194 195 196 197 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 193 def escape(value) URI::escape(value.to_s, RESERVED_CHARACTERS) rescue ArgumentError URI::escape(value.to_s.force_encoding(Encoding::UTF_8), RESERVED_CHARACTERS) end |
#normalize(params) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 199 def normalize(params) params.sort.map do |k, values| if values.is_a?(Array) # multiple values were provided for a single key values.sort.collect do |v| [escape(k),escape(v)] * "=" end else [escape(k),escape(values)] * "=" end end * "&" end |
#normalized_parameters ⇒ Object
173 174 175 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 173 def normalized_parameters normalize(parameters_for_signature) end |
#normalized_uri ⇒ Object
166 167 168 169 170 171 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 166 def normalized_uri u = URI.parse(@request["uri"]) "#{u.scheme.downcase}://#{u.host.downcase}#{(u.scheme.downcase == 'http' && u.port != 80) || (u.scheme.downcase == 'https' && u.port != 443) ? ":#{u.port}" : ""}#{(u.path && u.path != '') ? u.path : '/'}" rescue @request["uri"] end |
#parameters ⇒ Object
181 182 183 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 181 def parameters @request["parameters"] end |
#parameters_for_signature ⇒ Object
177 178 179 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 177 def parameters_for_signature parameters.reject { |k,v| k == "oauth_signature"} end |
#secret ⇒ Object
186 187 188 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 186 def secret "#{escape(@options[:consumer_secret])}&#{escape(@options[:token_secret])}" end |
#sign ⇒ Object
138 139 140 141 142 143 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 138 def sign s = digest # ダイジェストを計算 Base64.encode64(s). # BASE64でエンコード chomp. # 末尾から改行コード(\r or \n)を取り除いた文字列を返す gsub(/\n/,'') # 改行を全て取り除く end |
#signature_base_string ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/libgss/http_client_with_signature_key.rb', line 156 def signature_base_string base = [ @request["method"], normalized_uri, normalized_parameters] r = base.map { |v| escape(v) }.join("&") $stdout.puts("signature_base_string: #{r.inspect}") if ENV["VERBOSE"] r end |