Module: Conversant::V3::Services::Authorization
- Defined in:
- lib/conversant/v3/services/authorization.rb,
sig/conversant/v3/services/authorization.rbs
Overview
Shared authorization methods for service classes
This module provides common authentication and authorization functionality for Conversant V3 service classes. It handles session management, cookie generation, and HTTP header construction with automatic caching via Redis.
Instance Method Summary collapse
-
#authorize ⇒ String?
Authorizes and retrieves a session cookie.
-
#authorized_headers ⇒ Hash
Builds authorized HTTP headers with session cookies.
-
#base_headers ⇒ Hash
Builds base HTTP headers for API requests.
-
#build_cookie_hash(session_cookie, sso_gw_session2) ⇒ Hash
Builds hash of cookie key-value pairs.
-
#build_headers_with_cookies(session_cookie, sso_gw_session2) ⇒ Hash
Builds HTTP headers hash with cookie values.
-
#cached_session ⇒ String?
Retrieves cached session from Redis.
-
#fetch_new_session ⇒ String
abstract
Fetches a new session from the service.
-
#fetch_session_cookie ⇒ String?
Fetches session cookie from cache or authorizes to get a new one.
-
#fetch_sso_session ⇒ String?
Fetches SSO session from authentication or cache.
-
#format_cookies(session_cookie, sso_gw_session2) ⇒ String?
Formats cookie hash into HTTP Cookie header string.
-
#log_missing_session ⇒ void
Logs warning when required session is missing.
-
#requires_session? ⇒ Boolean
Indicates whether this service requires a session cookie.
-
#service_endpoint ⇒ String
Service-specific methods that must be implemented.
-
#session_cache_key ⇒ String
Generates Redis cache key for session storage.
-
#session_cookie_name ⇒ String
Optional methods that services can override.
-
#sso_cache_key ⇒ String
Generates Redis cache key for SSO session storage.
Instance Method Details
#authorize ⇒ String?
Authorizes and retrieves a session cookie
Checks Redis cache first, then fetches a new session if not cached.
47 48 49 50 51 52 53 54 55 |
# File 'lib/conversant/v3/services/authorization.rb', line 47 def cached = cached_session return cached if cached fetch_new_session rescue StandardError => e logger.debug "#{identifier}.METHOD:authorize.EXCEPTION:#{e.}" nil end |
#authorized_headers ⇒ Hash
Builds authorized HTTP headers with session cookies
Retrieves session cookies from cache or authenticates if needed, then constructs headers with appropriate cookie values.
33 34 35 36 37 38 39 40 |
# File 'lib/conversant/v3/services/authorization.rb', line 33 def = sso_gw_session2 = fetch_sso_session log_missing_session if .nil? && respond_to?(:requires_session?) && requires_session? (, sso_gw_session2) end |
#base_headers ⇒ Hash
Builds base HTTP headers for API requests
113 114 115 116 117 118 119 120 |
# File 'lib/conversant/v3/services/authorization.rb', line 113 def base_headers { 'Authority' => URI.parse(service_endpoint).hostname, 'Content-Type' => configuration.default_content_type, 'Referer' => portal_endpoint, 'User-Agent' => configuration.default_ua } end |
#build_cookie_hash(session_cookie, sso_gw_session2) ⇒ Hash
Builds hash of cookie key-value pairs
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/conversant/v3/services/authorization.rb', line 139 def (, sso_gw_session2) = {} if respond_to?(:session_cookie_name, true) [] = if elsif ['SESSION'] = end ['SSO_GW_SESSION2'] = sso_gw_session2 if sso_gw_session2 end |
#build_headers_with_cookies(session_cookie, sso_gw_session2) ⇒ Hash
Builds HTTP headers hash with cookie values
103 104 105 106 107 108 |
# File 'lib/conversant/v3/services/authorization.rb', line 103 def (, sso_gw_session2) headers = base_headers = (, sso_gw_session2) headers['Cookie'] = if headers end |
#cached_session ⇒ String?
Retrieves cached session from Redis
78 79 80 |
# File 'lib/conversant/v3/services/authorization.rb', line 78 def cached_session redis.get(session_cache_key) end |
#fetch_new_session ⇒ String
Subclasses must implement this method with service-specific logic
Fetches a new session from the service
174 175 176 |
# File 'lib/conversant/v3/services/authorization.rb', line 174 def fetch_new_session raise NotImplementedError, "#{self.class} must implement #fetch_new_session" end |
#fetch_session_cookie ⇒ String?
Fetches session cookie from cache or authorizes to get a new one
62 63 64 65 |
# File 'lib/conversant/v3/services/authorization.rb', line 62 def cached = cached_session cached || end |
#fetch_sso_session ⇒ String?
Fetches SSO session from authentication or cache
70 71 72 73 |
# File 'lib/conversant/v3/services/authorization.rb', line 70 def fetch_sso_session sessions = authenticate sessions&.[](:sso_gw_session2) end |
#format_cookies(session_cookie, sso_gw_session2) ⇒ String?
Formats cookie hash into HTTP Cookie header string
127 128 129 130 131 132 |
# File 'lib/conversant/v3/services/authorization.rb', line 127 def (, sso_gw_session2) = (, sso_gw_session2) return nil if .empty? .map { |k, v| "#{k}=#{v}" }.join('; ') end |
#log_missing_session ⇒ void
This method returns an undefined value.
Logs warning when required session is missing
155 156 157 158 |
# File 'lib/conversant/v3/services/authorization.rb', line 155 def log_missing_session session_type = respond_to?(:session_cookie_name, true) ? : 'SESSION' logger.warn "#{identifier}.METHOD:authorized_headers.NO_#{session_type}" end |
#requires_session? ⇒ Boolean
Indicates whether this service requires a session cookie
188 189 190 |
# File 'lib/conversant/v3/services/authorization.rb', line 188 def requires_session? false end |
#service_endpoint ⇒ String
Service-specific methods that must be implemented
165 166 167 |
# File 'lib/conversant/v3/services/authorization.rb', line 165 def service_endpoint raise NotImplementedError, "#{self.class} must implement #service_endpoint" end |
#session_cache_key ⇒ String
Generates Redis cache key for session storage
85 86 87 88 89 |
# File 'lib/conversant/v3/services/authorization.rb', line 85 def session_cache_key service_name = self.class.name.split('::').last.upcase session_type = respond_to?(:session_cookie_name, true) ? : 'SESSION' "CONVERSANT.V3.#{service_name}.#{session_type}.#{customer_id}" end |
#session_cookie_name ⇒ String
Optional methods that services can override
181 182 183 |
# File 'lib/conversant/v3/services/authorization.rb', line 181 def 'SESSION' end |
#sso_cache_key ⇒ String
Generates Redis cache key for SSO session storage
94 95 96 |
# File 'lib/conversant/v3/services/authorization.rb', line 94 def sso_cache_key "CONVERSANT.V3.PORTAL.SSO_GW_SESSION2.#{customer_id}" end |