Class: SoarSmaak::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/soar_smaak/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Router

Returns a new instance of Router.



5
6
7
# File 'lib/soar_smaak/router.rb', line 5

def initialize(app)
  @app = app
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



3
4
5
# File 'lib/soar_smaak/router.rb', line 3

def configuration
  @configuration
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/soar_smaak/router.rb', line 16

def call(env)
  @configuration = env['configuration']
  @auditing = env['auditing']
  @signed_routes = env['signed_routes']
  SoarSmaak::SecureService::auditing = @auditing
  secure_service = SoarSmaak::SecureService.get_instance(@configuration)
  @server = secure_service.smaak_server
  @auditing.debug("Smaak: signed routes are: #{@signed_routes}")
  request = Rack::Request.new(env)
  if SoarSmaak::Interpreter::smaak_request?(request) or @signed_routes[request.path]
    @auditing.debug("Smaak: routing smaak request")
    begin
      auth_message, body = smaak(request)
    rescue => ex
      return [500, {"Content-Type" => "text/html"}, ['Unable to route request on SMAAK route. Was your request a SMAAK request?']]
    end
    return [403, {}, [" 403 - Not authorized"]] if not auth_message or not auth_message.identifier
    @auditing.debug("Smaak: request authorized for #{auth_message.identifier}")
    session = update_session_with_smaak(request, auth_message)
    env["rack.input"] = body
  else
    @auditing.debug("routing non-smaak request")
  end

  http_code, content_type, body = @app.call(env)

  if SoarSmaak::Interpreter::smaak_request?(request) or @signed_routes[request.path]
    @auditing.debug("Smaak: request authorized for #{auth_message.identifier}")
    return [http_code, content_type, [@server.compile_response(auth_message, body[0])]] if auth_message.encrypt
    return [http_code, content_type, @server.compile_response(auth_message, body)]
  else
    return http_code, content_type, body
  end
# rescue => ex
#   puts ex
#   [500, {"Content-Type" => "text/html"}, ['Unable to route request on SMAAK route. Was your request a SMAAK request?']]
end

#smaak(request) ⇒ Object



9
10
11
12
13
14
# File 'lib/soar_smaak/router.rb', line 9

def smaak(request)
  @auditing.debug("Smaak: verifying request")
  auth_message, body = @server.verify_signed_request(request)
  @auditing.debug("Smaak: auth_message: #{auth_message}")
  return auth_message, body
end