module SoarAuthenticationToken class CascadeProvider def initialize(configuration) @configuration = configuration validate_configuration end def fetch(request) # TODO: refactor this functionality into modules - DON'T DUPLICATE! fetch_by_cookie(request) || fetch_by_auth_header(request) end private def fetch_by_cookie(request) return false unless request.env.has_key?('HTTP_COOKIE') cookies = HTTP::CookieJar.new.parse(request.env['HTTP_COOKIE'], 'http://irrelevant') auth_cookie = cookies.find { |cookie| cookie.name == @configuration['cookie_name'] } return false unless auth_cookie.is_a?(HTTP::Cookie) auth_cookie.value end def fetch_by_auth_header(request) return false unless request.env[@configuration['header_name']] request.env[@configuration['header_name']] end def validate_configuration raise "'cookie_name' must be configured" unless @configuration['header_name'] end end end