简体   繁体   English

设计忽略自定义策略

[英]Devise ignoring custom strategy

This is just plain weird. 这简直太奇怪了。

I've got Rails 3 RC running with Devise installed. 我安装了Devise,运行Rails 3 RC。 I've defined a custom strategy to try and use Kerberos for authentication. 我已经定义了一个自定义策略来尝试使用Kerberos进行身份验证。

module Devise
  module Strategies
    class Kerb < Devise::Strategies::Base
      def valid?
        params[:username] || params[:password]
      end

      def authenticate!
        # cheap debugging
        puts "PARAMS: #{params}"

        if check_kerb_auth(params[:username], params[:password])
          # create user account if none exists
          u = User.find(:first, :conditions => { :username => params[:username] }) || User.create({ :username => login })
          success!(u)
        else
          fail!("Could not log in")
        end
      end

      def check_kerb_auth(username, password)
        require 'krb5_auth'
        include Krb5Auth

        return false if username.blank? or password.blank?

        begin
            kerberos = Krb5.new
            return kerberos.get_init_creds_password(username, password)
        rescue Krb5Auth::Krb5::Exception
            return false
        end
      end
    end
  end
end

I have the Devise Warden configuration setup as follows: 我有Devise Warden配置设置如下:

config.warden do |manager|
  manager.strategies.add(:kerb, Devise::Strategies::Kerb)
  manager.default_strategies :kerb
end

I get no errors in my log. 我的日志中没有错误。 Everything seems to work ok. 一切似乎都运转正常。 If I add "cheap debugging" aka a bunch of puts statements, it seems to reflect that the :kerb strategy is the default. 如果我添加“廉价调试”又称一堆put语句,它似乎反映出:curb策略是默认的。 Here is a sample set of logs from a login attempt: 以下是登录尝试的一组示例日志:

=> Booting WEBrick
=> Rails 3.0.0.rc application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-08-17 10:50:35] INFO  WEBrick 1.3.1
[2010-08-17 10:50:35] INFO  ruby 1.8.7 (2010-01-10) [x86_64-linux]
[2010-08-17 10:50:40] INFO  WEBrick::HTTPServer#start: pid=12717 port=3000


Started POST "/users/login" for 127.0.0.1 at Tue Aug 17 10:50:43 -0400 2010
  Processing by Devise::SessionsController#create as HTML
  Parameters: {"commit"=>"Login", "authenticity_token"=>"afZF6ho96p47dc9LQFwwNN5PqnRpl7x+1J7V3MiKgTE=", "_snowman"=>"\342\230\203", "user"=>{"remember_me"=>"1", "username"=>"hernan43", "password"=>"[FILTERED]"}}
Completed   in 0ms
  Processing by Devise::SessionsController#new as HTML
  Parameters: {"commit"=>"Login", "authenticity_token"=>"afZF6ho96p47dc9LQFwwNN5PqnRpl7x+1J7V3MiKgTE=", "_snowman"=>"\342\230\203", "user"=>{"remember_me"=>"1", "username"=>"hernan43", "password"=>"[FILTERED]"}}
Rendered devise/shared/_links.erb (1.2ms)
Rendered devise/sessions/new.html.erb within layouts/application (8.2ms)
Completed 200 OK in 124ms (Views: 11.7ms | ActiveRecord: 1.3ms)

The kerberos code works in other things on the same machine. kerberos代码可以在同一台机器上运行。 I was sort of expecting it to show a bunch of errors if there was a problem but I am getting nothing. 如果出现问题,我有点期待它显示一堆错误,但我什么都没得到。 Is there a good way to debug Devise/Warden? 有没有一个很好的方法来调试Devise / Warden?

In case someone else comes across this, here's what I believe the problem is: 如果其他人遇到这个问题,我认为问题是:

According to Warden Strategies : 根据Warden Strategies的说法:

valid? 有效?

The valid? 有效吗? method acts as a guard for the strategy. 方法充当策略的守卫。 It's optional to declare a valid? 声明有效是可选的吗? method, and if you don't declare it, the strategy will always be run. 方法,如果您不声明它,将始终运行策略。 If you do declare it though, the strategy will only be tried if #valid? 如果您确实声明了它,那么只有在#valid时才会尝试策略? evaluates to true. 评估为真。

The strategy above is reasoning that if there's either a 'username' or a 'password' param, then the user is trying to login. 上面的策略是推断,如果有'用户名'或'密码'参数,那么用户正在尝试登录。 If there's only one of them, then the 'User.authenticate' call will fail, but it was still the desired (valid) strategy. 如果只有其中一个,那么'User.authenticate'调用将失败,但它仍然是所需的(有效)策略。

So your valid method: 所以你的有效方法:

def valid?
  params[:username] || params[:password]
end

It's returning false, so the authenticate! 它返回false,所以authenticate! is never called. 永远不会被称为。 params is a nested hash, so it should be params[:user][:username] instead of params[:username] . params是一个嵌套的哈希值,因此它应该是params[:user][:username]而不是params[:username]

Changing your valid method to: 将有效方法更改为:

def valid?
  params[:user] && (params[:user][:username] || params[:user][:password])
end

will return true and cause the authenticate! 将返回true并导致authenticate! method to be called. 要调用的方法。

I have run into a similar problem. 我遇到了类似的问题。 After a short session of debugging I found out the reason. 经过短暂的调试后,我发现了原因。 My user was not confirmed, so after initial successful signing in with my strategy, he was logged out by one of the following modules which is confirmable module :) 我的用户没有得到确认,所以在我最初成功登录我的策略后,他被以下模块中的一个登出,这是可确认的模块:)

Btw, the easiest way to debug rails application is to use following code: 顺便说一句,调试rails应用程序的最简单方法是使用以下代码:

require 'ruby-debug'
Debugger.wait_connection = true
Debugger.start_remote
debugger

and then rdebug -c from terminal. 然后从终端rdebug -c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM