module Sinatra::Nedry

Constants

NOT_AUTH_TEMPLATE

Public Instance Methods

flagged!(&blk) click to toggle source
# File lib/sinatra/nedry.rb, line 11
def flagged!(&blk)
  if authorized? || ENV["RACK_ENV"] != "production"
    blk.call
  else
    not_authorized
  end
end
protected!(&blk) click to toggle source
# File lib/sinatra/nedry.rb, line 3
def protected!(&blk)
  if authorized?
    blk.call
  else
    not_authorized
  end
end

Private Instance Methods

authorized?() click to toggle source
# File lib/sinatra/nedry.rb, line 21
def authorized?
  admin_credentials = if ENV["RACK_ENV"] == "production"
    [ENV["ADMIN_USERNAME"], ENV["ADMIN_PASSWORD"]]
  else
    ["admin", "admin"]
  end      
  
  @auth ||= Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == admin_credentials
end
not_authorized() click to toggle source
# File lib/sinatra/nedry.rb, line 32
def not_authorized
  headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
  halt 401, NOT_AUTH_TEMPLATE
end