0

我的用户可能有多个键,我希望它们仅通过查看它们就可以区分。我想在每个令牌类型之前添加几个字符(所以我想ses_在每个会话令牌之前添加)。

有没有办法做到这一点?我正在使用 Devise(我相信它使用 bcrypt?)。我已经阅读了 Devise 的整个文档,但我找不到在哪里覆盖令牌标记化。

4

1 回答 1

0

我看到的最不痛苦的场景是与监狱长挂钩,就像在这个要点中一样:

https://gist.github.com/r00k/906356

在你的情况下看起来像:

# In config/initializers/local_override.rb:

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LocalOverride < Authenticatable
      def valid?
        # If token came as: "Authorization: SES blahblahhash"
        token.provided? && ("ses" == token.scheme ||
        # If token came as: "Authorization: SOMEOTHER blahblahhash"
        "someother" == token.scheme)
        # shorter: token.provided? && ["ses", "someother"].include? token.scheme
      end

      def token
        @token ||= Rack::Auth::AbstractRequest.new(env)
      end

      def authenticate!

          # assuming that you name columns in pattern : <scheme_name>_token
          # so ses_token and someother_token would be just regular columns
          # and you don't have to do any extra devise magic about them outside this strategy
          # token.params will be a string containing blahblahhash
          if user = User.where(token.scheme + "_token" => token.params).first
            success!(user)
          else
            fail
          end 
      end 
    end 
  end 
end

Warden::Strategies.add(:local_override, Devise::Strategies::LocalOverride)



# In config/initializers/devise.rb

config.warden do |manager|
  manager.default_strategies(:scope => :user).unshift :local_override
于 2013-06-24T15:59:25.710 回答