Class: IswRestSecure
- Inherits:
-
Object
- Object
- IswRestSecure
- Defined in:
- lib/isw_rest_secure.rb
Class Method Summary collapse
- .generate_auth_headers(hash = {}) ⇒ Object
- .generate_authorization(client_id) ⇒ Object
- .generate_nonce ⇒ Object
- .generate_signature(client_id, secret, url, http_method, timestamp, nonce, tran_parameters) ⇒ Object
- .generate_timestamp ⇒ Object
- .valid_params(hash = {}) ⇒ Object
Class Method Details
.generate_auth_headers(hash = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/isw_rest_secure.rb', line 9 def self.generate_auth_headers(hash={}) if !valid_params(hash) return nil end client_id = hash[:client_id] secret = hash[:secret] http_method = hash[:http_method] url = hash[:url] tran_parameters = hash[:tran_parameters] content_type = hash[:content_type] nonce = generate_nonce = client_id = signature = generate_signature(client_id, secret, url, http_method, , nonce, tran_parameters ) headers = { "Signature" => signature, "Timestamp" => , "Nonce" => nonce, "Authorization" => , "SignatureMethod" => "SHA1", "Content-Type" => content_type } return headers rescue => e puts "Error occured #{e.}" return nil end |
.generate_authorization(client_id) ⇒ Object
68 69 70 |
# File 'lib/isw_rest_secure.rb', line 68 def self.(client_id) "InterswitchAuth #{Base64.encode64(client_id)}" end |
.generate_nonce ⇒ Object
77 78 79 |
# File 'lib/isw_rest_secure.rb', line 77 def self.generate_nonce SecureRandom.random_number(99999999999999999999999).to_s end |
.generate_signature(client_id, secret, url, http_method, timestamp, nonce, tran_parameters) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/isw_rest_secure.rb', line 43 def self.generate_signature( client_id, secret, url, http_method, , nonce, tran_parameters ) url = url.sub("http://", "https://") encoded_url = Rack::Utils.escape(url).to_s base_string = http_method + "&" + encoded_url + "&" + + "&" + nonce + "&" + client_id + "&" + secret temp_parameters = "" if tran_parameters && tran_parameters.kind_of?(Array) && tran_parameters.count > 0 tran_parameters.each do |tran_parameter| temp_parameters = temp_parameters + "&" + tran_parameter.to_s end end full_string_to_be_signed = base_string + temp_parameters signature = Base64.encode64((Digest::SHA1.new() << full_string_to_be_signed).digest).strip return signature end |
.generate_timestamp ⇒ Object
73 74 75 |
# File 'lib/isw_rest_secure.rb', line 73 def self. Time.now.to_i.to_s end |
.valid_params(hash = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/isw_rest_secure.rb', line 83 def self.valid_params(hash={}) if hash[:client_id].blank? puts ">>>Empty client id supplied.." return false end if hash[:secret].blank? puts ">>>Empty secret supplied.." return false end if hash[:http_method].blank? puts ">>>Empty http method supplied.." return false end if hash[:url].blank? puts ">>>Empty url supplied.." return false end return true end |