Rails / Google SSO - auth弹出窗口导致:阻止具有原点的帧

时间:2015-08-17 16:05:36

标签: javascript ruby-on-rails google-api google-apps google-sso

我有一个使用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)使用不同的流程,其中没有显示弹出窗口,而是用户被重定向到另一个页面。不知道这是否是一个解决方案(和/或)推荐的方法来避免我的错误。

在这种情况下,我可以在哪里找到Google文档丛林中的文档?

更新

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

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'

1 个答案:

答案 0 :(得分:1)

当你尝试向自己的服务器发出请求时,我唯一能想到的就是:

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']
}