在SSL模式下运行nginx后,Omniauth和open_id与Google断了

Jon*_*ian 6 openid ruby-on-rails nginx omniauth

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

"The page you requested is invalid."
Run Code Online (Sandbox Code Playgroud)

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

我知道X-Forwarded-Proto: https这里有特殊的酱汁,还有什么我需要做的就是在SSL网络服务器后面获得开心的快乐吗?

这是完整的示例代码:您可以克隆此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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

routes.rb中:

OpenIdSsl::Application.routes.draw do
  match '/auth/open_id/callback' => 'accounts#update'
  match '/auth/failure' => 'accounts#failure'
  root :to => 'accounts#show'
end
Run Code Online (Sandbox Code Playgroud)

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

Sch*_*rfy 2

发现你的问题,我仍在尝试寻找更干净的东西,但这是快速和肮脏的修复:

将其添加到您的 config/initializers/omniauth.rb 中:

class Rack::OpenID
  def realm_url(req)
    'https://localhost:3000'
  end
end
Run Code Online (Sandbox Code Playgroud)

现在解释一下:当rack-openid gem构建发送到google openid服务器的请求时,它在使用rails应用程序访问url而不是nginx(使用ssl)的地方失败,导致该请求被发送到开放ID服务器:

openid.realm:http://localhost:3001
openid.return_to:https://localhost:3001/auth/open_id/callback
Run Code Online (Sandbox Code Playgroud)

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

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