2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-28 10:44:16 -05:00
|
|
|
class Auth::ManagedAuthenticator < Auth::Authenticator
|
|
|
|
def description_for_user(user)
|
|
|
|
info = UserAssociatedAccount.find_by(provider_name: name, user_id: user.id)&.info
|
|
|
|
return "" if info.nil?
|
|
|
|
info["email"] || info["nickname"] || info["name"] || ""
|
|
|
|
end
|
|
|
|
|
|
|
|
# These three methods are designed to be overriden by child classes
|
|
|
|
def match_by_email
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-03-07 06:31:04 -05:00
|
|
|
def primary_email_verified?(auth_token)
|
|
|
|
# Omniauth providers should only provide verified emails in the :info hash.
|
|
|
|
# This method allows additional checks to be added
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-11-28 10:44:16 -05:00
|
|
|
def can_revoke?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_connect_existing_user?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-03-14 07:33:30 -04:00
|
|
|
def always_update_user_email?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-11-28 10:44:16 -05:00
|
|
|
def revoke(user, skip_remote: false)
|
|
|
|
association = UserAssociatedAccount.find_by(provider_name: name, user_id: user.id)
|
|
|
|
raise Discourse::NotFound if association.nil?
|
|
|
|
association.destroy!
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_authenticate(auth_token, existing_account: nil)
|
|
|
|
# Try and find an association for this account
|
2018-12-10 10:10:06 -05:00
|
|
|
association = UserAssociatedAccount.find_or_initialize_by(provider_name: auth_token[:provider], provider_uid: auth_token[:uid])
|
2018-11-28 10:44:16 -05:00
|
|
|
|
|
|
|
# Reconnecting to existing account
|
2018-12-10 10:10:06 -05:00
|
|
|
if can_connect_existing_user? && existing_account && (association.user.nil? || existing_account.id != association.user_id)
|
|
|
|
association.user = existing_account
|
2018-11-28 10:44:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Matching an account by email
|
2019-03-07 06:31:04 -05:00
|
|
|
if primary_email_verified?(auth_token) &&
|
|
|
|
match_by_email &&
|
|
|
|
association.user.nil? &&
|
|
|
|
(user = User.find_by_email(auth_token.dig(:info, :email)))
|
|
|
|
|
2018-11-28 10:44:16 -05:00
|
|
|
UserAssociatedAccount.where(user: user, provider_name: auth_token[:provider]).destroy_all # Destroy existing associations for the new user
|
2018-12-10 10:10:06 -05:00
|
|
|
association.user = user
|
2018-11-28 10:44:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update all the metadata in the association:
|
2018-12-10 10:10:06 -05:00
|
|
|
association.info = auth_token[:info] || {}
|
|
|
|
association.credentials = auth_token[:credentials] || {}
|
|
|
|
association.extra = auth_token[:extra] || {}
|
2018-11-28 10:44:16 -05:00
|
|
|
|
2019-02-27 10:29:26 -05:00
|
|
|
association.last_used = Time.zone.now
|
|
|
|
|
2018-12-10 10:10:06 -05:00
|
|
|
# Save to the DB. Do this even if we don't have a user - it might be linked up later in after_create_account
|
|
|
|
association.save!
|
|
|
|
|
2019-03-14 07:33:30 -04:00
|
|
|
# Update the user's email address from the auth payload
|
|
|
|
if association.user &&
|
|
|
|
(always_update_user_email? || association.user.email.end_with?(".invalid")) &&
|
|
|
|
primary_email_verified?(auth_token) &&
|
|
|
|
(email = auth_token.dig(:info, :email)) &&
|
|
|
|
(email != association.user.email) &&
|
|
|
|
!User.find_by_email(email)
|
|
|
|
|
|
|
|
association.user.update!(email: email)
|
|
|
|
end
|
|
|
|
|
2018-12-10 10:10:06 -05:00
|
|
|
# Update avatar/profile
|
|
|
|
retrieve_avatar(association.user, association.info["image"])
|
|
|
|
retrieve_profile(association.user, association.info)
|
|
|
|
|
|
|
|
# Build the Auth::Result object
|
|
|
|
result = Auth::Result.new
|
|
|
|
info = auth_token[:info]
|
|
|
|
result.email = info[:email]
|
|
|
|
result.name = "#{info[:first_name]} #{info[:last_name]}"
|
|
|
|
result.username = info[:nickname]
|
2019-03-07 06:31:04 -05:00
|
|
|
result.email_valid = primary_email_verified?(auth_token) if result.email
|
2018-12-10 10:10:06 -05:00
|
|
|
result.extra_data = {
|
|
|
|
provider: auth_token[:provider],
|
|
|
|
uid: auth_token[:uid]
|
|
|
|
}
|
|
|
|
result.user = association.user
|
2018-11-28 10:44:16 -05:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
2018-12-10 10:10:06 -05:00
|
|
|
auth_token = auth[:extra_data]
|
|
|
|
association = UserAssociatedAccount.find_or_initialize_by(provider_name: auth_token[:provider], provider_uid: auth_token[:uid])
|
|
|
|
association.user = user
|
|
|
|
association.save!
|
|
|
|
|
|
|
|
retrieve_avatar(user, association.info["image"])
|
|
|
|
retrieve_profile(user, association.info)
|
2018-11-28 10:44:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_avatar(user, url)
|
|
|
|
return unless user && url
|
|
|
|
return if user.user_avatar.try(:custom_upload_id).present?
|
|
|
|
Jobs.enqueue(:download_avatar_from_url, url: url, user_id: user.id, override_gravatar: false)
|
|
|
|
end
|
2018-12-07 09:04:21 -05:00
|
|
|
|
|
|
|
def retrieve_profile(user, info)
|
|
|
|
return unless user
|
|
|
|
|
2018-12-10 10:10:06 -05:00
|
|
|
bio = info["description"]
|
|
|
|
location = info["location"]
|
2018-12-07 09:04:21 -05: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
|
|
|
|
end
|
|
|
|
end
|
2018-11-28 10:44:16 -05:00
|
|
|
end
|