2014-05-22 06:19:40 +08:00
|
|
|
class Auth::GoogleOAuth2Authenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"google_oauth2"
|
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def enabled?
|
|
|
|
SiteSetting.enable_google_oauth2_logins
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_for_user(user)
|
|
|
|
info = GoogleUserInfo.find_by(user_id: user.id)
|
|
|
|
info&.email || info&.name || ""
|
|
|
|
end
|
|
|
|
|
2014-05-22 06:19:40 +08:00
|
|
|
def after_authenticate(auth_hash)
|
|
|
|
session_info = parse_hash(auth_hash)
|
|
|
|
google_hash = session_info[:google]
|
|
|
|
|
2017-03-08 05:00:51 +08:00
|
|
|
result = ::Auth::Result.new
|
2014-05-22 06:19:40 +08:00
|
|
|
result.email = session_info[:email]
|
|
|
|
result.email_valid = session_info[:email_valid]
|
|
|
|
result.name = session_info[:name]
|
|
|
|
|
|
|
|
result.extra_data = google_hash
|
|
|
|
|
2017-03-08 05:00:51 +08:00
|
|
|
user_info = ::GoogleUserInfo.find_by(google_user_id: google_hash[:google_user_id])
|
2014-05-22 06:19:40 +08:00
|
|
|
result.user = user_info.try(:user)
|
|
|
|
|
2016-11-07 09:48:00 +08:00
|
|
|
if !result.user && !result.email.blank? && result.email_valid
|
|
|
|
result.user = User.find_by_email(result.email)
|
|
|
|
if result.user
|
2017-12-21 02:03:32 +08:00
|
|
|
# we've matched an existing user to this login attempt...
|
|
|
|
if result.user.google_user_info && result.user.google_user_info.google_user_id != google_hash[:google_user_id]
|
|
|
|
# but the user has changed the google account used to log in...
|
|
|
|
if result.user.google_user_info.email != google_hash[:email]
|
|
|
|
# the user changed their email, go ahead and scrub the old record
|
|
|
|
result.user.google_user_info.destroy!
|
|
|
|
else
|
|
|
|
# same email address but different account? likely a takeover scenario
|
|
|
|
result.failed = true
|
|
|
|
result.failed_reason = I18n.t('errors.conflicting_google_user_id')
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
2017-07-28 09:20:09 +08:00
|
|
|
::GoogleUserInfo.create({ user_id: result.user.id }.merge(google_hash))
|
2016-11-07 09:48:00 +08:00
|
|
|
end
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
2017-07-28 09:20:09 +08:00
|
|
|
GoogleUserInfo.create({ user_id: user.id }.merge(data))
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_middleware(omniauth)
|
2018-02-23 07:19:36 +08:00
|
|
|
options = {
|
|
|
|
setup: lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
|
|
|
strategy.options[:client_id] = SiteSetting.google_oauth2_client_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.google_oauth2_client_secret
|
|
|
|
},
|
|
|
|
skip_jwt: true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (google_oauth2_prompt = SiteSetting.google_oauth2_prompt).present?
|
|
|
|
options[:prompt] = google_oauth2_prompt.gsub("|", " ")
|
|
|
|
end
|
|
|
|
|
|
|
|
google_oauth2_hd = SiteSetting.google_oauth2_hd
|
|
|
|
options[:hd] = google_oauth2_hd if google_oauth2_hd.present?
|
|
|
|
|
2016-02-05 05:48:16 +08:00
|
|
|
# jwt encoding is causing auth to fail in quite a few conditions
|
|
|
|
# skipping
|
2018-02-23 07:19:36 +08:00
|
|
|
omniauth.provider :google_oauth2, options
|
2014-05-22 06:19:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def parse_hash(hash)
|
|
|
|
extra = hash[:extra][:raw_info]
|
|
|
|
|
|
|
|
h = {}
|
|
|
|
|
|
|
|
h[:email] = hash[:info][:email]
|
|
|
|
h[:name] = hash[:info][:name]
|
2017-03-01 11:58:24 +08:00
|
|
|
h[:email_valid] = extra[:email_verified]
|
2014-05-22 06:19:40 +08:00
|
|
|
|
|
|
|
h[:google] = {
|
|
|
|
google_user_id: hash[:uid] || extra[:sub],
|
|
|
|
email: extra[:email],
|
|
|
|
first_name: extra[:given_name],
|
|
|
|
last_name: extra[:family_name],
|
|
|
|
gender: extra[:gender],
|
|
|
|
name: extra[:name],
|
|
|
|
link: extra[:hd],
|
|
|
|
profile_link: extra[:profile],
|
|
|
|
picture: extra[:picture]
|
|
|
|
}
|
|
|
|
|
|
|
|
h
|
|
|
|
end
|
2015-04-25 01:10:43 +08:00
|
|
|
end
|