Module: Conversant::V3::HttpClient
- Included in:
- Base
- Defined in:
- lib/conversant/v3/http_client.rb
Overview
HTTP client module for handling SSO authentication and HTTP requests
This module provides low-level HTTP operations including SSO login flow, cookie management, and authenticated request handling. It's included by Base class and used by all service classes for authentication.
Constant Summary collapse
- LOGIN_URL =
Default login URL for SSO authentication
'https://console.swiftfederation.com/'
- PORTAL_SESSION_REDIS_KEY =
Redis key for storing portal SESSION cookie
'CONVERSANT.V3.PORTAL.SESSION'
- SSO_GW_SESSION2_REDIS_KEY =
Redis key for storing SSO_GW_SESSION2 cookie
'CONVERSANT.V3.PORTAL.SSO_GW_SESSION2'
Instance Method Summary collapse
-
#authenticate ⇒ Hash{Symbol => String}?
Authenticates and returns cached SSO sessions.
-
#cookie_jar ⇒ Hash{String => String}
Gets the thread-local cookie jar for storing cookies during authentication.
-
#cookie_jar=(value) ⇒ Hash?
Sets the thread-local cookie jar.
-
#debug_log(message) ⇒ void
Logs a debug message if debug mode is enabled.
-
#request(method, url, payload, headers) ⇒ Array(Integer, RestClient::Response)
Makes an authenticated HTTP request.
-
#sso_login ⇒ Hash{Symbol => String}?
Performs SSO login to obtain root SESSION and SSO_GW_SESSION2 cookies.
Instance Method Details
#authenticate ⇒ Hash{Symbol => String}?
Authenticates and returns cached SSO sessions
Wrapper around sso_login that ensures sessions are cached in Redis. This is the primary method used by service classes for authentication.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/conversant/v3/http_client.rb', line 191 def authenticate result = sso_login if result && result[:session] redis.set(PORTAL_SESSION_REDIS_KEY, result[:session], ex: configuration.cache_ttl) redis.set(SSO_GW_SESSION2_REDIS_KEY, result[:sso_gw_session2], ex: configuration.cache_ttl) if result[:sso_gw_session2] result else nil end end |
#cookie_jar ⇒ Hash{String => String}
Gets the thread-local cookie jar for storing cookies during authentication
The cookie jar is used to maintain cookies across multiple HTTP requests during the SSO login flow. Each thread has its own isolated cookie jar.
44 45 46 |
# File 'lib/conversant/v3/http_client.rb', line 44 def Thread.current[:conversant_cookie_jar] ||= {} end |
#cookie_jar=(value) ⇒ Hash?
Sets the thread-local cookie jar
58 59 60 |
# File 'lib/conversant/v3/http_client.rb', line 58 def (value) Thread.current[:conversant_cookie_jar] = value end |
#debug_log(message) ⇒ void
This method returns an undefined value.
Logs a debug message if debug mode is enabled
71 72 73 74 75 |
# File 'lib/conversant/v3/http_client.rb', line 71 def debug_log() return unless configuration.debug_mode configuration.logger.debug "Conversant::V3 - #{message}" end |
#request(method, url, payload, headers) ⇒ Array(Integer, RestClient::Response)
Makes an authenticated HTTP request
Handles JSON payload serialization and SSL verification based on configuration. All service classes use this method for making API calls.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/conversant/v3/http_client.rb', line 222 def request(method, url, payload, headers) debug_log "[Request] #{method} #{url}" debug_log "[Request] Headers: #{headers.inspect}" if configuration.debug_mode # Check Content-Type using string key (headers are now plain hash with string keys) if headers&.[]('Content-Type') == configuration.default_content_type payload = payload&.to_json end request = RestClient::Request.new( method: method, url: url, payload: payload, headers: headers, verify_ssl: configuration.verify_ssl ) response = request.execute do |response| response end debug_log "[Response] Status: #{response.code}" if configuration.debug_mode [response.code, response] rescue StandardError => e configuration.logger.error "Conversant::V3 - Request exception: #{e.message}" [500, nil] end |
#sso_login ⇒ Hash{Symbol => String}?
Performs SSO login to obtain root SESSION and SSO_GW_SESSION2 cookies
Executes a multi-step SSO authentication flow:
- Fetches the login page
- Extracts the form action URL
- Submits credentials
- Extracts and caches session cookies
Sessions are cached in Redis for reuse across all services and customers.
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/conversant/v3/http_client.rb', line 97 def sso_login session_key = PORTAL_SESSION_REDIS_KEY sso_key = SSO_GW_SESSION2_REDIS_KEY # Check cache first cached_session = redis.get(session_key) cached_sso_gw = redis.get(sso_key) if cached_session && cached_sso_gw debug_log 'Using cached global SSO sessions' return { session: cached_session, sso_gw_session2: cached_sso_gw } end begin # Initialize thread-safe cookie jar self. = {} # Step 1: Get login page and extract form action debug_log "[SSO Login - Step 1] Fetching login page: #{LOGIN_URL}" login_page_response = http_get(LOGIN_URL) return nil unless login_page_response debug_log "Login page loaded, status: #{login_page_response[:status]}" # Step 2: Parse login form to get action URL form_action = extract_form_action(login_page_response[:body]) unless form_action debug_log 'Could not extract form action URL from login page' return nil end debug_log "[SSO Login - Step 2] Form action extracted: #{form_action}" # Step 3: Submit credentials form_data = URI.encode_www_form( username: configuration.swiftserve_identifier_id, password: configuration.swiftserve_identifier_hash, credentialId: '' ) debug_log '[SSO Login - Step 3] Submitting login form...' login_response = http_post(form_action, form_data, { 'Content-Type' => 'application/x-www-form-urlencoded' }) return nil unless login_response debug_log "Login completed, status: #{login_response[:status]}" # Step 4: Extract cookies = ['SESSION'] = ['SSO_GW_SESSION2'] if && # Cache the sessions globally redis.set(session_key, , ex: configuration.cache_ttl) redis.set(sso_key, , ex: configuration.cache_ttl) configuration.logger.info 'Conversant::V3 - SSO login successful, sessions cached' return { session: , sso_gw_session2: } end debug_log 'New SSO login failed - no valid sessions obtained' nil rescue StandardError => e configuration.logger.error "Conversant::V3 - SSO login error: #{e.message}" nil ensure self. = nil end end |