Rails / Google SSO - auth弹出导致:阻塞了带有origin的帧

Rails / Google SSO - auth popup causing: Blocked a frame with origin

本文关键字:的帧 origin SSO Google auth Rails      更新时间:2023-09-26

我有一个使用Google SSO登录/注册用户的Rails应用程序。它在Safari中工作得很好,但在Chrome中我一直得到这个错误:

Blocked a frame with origin "[domain]" from accessing a frame with origin "https://accounts.google.com". Protocols, domains, and ports must match.

我以前用过这个"解决方案"来解决它:https://github.com/zquestz/omniauth-google-oauth2/issues/122#issuecomment-60510241

但我最近注意到,它突然停止工作的Safari与上述解决方案,而不是。

我当前的实现看起来像这样(咖啡脚本):

$(document).ready ->
  $.ajax
    url: 'https://apis.google.com/js/client:plus.js?onload=gpAsyncInit'
    dataType: 'script'
    cache: false
window.gpAsyncInit = ->
  $('.googleplus-login').click (e) ->
  e.preventDefault()
  gapi.auth.authorize {
    immediate: false
    response_type: 'code'
    cookie_policy: 'single_host_origin'
    client_id: '[id]'
    scope: 'email profile'
  }, (response) ->
    if response and !response.error
      # google authentication succeed, now post data to server and handle data  securely
      jQuery.ajax
        type: 'POST'
        url: '/auth/google_oauth2/callback'
        dataType: 'json'
        data: response
        success: (json) ->
          # response from server
          [this doesn't happen]
          return
    else
      # google authentication failed

我没有在Google的文档中看到这个错误的描述,所以我不太确定如何修复它。

我确定我有相同的协议https,所以它一定是别的东西。我猜是定义域。

我看到其他网站(如Stack-overflow)使用不同的流程,其中不显示弹出窗口,而是将用户重定向到另一个页面。不知道这是否可能是一个解决方案(和/或)推荐的方法来避免我的错误。

在这种情况下,我可以在谷歌的文档丛林中找到关于这个的文档?

这是我控制器代码的相关部分。

def google_authentication
  respond_to do |format|
    code = params[:code]
    unless code.blank?
      [..]
      load = { code: code, client_id: client_id, client_secret: client_secret, grant_type: grant_type, redirect_uri: redirect_uri }
      url = "https://www.googleapis.com/oauth2/v3/token/"
      response = HTTParty.post(url, :query => load, headers: { "content-type" => "application/x-www-form-urlencoded"})
      json = JSON.parse(response.body)
      unless json.nil?
        unless json["error"].present?
          [..]
          email = decoded_hash["email"]
          user = User.find_by_email(email)
          if user
            sign_in_existing_user(user)
            format.json { render :json => {:status => "Success", user_id: "# {user.id}"} }
          else
            # Create user
            [..]
            format.html { redirect_to current_user, notice: "Welcome #{current_user.name}!" }
            format.json { render :json => {:status => "Success", user_id: "#{current_user.id}"} }
          end
        else
          #format.html { redirect_to root_path, error: "Could not sign up / in" }
          format.json { head :no_content }
        end
      end
    end
  end
end

我已经根据@EugZol下面的答案更新了我的JS,到:

data: {access_token: response['access_token'], error: response['error'], expires_in: response['expires_in']}

和我目前得到以下错误:

Started POST "/auth/google_oauth2/callback" [..]
Processing by UsersController#google_authentication as JSON
Parameters: {"expires_in"=>"86400", "provider"=>"google_oauth2"}
Completed 406 Not Acceptable in 1ms (ActiveRecord: 0.0ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/users_controller.rb:237:in `google_authentication'

我唯一可以假设的是,当您试图向自己的服务器发出请求时:

data: response

…您的代码以间接的方式访问由GAPI创建的框架。例如,response对象引用了它的一个内部字段,jQuery试图序列化它。

解决方案是手动选择所需的字段:

data: {
  state: response['state'],
  code: response['code'],
  scope: response['scope'],
  client_id: response['client_id'],
  g_user_cookie_policy: response['g_user_cookie_policy']
}