2016-02-14 23 views
0

儘管在我的/config/initializers/omniauth.rb文件中添加了「hd:」標記,但不在域中的用戶仍然可以登錄。Omniauth 2.0 + Google「hd:」標記不起作用

Rails.application.config.middleware.use OmniAuth::Builder do 
provider :google_oauth2, Rails.application.secrets.secret_key_base, Rails.application.secrets.secret_token, 
    skip_jwt: true, 
    scope: 'profile, email', 
    image_aspect_ratio: 'square', 
    image_size: 48, 
    access_type: 'online', 
    hd: 'domain.com', 
    name: 'google' 
end 

使用具有域@ gmail.com的帳戶登錄時,用戶仍能夠登錄。

櫃面這是利用:

user.rb

class User < ActiveRecord::Base 
class << self 
    def from_omniauth(auth_hash) 
     user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider']) 
     user.name = auth_hash['info']['name'] 
     user.location = auth_hash['info']['location'] 
     user.image_url = auth_hash['info']['image'] 
     user.url = auth_hash['info']['urls']['user.provide.capitalize'] 
     user.email = auth_hash['info']['email'] 
     user.save! 
     user 
    end 
end 
end 

建議來解決這個問題,或者替代解決方案?

回答

0

hd標記僅在連接到Google的oAuth系統時設置請求URL參數。它可以很容易地由用戶更改,即使沒有更改,我發現它在塊不需要的域時效率低下。

解決這個問題的最好辦法是強制驗證您User模型像這樣:

validates :email, format: { with: /\b[A-Z0-9._%a-z\-][email protected]\.com\z/, 
      message: "must be a domain.com account" }