2010-01-14 14 views

Respuesta

11
require 'sinatra' 

helpers do 
    def protected! 
    unless authorized? 
     response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth") 
     throw(:halt, [401, "Not authorized\n"]) 
    end 
    end 

    def authorized? 
    @auth ||= Rack::Auth::Basic::Request.new(request.env) 
    @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin'] 
    end 
end 

before { protected! unless request.path_info == "/public" } 

get('/public') { "I'm public!" } 
+0

Impresionante. ¡Gracias! –

Cuestiones relacionadas