Class: IswRestSecure

Inherits:
Object
  • Object
show all
Defined in:
lib/isw_rest_secure.rb

Class Method Summary collapse

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
	authorization = generate_authorization client_id
	timestamp = generate_timestamp
	signature = generate_signature(client_id, secret, url, http_method, timestamp, nonce, tran_parameters )
	
	headers = {
         "Signature" => signature,
         "Timestamp" => timestamp,
         "Nonce" => nonce,
         "Authorization" => authorization,
         "SignatureMethod" => "SHA1",
         "Content-Type" => content_type
       }

    return headers

rescue => e
	puts "Error occured #{e.message}"
	return nil
end

.generate_authorization(client_id) ⇒ Object



68
69
70
# File 'lib/isw_rest_secure.rb', line 68

def self.generate_authorization(client_id)
	"InterswitchAuth #{Base64.encode64(client_id)}"
end

.generate_nonceObject



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, timestamp, nonce, tran_parameters )
 
 		url = url.sub("http://", "https://")
 		encoded_url = Rack::Utils.escape(url).to_s 

 		base_string = http_method + "&" +
      encoded_url + "&" +
      timestamp + "&" +
      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_timestampObject



73
74
75
# File 'lib/isw_rest_secure.rb', line 73

def self.generate_timestamp
  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