Class: Session
- Inherits:
-
Object
- Object
- Session
- Defined in:
- lib/redpay/session.rb
Instance Method Summary collapse
-
#initialize(app, key, endpoint) ⇒ Session
constructor
A new instance of Session.
- #Send(request) ⇒ Object
Constructor Details
#initialize(app, key, endpoint) ⇒ Session
Returns a new instance of Session.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/redpay/session.rb', line 11 def initialize(app, key, endpoint) @app = app @key = key @endpoint = endpoint # Generate and save Random key @cipher = OpenSSL::Cipher::AES.new(128, :CBC) @cipher.encrypt @random = @cipher.random_key @iv = @cipher.random_iv # Have decipher ready to decrypt response @decipher = OpenSSL::Cipher::AES.new(128, :CBC) @decipher.decrypt @decipher.key = @random @decipher.iv = @iv @public_key = OpenSSL::PKey::RSA.new(Base64.decode64(@key)) @encrypted_random = Base64.encode64(@public_key.public_encrypt(Base64.encode64(@random))) #puts "encrypted_random ->" + @encrypted_random # POST Request sessionId begin uri = URI(@endpoint) header = {'Content-Type': 'text/json'} data = { "rsaPublicKey" => @key, "aesKey" => @encrypted_random } # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' req = Net::HTTP::Post.new(uri.request_uri, header) req.body = data.to_json # Send the request res = http.request(req) # Parse and send back sessionId ses = JSON.parse(res.body) @sessionId = ses["sessionId"] # puts "sessionId ->" + @sessionId rescue => e @sessionId = "ERROR #{e}" end end |
Instance Method Details
#Send(request) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/redpay/session.rb', line 61 def Send(request) plaintext_request = request.to_json; #puts "plaintext_request -> " + plaintext_request encrypted = @cipher.update(plaintext_request) + @cipher.final #encrypted_request = URI::encode(Base64.encode64(encrypted)) encrypted_request = Base64.encode64(encrypted) #puts "encrypted_request -> " + encrypted_request data = { "sessionId" => @sessionId, "app" => @app, "iv" => Base64.encode64(@iv), "aesData" => encrypted_request } #POST Request packet begin uri = URI(@endpoint) header = {'Content-Type': 'text/json'} # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' req = Net::HTTP::Post.new(uri.request_uri, header) req.body = data.to_json # Send the request res = http.request(req) # Get encrypted response encrypted_response = JSON.parse(res.body) # Decrypt response plain_response = @decipher.update(Base64.decode64(encrypted_response["aesData"])) + @decipher.final return JSON.parse(plain_response) rescue => e return "ERROR #{e}" end end |