Module: Sinatra::DebugConsole::Authorization

Defined in:
lib/sinatra_debug_console.rb

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/sinatra_debug_console.rb', line 85

def admin?
  authorized?
end

#authObject



56
57
58
# File 'lib/sinatra_debug_console.rb', line 56

def auth
  @auth ||= Rack::Auth::Basic::Request.new(request.env)
end

#authorize(username, password) ⇒ Object



73
74
75
# File 'lib/sinatra_debug_console.rb', line 73

def authorize(username, password)
  username == DebugConsole.username && password == DebugConsole.password
end

#authorized?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/sinatra_debug_console.rb', line 69

def authorized?
  request.env['REMOTE_USER']
end

#bad_request!Object



65
66
67
# File 'lib/sinatra_debug_console.rb', line 65

def bad_request!
  throw :halt, [ 400, 'Bad Request' ]
end

#require_administrative_privilegesObject



77
78
79
80
81
82
83
# File 'lib/sinatra_debug_console.rb', line 77

def require_administrative_privileges
  return if authorized?
  unauthorized! unless auth.provided?
  bad_request! unless auth.basic?
  unauthorized! unless authorize(*auth.credentials)
  request.env['REMOTE_USER'] = auth.username
end

#unauthorized!(realm = "sinatra_debug_console") ⇒ Object



60
61
62
63
# File 'lib/sinatra_debug_console.rb', line 60

def unauthorized!(realm = "sinatra_debug_console")
  header 'WWW-Authenticate' => %(Basic realm="#{realm}")
  throw :halt, [ 401, 'Authorization Required' ]
end