传递google身份验证的其他参数 - rails

时间:2011-07-25 19:46:16

标签: ruby-on-rails-3 openid rack google-authentication

如何使用Rack :: OpenID传递其他参数,例如openid.ns.papeopenid.pape.max_auth_age进行Google身份验证

def new 
  response.headers['WWW-Authenticate'] = Rack::OpenID.build_header(
      :identifier => "https://www.google.com/accounts/o8/id",
      :required => ["http://axschema.org/contact/email"],
      :return_to => accounts_url,
      :method => 'POST')
    head 401
end

我只想强制进行身份验证的新会话 我是rails openid的新手 提前谢谢。

1 个答案:

答案 0 :(得分:1)

经过大量的Google搜索后,我发现了在openid.ns.pape中传递Rack::OpenID参数的灵魂。解决方案是在我的personal blog中发布的。

我刚刚完成了一个用于解决此问题的Monkey补丁。这是补丁

require 'openid/extensions/pape'

module Rack
  class OpenID

    private
      def begin_authentication(env, qs)
        req = Rack::Request.new(env)
        params = self.class.parse_header(qs)
        session = env["rack.session"]

        unless session
          raise RuntimeError, "Rack::OpenID requires a session"
        end

        consumer = ::OpenID::Consumer.new(session, @store)
        identifier = params['identifier'] || params['identity']
        immediate = params['immediate'] == 'true'

        begin
          oidreq = consumer.begin(identifier)
          add_simple_registration_fields(oidreq, params)

           unless params['pape'].nil?
            add_pape(oidreq,params['pape'])
          end

          add_attribute_exchange_fields(oidreq, params)
          add_oauth_fields(oidreq, params)
          url = open_id_redirect_url(req, oidreq, params["trust_root"], params["return_to"], params["method"], immediate)
          return redirect_to(url)
        rescue ::OpenID::OpenIDError, Timeout::Error => e
          env[RESPONSE] = MissingResponse.new
          return @app.call(env)
        end
      end


     def add_pape(oidreq,max_auth_age)
        papereq = ::OpenID::PAPE::Request.new
        papereq.add_policy_uri(::OpenID::PAPE::AUTH_PHISHING_RESISTANT)
        papereq.max_auth_age = max_auth_age
        oidreq.add_extension(papereq)
        oidreq.return_to_args['did_pape'] = 'y'
    end
  end
end

因为我在rails应用程序中需要这个,所以我将上面的代码保存在initializers(config / initializers / rack_openid_patch.rb)文件夹中。如果你没有使用rails,我认为你需要保存它并在你的项目中手动需要。

现在您需要将pape选项添加到Rack :: OpenID.build_header

max_auth_age = 0
response.headers['WWW-Authenticate'] = Rack::OpenID.build_header(
      :identifier => "https://www.google.com/accounts/o8/id",
      :required => ["http://axschema.org/contact/email"],
      :return_to => accounts_url,
      :pape => max_auth_age,
      :method => 'POST')
head 401

确保设置max_auth_age = 0,强制进行新会话的Google身份验证