ruby - "CSRF detected"与 Omniauth 和谷歌

标签 ruby oauth-2.0 sinatra omniauth

我明白了

OmniAuth::Strategies::OAuth2::CallbackError at /auth/google/callback csrf_detected | CSRF detected

我的代码:

require 'sinatra'
require "sinatra/json"
require "sinatra/config_file"
require 'omniauth-oauth2'
require 'omniauth-google-oauth2'

use Rack::Logger

config_file "config/app_config.yml"
use Rack::Session::Cookie, secret: '5fb7w345y3489f523y4h'

configure do
  enable :sessions
end

use OmniAuth::Builder do
  provider :google_oauth2, settings.google[:client_id], settings.google[:secret],
    {
      :scope => "userinfo.profile",
      :access_type => "offline",
      :prompt => "select_account consent",
      :name => "google"
    }
end

get '/list' do
  json get_list
end

get '/' do
  %Q|<a href='/auth/google'>Sign in with Google</a>|
end

get '/auth/:name/callback' do
  @auth = request.env['omniauth.auth']
  @auth.inspect
end

我的回调返回代码和状态。

最佳答案

/config/initializer/session_store.rb 中定义的域与 google api 控制台中定义的 origin/redirect_uri 不同时,rails 会出现此问题。

MyApp::Application.config.session_store :cookie_store, key: '_app_session', domain: 'my_app.com'

删除域参数或在两侧使用相同的域可以解决问题。

关于ruby - "CSRF detected"与 Omniauth 和谷歌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386149/

相关文章:

ruby-on-rails - 通过扩展方法有条件地检索 has_many 中的记录

oauth - 微服务 OAuth2/OpenID Connect 流程

android - 如何从我的 Android 应用程序访问新的 Gmail API?

ruby - 使用 Ruby Sinatra 创建单页代理

ruby - 当 Content-Type 为 application/json 时自动使用 JSON 编码 Rack 输出

ruby - 在字符串中查找并替换常用街道后缀

ruby - 仅当哈希键的值设置为 true 时才将哈希键转换为数组的最佳方法

ruby - 是否有用于 Ruby(如 JAXB)的 XML 绑定(bind)库?

node.js - 从 Azure AD 获取刷新 token 时出错

html - 无法在 ERB 文件中添加 CSS,Sinatra