限制使用Google OAuth2.0登录到Ruby上的特定白名单域名 [英] Restrict Login with Google OAuth2.0 to Specific Whitelisted Domain Name on Ruby

查看:191
本文介绍了限制使用Google OAuth2.0登录到Ruby上的特定白名单域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我改变了我想要做的事情,我想我将使用MySQL表将谷歌的设计登录列入白名单。已更改的问题发布于此:使用Google OAuth2.0限制登录,并使用Ruby设计到特定的白名单表

I changed how I'd like to do this I think I will use a MySQL table to whitelist the devise logins using google. The changed question is posted here: Restrict Login with Google OAuth2.0 and Devise to Specific Whitelist Table using Ruby

好吧,所以我试图在rails网站上获得我的ruby的受限身份验证使用Devise和Omni-Auth2,只有google。一切都工作到目前为止,但我只希望来自某个领域的电子邮件被接受。无论如何,我是开放的。

Alright so I am trying to get restricted authentication for my ruby on rails website using Devise and Omni-Auth2 and only google. Everything is working so far, but I only want emails coming from a certain domain to be accepted. I am open to anyway to do this.

我已经做了一些谷歌搜索,但似乎有些PHP用户比本地文件要多一些,也许是因为在本地使用google API客户端?我不太确定,因为我对编码一般而言很新,令我惊讶,我已经做到了这一点。

I have done some googling but it seems some PHP users have a bit more local files than I do, maybe because of using the google API client locally? I'm not exactly sure, as I am quite new to coding in general and surprised I made it this far.

以下是一个示例: Google Oauth2.0 with Python:如何限制对特定域的访问?

此处:限制使用Google登录电子邮件OAuth2.0到特定域名

两个似乎都使用hd:domain或类似的东西,但似乎有一些问题,加上我我不知道我的应用程序将如何表现出来。

Both seem to use the "hd:domain" or something similar, but there seems to be issues with that plus I'm not sure how I would impliment it in my app.

现在,对于一些更多的信息,我只使用宝石设计和omniauth-google-oauth2( https://github.com/zquestz/omniauth-google-oauth2 )我觉得,这样做的方式,但仍然不完全确定。任何帮助将不胜感激,如果我可以发布任何更多的信息让我知道。

Now for some more info, I am only using the gem devise and omniauth-google-oauth2 (https://github.com/zquestz/omniauth-google-oauth2) I feel like theres a way to do it with that gem but still not entirely sure. Any help would be appreciated if I can post any more info let me know.

我的omniauth_callbacks_controller:

My omniauth_callbacks_controller:

class User::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end


推荐答案

我知道这个问题是老的,但回答只是以供参考。您需要更改config / initializer / omniauth.rb并将hd添加到提供程序。

I know this question is old but answering it just for reference. You need to change config/initializer/omniauth.rb and add "hd" to provider.

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV["google_client_id"], ENV["google_client_secret"],
           {
               hd: 'domain.com'
           }
end

这篇关于限制使用Google OAuth2.0登录到Ruby上的特定白名单域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆