class SoarScRouting::RouterMeta

Constants

AUTHORIZED
SIGNED
UNAUTHORIZED
UNSIGNED

Attributes

autenticate[RW]
configuration[RW]
lexicon[RW]
name[R]
policy_am[RW]
routing[RW]
service_names[RW]
signed_routes[RW]

Public Class Methods

new(configuration,name = 'router') click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 24
def initialize(configuration,name = 'router')
  @configuration = configuration
  @lexicon = {}
  @routing = {}
  @signed_routes = {}
  @service_names = {}
  @name = name
  setup_routing_table
end

Public Instance Methods

access_manager() click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 34
def access_manager
  # TODO: Remove coupling to SoarSc::service_registry
  provider = Soar::Authorization::AccessManager::Provider::ServiceRegistry.new(SoarSc::service_registry)
  @policy_am ||= Soar::Authorization::AccessManager.new(provider)
  @policy_am
end
add_route(path, description, &block) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 55
def add_route(path, description, &block)
  resource = SoarScRouting::Resource.new(description, generate_id(path))
  add_resource_route(path, resource, UNSIGNED, AUTHORIZED, &block)
end
add_signed_route(path, description, &block) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 65
def add_signed_route(path, description, &block)
  resource = SoarScRouting::Resource.new(description, generate_id(path))
  add_resource_route(path, resource, SIGNED, AUTHORIZED, &block)
end
add_signed_unauthorized_route(path, description, &block) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 70
def add_signed_unauthorized_route(path, description, &block)
  resource = SoarScRouting::Resource.new(description, generate_id(path))
  add_resource_route(path, resource, SIGNED, UNAUTHORIZED, &block)
end
add_unsigned_unauthorized_route(path, description, &block) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 60
def add_unsigned_unauthorized_route(path, description, &block)
  resource = SoarScRouting::Resource.new(description, generate_id(path))
  add_resource_route(path, resource, UNSIGNED, UNAUTHORIZED, &block)
end
register_route(detail, startup_flow_id = nil) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 41
def register_route(detail, startup_flow_id = nil)
  validate_detail(detail)
  info("Registering service: #{detail} on router #{@name}", startup_flow_id)
  resource = SoarScRouting::Resource.new(detail['description'], "#{detail['service_name']}", upcase(detail['method']), detail['params'])
  add_resource_route(detail['path'], resource, interpret_secured(detail), interpret_authorized(detail)) do |request|
    if detail['controller']
      delegate_to_controller_and_renderer(detail, startup_flow_id, request)
    elsif detail['action']
      http_code, headers, body = detail['action'].call(request)
      [http_code, headers, [body]]
    end
  end
end

Protected Instance Methods

render_view(detail, http_code, body) click to toggle source

IoC renderer for views

# File lib/soar_sc_routing/router_meta.rb, line 82
def render_view(detail, http_code, body)
  raise NotImplementedError.new "No renderer"
end
setup_routing_table() click to toggle source

Inversion of control

# File lib/soar_sc_routing/router_meta.rb, line 78
def setup_routing_table
end

Private Instance Methods

add_resource_route(path, resource, signed, authorization_required, &block) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 134
def add_resource_route(path, resource, signed, authorization_required, &block)
  @routing[path] = block
  @service_names[path] = resource.id
  @lexicon[path] = resource.content
  @signed_routes[path] = signed
  SoarAuthorization::Authorize::register_access_manager(path, resource.id, access_manager) if authorization_required
end
delegate_to_controller_and_renderer(detail, startup_flow_id, request) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 100
def delegate_to_controller_and_renderer(detail, startup_flow_id, request)
  controller = nil
  begin
    controller = Object::const_get("SoarSc::Web::Controllers::#{detail['controller']}").new(@configuration)
  rescue NameError => ex
    warn("Could not instantiate SoarSc::Web::Controllers::#{detail['controller']}. Trying class name as-is", startup_flow_id)
    controller = Object::const_get(detail['controller']).new(@configuration)
  end
  http_code, body = controller.serve(request)
  render_view(detail, http_code, body)
end
generate_id(path) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 130
def generate_id(path)
  path.gsub("/","_")
end
info(message, startup_flow_id) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 112
def info(message, startup_flow_id)
  auditing = SoarAspects::Aspects::auditing
  if auditing
    auditing.info(message, startup_flow_id)
  else
    $stderr.puts(message)
  end
end
interpret_authorized(detail) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 151
def interpret_authorized(detail)
  authorized = 'AUTHORIZED' # default to strong
  if (not detail['nfrs'].nil?) and (not detail['nfrs']['authorization'].nil?) and (detail['nfrs']['authorization'].is_a?(String))
    authorized = detail['nfrs']['authorization']
    authorized = 'AUTHORIZED' if not ['AUTHORIZED', 'UNAUTHORIZED'].include?(detail['nfrs']['authorization'])
  end
  authorized == 'AUTHORIZED'
end
interpret_secured(detail) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 142
def interpret_secured(detail)
  secured = 'SIGNED' # default to strong
  if (not detail['nfrs']['secured'].nil?) and detail['nfrs']['secured'].is_a?(String)
    secured = 'SIGNED' if detail['nfrs']['secured'].upcase.strip == 'SIGNED'
    secured = nil if detail['nfrs']['secured'].upcase.strip == 'UNSIGNED'
  end
  secured == 'SIGNED'
end
upcase(methods) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 160
def upcase(methods)
  return methods.map(&:upcase) if methods.is_a?(Array)
  methods.upcase if methods.is_a?(String)
end
validate_detail(detail) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 88
def validate_detail(detail)
  raise ArgumentError.new("detail must not be nil") if detail.nil?
  raise ArgumentError.new("path must be provided") if detail['path'].nil?
  raise ArgumentError.new("path must start with /") if detail['path'][0] != '/'
  raise ArgumentError.new("description must be provided") if detail['description'].nil?
  raise ArgumentError.new("method must be provided") if detail['method'].nil?
  raise ArgumentError.new("service_name must be provided") if detail['service_name'].nil?
  raise ArgumentError.new("nfrs must be provided") if detail['nfrs'].nil?
  raise ArgumentError.new("nfrs['secured'] must be provided") if detail['nfrs']['secured'].nil?
  raise ArgumentError.new("nfrs['authorization'] must be provided") if detail['nfrs']['authorization'].nil?
end
warn(message, startup_flow_id) click to toggle source
# File lib/soar_sc_routing/router_meta.rb, line 121
def warn(message, startup_flow_id)
  auditing = SoarAspects::Aspects::auditing
  if auditing
    auditing.warn(message, startup_flow_id)
  else
    $stderr.puts(message)
  end
end