Rails中的Google Apps和Open ID身份验证 - 安全性

时间:2010-08-09 16:40:08

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

我正在移动一个应用程序,只为应用程序使用Google联合登录(OpenID)(我们使用谷歌应用程序处理所有内容,并认为将用户管理结合起来会更容易)。虽然我可以成功登录并创建用户,但我现在的想法是安全性......

当用户登录时,我只有一个“登录”按钮 - 没有别的。网站域是硬编码的(SITE_DOMAIN显示在下方),用户被重定向到典型的谷歌登录页面。

以下是代码:

    def create
    open_id_authentication
  end

  protected

  def open_id_authentication
    openid_url = 'https://www.google.com/accounts/o8/site-xrds?hd=SITE_DOMAIN'
    authenticate_with_open_id(openid_url, 
                              :required => ['http://axschema.org/contact/email',
                                            'http://axschema.org/namePerson/first',
                                            'http://axschema.org/namePerson/last']) do |result, identity_url, registration|
      case result.status
      when :missing
        failed_login "Sorry, the OpenID server couldn't be found"
      when :invalid
        failed_login "Sorry, but this does not appear to be a valid OpenID"
      when :canceled
        failed_login "OpenID verification was canceled"
      when :failed
        failed_login "Sorry, the OpenID verification failed"
      when :successful
        if @current_user = User.find_by_id_url(identity_url)
          if @current_user.login_from(request.env['REMOTE_ADDR'])
            successful_login
          else
            failed_login "Your OpenID profile registration failed: " + @current_user.errors.full_messages.to_sentence
          end
        else
          ax_response = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
          @current_user = User.login_create(ax_response, identity_url, request.env['REMOTE_ADDR'])
          successful_login
        end
      end
    end
  end

成功登录后,我只需将用户保存到会话中......

session[:current_user] = @current_user

...并在Application控制器中使用一个简单的current_user方法......

  def current_user
    return session[:current_user] if defined?(session[:current_user])
  end

我主要担心的是安全问题。 OpenIDAuthentication使用的是内存存储,总体而言,这似乎有点太容易实现(在阅读了大量文档之后)。基本测试显示这很好,但我很紧张。 :)

有什么想法吗?

我正在使用open_id_authentication插件和基本的ruby openid gem(使用ruby-openid-apps-discovery gem for google apps)

1 个答案:

答案 0 :(得分:1)

由于omniauth,现在更加容易了。