2013-08-23 02:20:43 -04:00
|
|
|
class Auth::TwitterAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"twitter"
|
|
|
|
end
|
|
|
|
|
2018-07-23 11:51:57 -04:00
|
|
|
def enabled?
|
|
|
|
SiteSetting.enable_twitter_logins
|
|
|
|
end
|
|
|
|
|
|
|
|
def description_for_user(user)
|
|
|
|
info = TwitterUserInfo.find_by(user_id: user.id)
|
|
|
|
info&.email || info&.screen_name || ""
|
|
|
|
end
|
|
|
|
|
2018-07-27 07:28:51 -04:00
|
|
|
def can_revoke?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke(user, skip_remote: false)
|
|
|
|
info = TwitterUserInfo.find_by(user_id: user.id)
|
|
|
|
raise Discourse::NotFound if info.nil?
|
|
|
|
|
|
|
|
# We get a token from twitter upon login but do not need it, and do not store it.
|
|
|
|
# Therefore we do not have any way to revoke the token automatically on twitter's end
|
|
|
|
|
|
|
|
info.destroy!
|
|
|
|
true
|
|
|
|
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
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
|
|
|
|
2016-10-12 18:30:39 -04:00
|
|
|
result.email = data["email"]
|
|
|
|
result.email_valid = result.email.present?
|
|
|
|
result.username = data["nickname"]
|
2016-09-06 12:18:13 -04:00
|
|
|
result.name = data["name"]
|
2013-08-23 02:20:43 -04:00
|
|
|
twitter_user_id = auth_token["uid"]
|
|
|
|
|
|
|
|
result.extra_data = {
|
2017-12-13 23:35:27 -05:00
|
|
|
twitter_email: result.email,
|
2013-08-23 02:20:43 -04:00
|
|
|
twitter_user_id: twitter_user_id,
|
2016-10-17 09:43:40 -04:00
|
|
|
twitter_screen_name: result.username,
|
|
|
|
twitter_image: data["image"],
|
|
|
|
twitter_description: data["description"],
|
|
|
|
twitter_location: data["location"]
|
2013-08-23 02:20:43 -04:00
|
|
|
}
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = TwitterUserInfo.find_by(twitter_user_id: twitter_user_id)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2018-07-27 07:28:51 -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 = TwitterUserInfo.create!(
|
|
|
|
user_id: result.user.id,
|
|
|
|
screen_name: result.username,
|
|
|
|
twitter_user_id: twitter_user_id,
|
|
|
|
email: result.email
|
|
|
|
)
|
|
|
|
else
|
|
|
|
result.user = user_info&.user
|
|
|
|
end
|
|
|
|
|
2017-12-13 23:35:27 -05:00
|
|
|
if (!result.user) && result.email_valid && (result.user = User.find_by_email(result.email))
|
2018-07-27 07:28:51 -04:00
|
|
|
TwitterUserInfo.create!(
|
2016-09-06 12:18:13 -04:00
|
|
|
user_id: result.user.id,
|
2016-10-12 18:30:39 -04:00
|
|
|
screen_name: result.username,
|
2017-12-13 23:35:27 -05:00
|
|
|
twitter_user_id: twitter_user_id,
|
|
|
|
email: result.email
|
2016-09-06 12:18:13 -04:00
|
|
|
)
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2016-10-17 09:43:40 -04:00
|
|
|
retrieve_avatar(result.user, result.extra_data)
|
|
|
|
retrieve_profile(result.user, result.extra_data)
|
2016-10-12 18:30:39 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
2016-10-17 09:43:40 -04:00
|
|
|
extra_data = auth[:extra_data]
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
TwitterUserInfo.create(
|
|
|
|
user_id: user.id,
|
2016-10-17 09:43:40 -04:00
|
|
|
screen_name: extra_data[:twitter_screen_name],
|
2017-12-13 23:35:27 -05:00
|
|
|
twitter_user_id: extra_data[:twitter_user_id],
|
|
|
|
email: extra_data[:email]
|
2013-08-23 02:20:43 -04:00
|
|
|
)
|
2016-10-17 09:43:40 -04:00
|
|
|
|
|
|
|
retrieve_avatar(user, extra_data)
|
|
|
|
retrieve_profile(user, extra_data)
|
2016-10-24 11:15:13 -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 :twitter,
|
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[:consumer_key] = SiteSetting.twitter_consumer_key
|
|
|
|
strategy.options[:consumer_secret] = SiteSetting.twitter_consumer_secret
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-24 11:15:13 -04:00
|
|
|
protected
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
if (avatar_url = data[:twitter_image]).present?
|
|
|
|
url = avatar_url.sub("_normal", "")
|
|
|
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
2016-10-17 09:43:40 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def retrieve_profile(user, data)
|
|
|
|
return unless user
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
bio = data[:twitter_description]
|
|
|
|
location = data[:twitter_location]
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if bio || location
|
|
|
|
profile = user.user_profile
|
|
|
|
profile.bio_raw = bio unless profile.bio_raw.present?
|
|
|
|
profile.location = location unless profile.location.present?
|
|
|
|
profile.save
|
2016-10-17 09:43:40 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-10-17 09:43:40 -04:00
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|