Module: Conversant::V3::Services::Authorization

Included in:
CDN, LMS, OSS, Portal, VMS
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.

Examples:

Including in a service class

class MyService < Conversant::V3::BaseService
  include Conversant::V3::Services::Authorization

  def fetch_new_session
    # Custom session fetching logic
  end

  def service_endpoint
    'https://api.example.com'
  end
end

Instance Method Summary collapse

Instance Method Details

#authorizeString?

Authorizes and retrieves a session cookie

Checks Redis cache first, then fetches a new session if not cached.

Returns:

  • (String, nil)

    The session cookie value, or nil if authorization fails



47
48
49
50
51
52
53
54
55
# File 'lib/conversant/v3/services/authorization.rb', line 47

def authorize
  cached = cached_session
  return cached if cached

  fetch_new_session
rescue StandardError => e
  logger.debug "#{identifier}.METHOD:authorize.EXCEPTION:#{e.message}"
  nil
end

#authorized_headersHash

Builds authorized HTTP headers with session cookies

Retrieves session cookies from cache or authenticates if needed, then constructs headers with appropriate cookie values.

Returns:

  • (Hash)

    HTTP headers including Cookie header if sessions exist



33
34
35
36
37
38
39
40
# File 'lib/conversant/v3/services/authorization.rb', line 33

def authorized_headers
  session_cookie = fetch_session_cookie
  sso_gw_session2 = fetch_sso_session

  log_missing_session if session_cookie.nil? && respond_to?(:requires_session?) && requires_session?

  build_headers_with_cookies(session_cookie, sso_gw_session2)
end

#base_headersHash

Builds base HTTP headers for API requests

Returns:

  • (Hash)

    Base headers with Authority, Content-Type, Referer, and User-Agent



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

Builds hash of cookie key-value pairs

Parameters:

  • session_cookie (String, nil)

    The session cookie value

  • sso_gw_session2 (String, nil)

    The SSO session cookie value

Returns:

  • (Hash)

    Cookie hash with appropriate keys based on service configuration



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/conversant/v3/services/authorization.rb', line 139

def build_cookie_hash(session_cookie, sso_gw_session2)
  cookies = {}

  if respond_to?(:session_cookie_name, true)
    cookies[session_cookie_name] = session_cookie if session_cookie
  elsif session_cookie
    cookies['SESSION'] = session_cookie
  end

  cookies['SSO_GW_SESSION2'] = sso_gw_session2 if sso_gw_session2
  cookies
end

#build_headers_with_cookies(session_cookie, sso_gw_session2) ⇒ Hash

Builds HTTP headers hash with cookie values

Parameters:

  • session_cookie (String, nil)

    The session cookie value

  • sso_gw_session2 (String, nil)

    The SSO session cookie value

Returns:

  • (Hash)

    Complete HTTP headers including Cookie header if applicable



103
104
105
106
107
108
# File 'lib/conversant/v3/services/authorization.rb', line 103

def build_headers_with_cookies(session_cookie, sso_gw_session2)
  headers = base_headers
  cookie_header = format_cookies(session_cookie, sso_gw_session2)
  headers['Cookie'] = cookie_header if cookie_header
  headers
end

#cached_sessionString?

Retrieves cached session from Redis

Returns:

  • (String, nil)

    The cached session value



78
79
80
# File 'lib/conversant/v3/services/authorization.rb', line 78

def cached_session
  redis.get(session_cache_key)
end

#fetch_new_sessionString

This method is abstract.

Subclasses must implement this method with service-specific logic

Fetches a new session from the service

Returns:

  • (String)

    The new session cookie value

Raises:

  • (NotImplementedError)

    if not implemented by subclass



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

Fetches session cookie from cache or authorizes to get a new one

Returns:

  • (String, nil)

    The session cookie value



62
63
64
65
# File 'lib/conversant/v3/services/authorization.rb', line 62

def fetch_session_cookie
  cached = cached_session
  cached || authorize
end

#fetch_sso_sessionString?

Fetches SSO session from authentication or cache

Returns:

  • (String, nil)

    The SSO_GW_SESSION2 cookie value



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

Parameters:

  • session_cookie (String, nil)

    The session cookie value

  • sso_gw_session2 (String, nil)

    The SSO session cookie value

Returns:

  • (String, nil)

    Formatted cookie string "key1=value1; key2=value2" or nil if no cookies



127
128
129
130
131
132
# File 'lib/conversant/v3/services/authorization.rb', line 127

def format_cookies(session_cookie, sso_gw_session2)
  cookies = build_cookie_hash(session_cookie, sso_gw_session2)
  return nil if cookies.empty?

  cookies.map { |k, v| "#{k}=#{v}" }.join('; ')
end

#log_missing_sessionvoid

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_cookie_name : 'SESSION'
  logger.warn "#{identifier}.METHOD:authorized_headers.NO_#{session_type}"
end

#requires_session?Boolean

Indicates whether this service requires a session cookie

Returns:

  • (Boolean)

    true if session is required, false otherwise



188
189
190
# File 'lib/conversant/v3/services/authorization.rb', line 188

def requires_session?
  false
end

#service_endpointString

Service-specific methods that must be implemented

Returns:

  • (String)


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_keyString

Generates Redis cache key for session storage

Returns:

  • (String)

    Redis key in format "CONVERSANT.V3.SERVICE.SESSION_TYPE.CUSTOMER_ID"



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_cookie_name : 'SESSION'
  "CONVERSANT.V3.#{service_name}.#{session_type}.#{customer_id}"
end

Optional methods that services can override

Returns:

  • (String)


181
182
183
# File 'lib/conversant/v3/services/authorization.rb', line 181

def session_cookie_name
  'SESSION'
end

#sso_cache_keyString

Generates Redis cache key for SSO session storage

Returns:

  • (String)

    Redis key in format "CONVERSANT.V3.PORTAL.SSO_GW_SESSION2.CUSTOMER_ID"



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