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

2024-02-25

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

"The page you requested is invalid."

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

我知道X-Forwarded-Proto: https这是这里的特别之处,我还需要做些什么才能让 OpenID 在 SSL Web 服务器后面满意吗?

这是完整的示例代码:您可以克隆此存储库,bundle install,然后运行rails s看看它工作正常,然后运行rake server看到它失败。https://github.com/jjulian/open_id_ssl 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

UPDATE:此示例使用 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)的地方失败,导致该请求被发送到开放ID服务器:

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:如果我设法找到更干净的方法,我将编辑答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 SSL 模式下在 nginx 后面运行时,Omniauth 和 open_id 与 Google 损坏 的相关文章

随机推荐