Module: SimpleHmac::Helper
- Included in:
- ActionDispatch::Request, RestClient::Request
- Defined in:
- lib/simple-hmac/helper.rb
Instance Method Summary collapse
- #authentication_headers(verb, content_type, payload, url, api_key, api_secret, options = {}) ⇒ Object
- #digest_md5(verb, payload) ⇒ Object
- #hmac_token(verb, content_type, content_md5, url, date, api_secret, options = {}) ⇒ Object
- #sign_string(string, secret, algorithm = 'sha256') ⇒ Object
Instance Method Details
#authentication_headers(verb, content_type, payload, url, api_key, api_secret, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/simple-hmac/helper.rb', line 21 def authentication_headers(verb, content_type, payload, url, api_key, api_secret, ={}) = { auth_prefix: 'WIZYPAY' }.merge() auth_prefix = .delete :auth_prefix date = Time.now.httpdate content_type ||= 'text/plain' content_md5 = digest_md5(verb, payload) hmac_token = hmac_token(verb, content_type, content_md5, url, date, api_secret, ) { 'Date' => date, 'Content-Type' => content_type, 'Content-MD5' => content_md5, 'Authorization' => "#{auth_prefix} #{api_key}:#{hmac_token}" } end |
#digest_md5(verb, payload) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simple-hmac/helper.rb', line 36 def digest_md5(verb, payload) return '' unless [:post, :put, :patch].include?(verb.downcase.to_sym) if payload.nil? body = '' elsif payload.respond_to? :read body = payload.read payload.instance_variable_get(:@stream).seek(0) else body = payload end Digest::MD5.base64digest(body) end |
#hmac_token(verb, content_type, content_md5, url, date, api_secret, options = {}) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/simple-hmac/helper.rb', line 8 def hmac_token(verb, content_type, content_md5, url, date, api_secret, ={}) = { separator: "\n", algorithm: 'sha256', include_verb: true }.merge() url_without_server = url.gsub(/https?:\/\/[^(,|\?|\/)]*/, '') data = [verb.upcase, content_type, content_md5, url_without_server, date] data.shift unless [:include_verb] string_to_sign = data.join([:separator]) sign_string(string_to_sign, api_secret, [:algorithm]) end |
#sign_string(string, secret, algorithm = 'sha256') ⇒ Object
17 18 19 |
# File 'lib/simple-hmac/helper.rb', line 17 def sign_string(string, secret, algorithm = 'sha256') Base64.strict_encode64(OpenSSL::HMAC.digest(algorithm, secret, string)) end |