Module: JunglePath::API::Helpers::Defaults

Included in:
Server::Base
Defined in:
lib/jungle_path/api/helpers/defaults.rb

Instance Method Summary collapse

Instance Method Details

#set_default_authentication_check(debug_show_params = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jungle_path/api/helpers/defaults.rb', line 62

def set_default_authentication_check debug_show_params=false
  before do
    puts ""
    puts "::::[request_start: #{Time.now.utc}]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
    puts "::::[#{request.env['REMOTE_USER']}: #{request.request_method} #{request.url}"
    puts "(api_helpsers - before do - default authentication check)"
    #puts "[params] #{params.to_h}" if configatron.debug.show_params
    puts "[params] #{params.to_h}" if debug_show_params
    puts "authenticate..."
    # These three request paths return user info, so force no_cache = true so that stale user data is not returned!
    no_cache = request.path_info == '/authenticate' or request.path_info == '/current/user' or request.path_info == '/current/user/auth'
    puts "authenticate no_cache: #{no_cache}."
    authenticate no_cache
  end

  after do
    puts "[request end]"
  end
end

#set_default_error_handling(logger) ⇒ Object



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
# File 'lib/jungle_path/api/helpers/defaults.rb', line 26

def set_default_error_handling logger
  # These must be disabled to allow error methods to fire.
  disable :raise_errors
  disable :show_exceptions

  error JunglePath::Exceptions::MissingRequiredFields do
    e = env['sinatra.error']
    trace = e.backtrace.join("\n")
    logger.error "#{e.message}\n#{trace}."
    halt 400, e.message
  end

  error JunglePath::Exceptions::NotFoundException do
    e = env['sinatra.error']
    trace = e.backtrace.join("\n")
    logger.error "#{e.message}\n#{trace}."
    halt 404, e.message
  end

  error do
    e = env['sinatra.error']
    trace = e.backtrace.join("\n")
    logger.error "#{e.message}\n#{trace}."
    halt 500, e.message
  end
end

#set_default_helpersObject



53
54
55
56
57
58
59
60
# File 'lib/jungle_path/api/helpers/defaults.rb', line 53

def set_default_helpers
  # standard helpers:
  helpers JunglePath::API::Helpers::DataCache
  helpers JunglePath::API::Helpers::Logging
  helpers JunglePath::API::Helpers::Result
  helpers JunglePath::API::Helpers::Auth
  helpers JunglePath::API::Helpers::QueryFilters
end

#set_default_rack_middleware(logger, issue_challenge = true) ⇒ Object

default mixin that may be included in your Sinatra application class.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jungle_path/api/helpers/defaults.rb', line 13

def set_default_rack_middleware logger, issue_challenge=true
  use ::Rack::MobileDetect
  use ::Rack::CommonLogger, logger
  use JunglePath::Rack::JsonBodyParser, true
  # This is rack middleware that adds 'REMOTE_USER' and 'REMOTE_PASSWORD'
  # keys with their associated basic auth values to request.env (if present in the HTTP header).
  # The "Authorization: Basic ..."" header must be present. Looks like this:
  #     Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
  # If this header is missing or incorrect. Error codes are returned.
  # See basic_credentials.rb for details.
  use JunglePath::Rack::BasicCredentials::Basic, "Basic Authentication Required.", issue_challenge
end