Ruby: Sinatra で Basic 認証

basicauth.rb

require 'sinatra'
require 'sinatra/reloader'

helpers do
    def protected!
        unless authorized?
            response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
            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 == ['name', 'password']
    end
end

get '/admin' do
    protected!
    "ベーシック認証突破サレチャッタネー"
end

get '/' do
    "普通のページ"
end
$ ruby basicauth.rb