Class: Oxd::OxdConnector
- Inherits:
-
Object
- Object
- Oxd::OxdConnector
- Defined in:
- lib/oxd/oxd_connector.rb
Overview
A class which takes care of the socket communication with oxd Server.
Direct Known Subclasses
Instance Method Summary collapse
-
#getData ⇒ Array
combines command and command parameters for socket request.
-
#getResponseData ⇒ Mixed
extracts ‘data’ parameter from response object.
-
#getResponseObject ⇒ Mixed
Response object set by request method.
-
#initialize ⇒ OxdConnector
constructor
class constructor.
-
#is_json?(string_to_validate) ⇒ Boolean
checks whether the passed string is in JSON format or not.
-
#logger(log_msg) ⇒ Object
Logs server response and errors to log file.
-
#oxd_http_request(request_params, command = "") ⇒ Object
method to communicate with the oxd-to-https server.
-
#oxd_socket_request(request, char_count = 8192) ⇒ Object
method to communicate with the oxd server.
-
#request(comm = "") ⇒ JSON
method to send commands to the oxd server and oxd-to-http and to recieve the response via #oxd_socket_request.
-
#trigger_error(msg) ⇒ Object
Logs generated errors to log file.
-
#validate_command ⇒ Object
Checks the validity of command that is to be passed to oxd-server.
Constructor Details
#initialize ⇒ OxdConnector
class constructor
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/oxd/oxd_connector.rb', line 15 def initialize @command @response_json @response_object @data = Hash.new @params = Hash.new @response_data = Hash.new @configuration = Oxd.config trigger_error("Problem with json data : authorization_redirect_uri can't be blank") if @configuration..empty? trigger_error("#{@configuration.oxd_host_ip} is not a valid IP address") if (IPAddr.new(@configuration.oxd_host_ip) rescue nil).nil? trigger_error("#{@configuration.oxd_host_port} is not a valid port for socket. Port must be integer and between from 0 to 65535") if (!@configuration.oxd_host_port.is_a?(Integer) || (@configuration.oxd_host_port < 0 && @configuration.oxd_host_port > 65535)) end |
Instance Method Details
#getData ⇒ Array
combines command and command parameters for socket request
153 154 155 156 |
# File 'lib/oxd/oxd_connector.rb', line 153 def getData @data = {'command' => @command, 'params' => @params} return @data end |
#getResponseData ⇒ Mixed
extracts ‘data’ parameter from response object
142 143 144 145 146 147 148 149 |
# File 'lib/oxd/oxd_connector.rb', line 142 def getResponseData if (!@response_object) @response_data = 'Data is empty'; else @response_data = @response_object['data'] end return @response_data end |
#getResponseObject ⇒ Mixed
Returns response object set by request method.
136 137 138 |
# File 'lib/oxd/oxd_connector.rb', line 136 def getResponseObject return @response_object end |
#is_json?(string_to_validate) ⇒ Boolean
checks whether the passed string is in JSON format or not
161 162 163 164 165 166 167 |
# File 'lib/oxd/oxd_connector.rb', line 161 def is_json? (string_to_validate) begin !!JSON.parse(string_to_validate) rescue false end end |
#logger(log_msg) ⇒ Object
Logs server response and errors to log file
172 173 174 175 176 177 |
# File 'lib/oxd/oxd_connector.rb', line 172 def logger(log_msg) # Initialize Log file # Location : app_root/log/oxd-ruby.log @logger ||= Logger.new("log/oxd-ruby.log") @logger.info(log_msg) end |
#oxd_http_request(request_params, command = "") ⇒ Object
method to communicate with the oxd-to-https server
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/oxd/oxd_connector.rb', line 68 def oxd_http_request(request_params, command = "") uri = URI.parse("https://127.0.0.1/"+command) http = Net::HTTP.new("127.0.0.1", 8443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.request_uri) request.add_field('Content-Type', 'application/json') if(@configuration.protection_access_token.present?) request.add_field('Authorization','Bearer '+@configuration.protection_access_token) end request.body = request_params logger("Sending oxd_http_request command #{command} with data #{request_params.inspect}") http_response = http.request(request) response = http_response.body logger("oxd_http_request response #{response}") return response end |
#oxd_socket_request(request, char_count = 8192) ⇒ Object
method to communicate with the oxd server
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/oxd/oxd_connector.rb', line 41 def oxd_socket_request(request, char_count = 8192) host = @configuration.oxd_host_ip # The web server port = @configuration.oxd_host_port # Default HTTP port if(!socket = TCPSocket.new(host, port) ) # Connect to oxd server trigger_error("Socket Error : Couldn't connect to socket") else logger("Client: socket::socket_connect connected : #{request}") end socket.print(request) # Send request response = socket.recv(char_count) # Read response if(response) logger("Client: oxd_socket_response: #{response}") else trigger_error("Client: oxd_socket_response : Error socket reading process.") end # close connection if(socket.close) logger("Client: oxd_socket_connection : disconnected.") end return response end |
#request(comm = "") ⇒ JSON
method to send commands to the oxd server and oxd-to-http and to recieve the response via #oxd_socket_request
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/oxd/oxd_connector.rb', line 91 def request(comm = "") uri = URI.parse(@configuration.) trigger_error("Please enable SSL on your website or check URIs in oxd configuration.") if (uri.scheme != 'https') validate_command if(@configuration.connection_type == 'local') jsondata = getData.to_json if(!is_json? (jsondata)) trigger_error("Sending parameters must be JSON. Exiting process.") end length = jsondata.length if( length <= 0 ) trigger_error("JSON data length must be more than zero. Exiting process.") else length = length <= 999 ? sprintf('0%d', length) : length end @response_json = oxd_socket_request((length.to_s + jsondata).encode("UTF-8")) @response_json.sub!(@response_json[0..3], "") else jsondata = @params.to_json @response_json = oxd_http_request(jsondata, comm) end if (@response_json) response = JSON.parse(@response_json) if (response['status'] == 'error') raise ServerError, response['data'] if response['data']['error'] == 'internal_error' raise NeedInfoError, response['data'] if response['data']['error'] == 'need_info' raise InvalidTicketError, response['data'] if response['data']['error'] == 'invalid_ticket' raise InvalidRequestError, response['data'] if response['data']['error'] == 'invalid_request' trigger_error("oxd Server Error : #{response['data']['error_description']}") elsif (response['status'] == 'ok') @response_object = JSON.parse(@response_json) end else trigger_error("Response is empty. Exiting process.") end return @response_object end |
#trigger_error(msg) ⇒ Object
Logs generated errors to log file
181 182 183 184 |
# File 'lib/oxd/oxd_connector.rb', line 181 def trigger_error(msg) logger(msg) raise msg end |
#validate_command ⇒ Object
Checks the validity of command that is to be passed to oxd-server
30 31 32 33 34 35 |
# File 'lib/oxd/oxd_connector.rb', line 30 def validate_command command_types = ['setup_client', 'get_client_token', 'introspect_access_token', 'get_authorization_url','update_site','remove_site','get_tokens_by_code','get_access_token_by_refresh_token', 'get_user_info', 'register_site', 'get_logout_uri','get_authorization_code','uma_rs_protect','uma_rs_check_access','uma_rp_get_rpt','uma_rp_get_claims_gathering_url','introspect_rpt'] if (!command_types.include?(@command)) trigger_error("Command: #{@command} does not exist! Exiting process.") end end |