ruby-on-rails - 在 SSL 模式下在 nginx 后面运行时,Omniauth 和 open_id 与 Google 损坏

标签 ruby-on-rails nginx openid omniauth

Rails 3.0.12,最新的omniauth,我可以连接到谷歌并获取用户的电子邮件地址就好了。但是后来我在 SSL 模式下在 nginx 后面运行了相同的 rails 应用程序,但它在 Google 页面上失败了:

"The page you requested is invalid."

是我的 nginx 配置吗?我的 omniauth 设置?

我知道X-Forwarded-Proto: https这里有什么特别的东西吗,我还需要做些什么才能让 SSL Web 服务器背后的 openid 满意?

这是完整的示例代码:你可以克隆这个 repo,bundle install ,然后运行 ​​rails s看看它工作正常,然后运行 ​​rake server看到它失败。
https://github.com/jjulian/open_id_ssl

nginx.conf:
worker_processes  2;
pid        tmp/nginx.pid;
error_log  log/error.log;
daemon     off;

events {
}

http {
  client_body_temp_path tmp/body;
  proxy_temp_path       tmp/proxy;
  fastcgi_temp_path     tmp/fastcgi;
  uwsgi_temp_path       tmp/uwsgi;
  scgi_temp_path        tmp/scgi;

  server {
    listen 3000 ssl;
    ssl_certificate      development.crt;
    ssl_certificate_key  development.key;
    ssl_verify_depth     6;

    access_log log/access.log;
    proxy_buffering off;
    location / {
      proxy_pass        http://127.0.0.1:3300;
      proxy_set_header  X-Real-IP        $remote_addr;
      proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header  Host             $http_host;
      proxy_redirect    off;
      proxy_set_header  X-Forwarded-Proto https;
    }
  }
}

omn​​iauth.rb 初始值设定项:
require 'openid/store/filesystem'

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :open_id, :identifier => 'https://www.google.com/accounts/o8/id'
end

路线.rb:
OpenIdSsl::Application.routes.draw do
  match '/auth/open_id/callback' => 'accounts#update'
  match '/auth/failure' => 'accounts#failure'
  root :to => 'accounts#show'
end

更新:此示例使用 Rails 3.1.12 和 OmniAuth 1.0.3。升级到 Rails 3.1.4 和 OmniAuth 1.1.0 修复了这个问题。

最佳答案

发现您的问题,我仍在尝试寻找更清洁的东西,但这是快速而肮脏的解决方法:

在你的 config/initializers/omniauth.rb 中添加:

class Rack::OpenID
  def realm_url(req)
    'https://localhost:3000'
  end
end

现在解释一下:当 rack-openid gem 构建发送到 google openid 服务器的请求时,它使用 rails 应用程序访问 url 而不是 nginx 一个(使用 ssl)在一个地方失败,导致这被发送到openid 服务器:
openid.realm:http://localhost:3001
openid.return_to:https://localhost:3001/auth/open_id/callback

领域使用 http url (rails url) 而 return_to 指向正确的 https url (nginx),当 openid 服务器看到它时,它会停止并返回错误。

PS:如果我设法找到更干净的方法,我会编辑答案。

关于ruby-on-rails - 在 SSL 模式下在 nginx 后面运行时,Omniauth 和 open_id 与 Google 损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032249/

相关文章:

java - 如何在 Java 中实现 OpenID

ruby-on-rails - Ruby on Rails Rake 测试 - 预期 762146111 匹配 762146111

ruby-on-rails - 如何在rails中将静态页面的输入保存到数据库

security - 删除 nginx 反向代理中的特定 cookie

angularjs - Angular2应用的配置——nginx和docker

javascript - 需要 React.js/Redux OpenID 库/示例(特别是 Steam OpenID)

ruby-on-rails - 如何在无表 Rails 4 模型中添加枚举支持

ruby-on-rails - 一位经验丰富的程序员,对如何从头开始编写 Rails 应用程序感到困惑

email - PHP 进程终止时发送电子邮件的最佳方式

ruby-on-rails - 使用/不使用 OAuth 和 OpenID 构建 API