Rails Devise omniauth 如何同时部署 2 个 sina 的 app key 和 secret?

nanzhang · 2012年09月21日 · 最后由 NanZhang 回复于 2012年09月22日 · 2593 次阅读

RT.求指导,很急~~ 一般 devise.rb 中只能配置一个 provider :weibo

provider 可以换名字啊

@suupic 不是换名字,而是同时添加 2 个相同的 比如 provider :weibo, key,secret provider :weibo, key,secret

@suupic

devise.rb

config.omniauth :weibo, :setup => SETUP_PROC

SETUP_PROC = lambda do |env|
  if Rack::Request.new(env).env["HTTP_REFERER"].match %r(app2)
    client_id,client_secret = "",""
  else
    client_id,client_secret = "",""
  end  

  env['omniauth.strategy'].options[:client_id] = client_id
  env['omniauth.strategy'].options[:client_secret] = client_secret
end
https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb#L292

# The setup phase looks for the `:setup` option to exist and,
    # if it is, will call either the Rack endpoint supplied to the
    # `:setup` option or it will call out to the setup path of the
    # underlying application. This will default to `/auth/:provider/setup`.
    def setup_phase
      if options[:setup].respond_to?(:call)
        log :info, "Setup endpoint detected, running now."
        options[:setup].call(env)
      elsif options.setup?
        log :info, "Calling through to underlying application for setup."
        setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
        call_app!(setup_env)
      end
    end

这个需求太奇怪了,为什么同一个 provider 要有两个 key?

@suupic 不同的 2 个应用写在一个 rails 的程序里面,应用的授权页面不一样所以要分开

补充:

SETUP_PROC = lambda do |env|
  request = Rack::Request.new(env).env["HTTP_REFERER"] || Rack::Request.new(env).env["rack.session"]["omniauth.origin"]
  filter = request.split("/")

  client_id,client_secret = "","" if filter.include? "app1" 
  client_id,client_secret = "","" if filter.include? "app2"

  env['omniauth.strategy'].options[:client_id] = client_id
  env['omniauth.strategy'].options[:client_secret] = client_secret
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号