2013-08-23 02:20:43 -04:00
|
|
|
class Auth::FacebookAuthenticator < Auth::Authenticator
|
|
|
|
|
2018-08-09 11:29:02 -04:00
|
|
|
AVATAR_SIZE ||= 480
|
2016-09-28 00:38:41 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
def name
|
|
|
|
"facebook"
|
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
def enabled?
|
|
|
|
SiteSetting.enable_facebook_logins
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_for_user(user)
|
|
|
|
info = FacebookUserInfo.find_by(user_id: user.id)
|
|
|
|
info&.email || info&.username || ""
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_revoke?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke(user, skip_remote: false)
|
|
|
|
info = FacebookUserInfo.find_by(user_id: user.id)
|
|
|
|
raise Discourse::NotFound if info.nil?
|
|
|
|
|
|
|
|
if skip_remote
|
|
|
|
info.destroy!
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
response = Excon.delete(revoke_url(info.facebook_user_id))
|
|
|
|
|
|
|
|
if response.status == 200
|
|
|
|
info.destroy!
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_url(fb_user_id)
|
|
|
|
"https://graph.facebook.com/#{fb_user_id}/permissions?access_token=#{SiteSetting.facebook_app_id}|#{SiteSetting.facebook_app_secret}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_connect_existing_user?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token, existing_account: nil)
|
2013-08-23 02:20:43 -04:00
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
session_info = parse_auth_token(auth_token)
|
|
|
|
facebook_hash = session_info[:facebook]
|
|
|
|
|
|
|
|
result.email = email = session_info[:email]
|
2014-12-08 05:40:44 -05:00
|
|
|
result.email_valid = !email.blank?
|
2014-02-16 22:45:17 -05:00
|
|
|
result.name = facebook_hash[:name]
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2013-08-26 03:36:20 -04:00
|
|
|
result.extra_data = facebook_hash
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = FacebookUserInfo.find_by(facebook_user_id: facebook_hash[:facebook_user_id])
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
if existing_account && (user_info.nil? || existing_account.id != user_info.user_id)
|
|
|
|
user_info.destroy! if user_info
|
|
|
|
result.user = existing_account
|
|
|
|
user_info = FacebookUserInfo.create!({ user_id: result.user.id }.merge(facebook_hash))
|
|
|
|
else
|
|
|
|
result.user = user_info&.user
|
|
|
|
end
|
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
if !result.user && !email.blank? && result.user = User.find_by_email(email)
|
2018-07-23 11:51:57 -04:00
|
|
|
FacebookUserInfo.create!({ user_id: result.user.id }.merge(facebook_hash))
|
2016-09-19 01:10:02 -04:00
|
|
|
end
|
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
user_info.update_columns(facebook_hash) if user_info
|
2016-09-19 02:14:11 -04:00
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
retrieve_avatar(result.user, result.extra_data)
|
|
|
|
retrieve_profile(result.user, result.extra_data)
|
2016-09-19 02:14:11 -04:00
|
|
|
|
2014-03-19 13:31:17 -04:00
|
|
|
if email.blank?
|
|
|
|
UserHistory.create(
|
|
|
|
action: UserHistory.actions[:facebook_no_email],
|
|
|
|
details: "name: #{facebook_hash[:name]}, facebook_user_id: #{facebook_hash[:facebook_user_id]}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
2016-10-24 11:15:13 -04:00
|
|
|
extra_data = auth[:extra_data]
|
2018-07-23 11:51:57 -04:00
|
|
|
FacebookUserInfo.create!({ user_id: user.id }.merge(extra_data))
|
2016-09-19 01:10:02 -04:00
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
retrieve_avatar(user, extra_data)
|
|
|
|
retrieve_profile(user, extra_data)
|
2016-09-19 02:14:11 -04:00
|
|
|
|
2016-09-19 01:10:02 -04:00
|
|
|
true
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :facebook,
|
2017-07-27 21:20:09 -04:00
|
|
|
setup: lambda { |env|
|
|
|
|
strategy = env["omniauth.strategy"]
|
2013-08-25 21:04:16 -04:00
|
|
|
strategy.options[:client_id] = SiteSetting.facebook_app_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.facebook_app_secret
|
2016-10-10 19:56:07 -04:00
|
|
|
strategy.options[:info_fields] = 'gender,email,name,about,first_name,link,last_name,website,location'
|
2013-08-25 21:04:16 -04:00
|
|
|
},
|
2017-07-27 21:20:09 -04:00
|
|
|
scope: "email"
|
2013-08-25 21:04:16 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
protected
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def parse_auth_token(auth_token)
|
|
|
|
raw_info = auth_token["extra"]["raw_info"]
|
|
|
|
info = auth_token["info"]
|
|
|
|
|
|
|
|
email = auth_token["info"][:email]
|
|
|
|
|
|
|
|
website = (info["urls"] && info["urls"]["Website"]) || nil
|
|
|
|
|
|
|
|
{
|
|
|
|
facebook: {
|
|
|
|
facebook_user_id: auth_token["uid"],
|
|
|
|
link: raw_info["link"],
|
|
|
|
username: raw_info["username"],
|
|
|
|
first_name: raw_info["first_name"],
|
|
|
|
last_name: raw_info["last_name"],
|
2016-10-24 11:15:13 -04:00
|
|
|
email: email,
|
2018-06-07 01:28:18 -04:00
|
|
|
gender: raw_info["gender"],
|
|
|
|
name: raw_info["name"],
|
|
|
|
avatar_url: info["image"],
|
|
|
|
location: info["location"],
|
|
|
|
website: website,
|
|
|
|
about_me: info["description"]
|
|
|
|
},
|
|
|
|
email: email,
|
|
|
|
email_valid: true
|
|
|
|
}
|
|
|
|
end
|
2016-09-19 01:10:02 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def retrieve_avatar(user, data)
|
|
|
|
return unless user
|
|
|
|
return if user.user_avatar.try(:custom_upload_id).present?
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if (avatar_url = data[:avatar_url]).present?
|
|
|
|
url = "#{avatar_url}?height=#{AVATAR_SIZE}&width=#{AVATAR_SIZE}"
|
|
|
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
2016-10-24 11:15:13 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-09-19 02:14:11 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def retrieve_profile(user, data)
|
|
|
|
return unless user
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
bio = data[:about_me] || data[:about]
|
|
|
|
location = data[:location]
|
|
|
|
website = data[:website]
|
2013-08-25 21:04:16 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if bio || location || website
|
|
|
|
profile = user.user_profile
|
|
|
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
|
|
|
profile.location = location unless profile.location.present?
|
|
|
|
profile.website = website unless profile.website.present?
|
|
|
|
profile.save
|
2016-10-24 11:15:13 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-08-25 21:04:16 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|