我正在编写一个 Sinatra Rack 应用程序,我想使用 Warden 进行身份验证。我正在使用 heroku 的工具带,所以我使用工头来运行我的应用程序。我发现了一些可能应该让这个工作的代码。不幸的是,当我尝试实际访问 Warden env 对象时,它是 nil。
我尝试使用sinatra_warden gem,但它也有自己的错误(可能与这个有关)。
配置.ru:
require './web.rb'
use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
run MyApp
网络.rb:
require 'sinatra'
require 'warden'
require 'data_mapper'
require './config/datamapper.rb'
require './config/warden.rb' # I've tried this inside of MyApp, still didn't work
class MyApp < Sinatra::Base
get '/test' do
env['warden'].authenticate! # env['warden'] is nil :(
end
end
配置/warden.rb:
use Rack::Session::Cookie, :secret => ENV['SESSION_SECRET']
use Warden::Manager do |manager|
manager.default_strategies :password
manager.failure_app = MyApp.new
end
Warden::Manager.serialize_into_session { |user| user.id }
Warden::Manager.serialize_from_session { |id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
Warden::Strategies.add(:password) do
def valid?
params["email"] || params["password"]
end
def authenticate!
u = User.authenticate(params["email"], params["password"])
u.nil? ? fail!("Could not log in") : success!(u)
end
end
版本:
- 西纳特拉:1.1.0
- 典狱长:1.2.1
- 机架:1.4.1
- 红宝石:1.9.3p194
- 工头:0.60.0
任何想法如何使用我描述的设置的守望者?
(PS出于好奇,env变量到底是什么?)