2016-09-22 14:31:10 -04:00
|
|
|
require_dependency 'has_errors'
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
class Auth::GithubAuthenticator < Auth::Authenticator
|
|
|
|
|
|
|
|
def name
|
|
|
|
"github"
|
|
|
|
end
|
|
|
|
|
2016-09-22 14:31:10 -04:00
|
|
|
class GithubEmailChecker
|
|
|
|
include ::HasErrors
|
|
|
|
|
|
|
|
def initialize(validator, email)
|
|
|
|
@validator = validator
|
|
|
|
@email = Email.downcase(email)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?()
|
|
|
|
@validator.validate_each(self, :email, @email)
|
|
|
|
return errors.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
def after_authenticate(auth_token)
|
|
|
|
result = Auth::Result.new
|
|
|
|
|
|
|
|
data = auth_token[:info]
|
2016-09-22 14:31:10 -04:00
|
|
|
result.username = screen_name = data[:nickname]
|
|
|
|
result.name = data[:name]
|
2013-08-23 02:20:43 -04:00
|
|
|
|
2016-09-22 14:31:10 -04:00
|
|
|
github_user_id = auth_token[:uid]
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
result.extra_data = {
|
|
|
|
github_user_id: github_user_id,
|
|
|
|
github_screen_name: screen_name,
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_info = GithubUserInfo.find_by(github_user_id: github_user_id)
|
2013-08-23 02:20:43 -04:00
|
|
|
|
|
|
|
if user_info
|
2016-09-22 14:31:10 -04:00
|
|
|
# If there's existing user info with the given GitHub ID, that's all we
|
|
|
|
# need to know.
|
2013-08-23 02:20:43 -04:00
|
|
|
user = user_info.user
|
2016-09-22 14:31:10 -04:00
|
|
|
result.email = data[:email],
|
2018-01-11 23:17:29 -05:00
|
|
|
result.email_valid = data[:email].present?
|
2016-09-22 14:31:10 -04:00
|
|
|
else
|
|
|
|
# Potentially use *any* of the emails from GitHub to find a match or
|
|
|
|
# register a new user, with preference given to the primary email.
|
|
|
|
all_emails = Array.new(auth_token[:extra][:all_emails])
|
|
|
|
|
|
|
|
# Only consider verified emails to match an existing user. We don't want
|
|
|
|
# someone to be able to create a GitHub account with an unverified email
|
|
|
|
# in order to access someone else's Discourse account!
|
|
|
|
all_emails.each do |candidate|
|
|
|
|
if !!candidate[:verified] && (user = User.find_by_email(candidate[:email]))
|
|
|
|
result.email = candidate[:email]
|
|
|
|
result.email_valid = !!candidate[:verified]
|
|
|
|
GithubUserInfo.create(
|
|
|
|
user_id: user.id,
|
|
|
|
screen_name: screen_name,
|
|
|
|
github_user_id: github_user_id
|
|
|
|
)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we *still* don't have a user, check to see if there's an email that
|
|
|
|
# passes validation (this includes whitelist/blacklist filtering if any is
|
|
|
|
# configured). When no whitelist/blacklist is in play, this will simply
|
|
|
|
# choose the primary email since it's at the front of the list.
|
|
|
|
if !user
|
|
|
|
validator = EmailValidator.new(attributes: :email)
|
|
|
|
found_email = false
|
|
|
|
all_emails.each do |candidate|
|
|
|
|
checker = GithubEmailChecker.new(validator, candidate[:email])
|
|
|
|
if checker.valid?
|
|
|
|
result.email = candidate[:email]
|
|
|
|
result.email_valid = !!candidate[:verified]
|
|
|
|
found_email = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !found_email
|
|
|
|
result.failed = true
|
|
|
|
escaped = Rack::Utils.escape_html(screen_name)
|
|
|
|
result.failed_reason = I18n.t("login.authenticator_error_no_valid_email", account: escaped)
|
|
|
|
end
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
|
|
|
|
2017-08-21 19:14:26 -04:00
|
|
|
retrieve_avatar(user, data)
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
result.user = user
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_account(user, auth)
|
|
|
|
data = auth[:extra_data]
|
|
|
|
GithubUserInfo.create(
|
|
|
|
user_id: user.id,
|
|
|
|
screen_name: data[:github_screen_name],
|
|
|
|
github_user_id: data[:github_user_id]
|
|
|
|
)
|
2017-08-21 19:14:26 -04:00
|
|
|
|
|
|
|
retrieve_avatar(user, data)
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
2013-08-25 21:04:16 -04:00
|
|
|
|
|
|
|
def register_middleware(omniauth)
|
|
|
|
omniauth.provider :github,
|
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.github_client_id
|
|
|
|
strategy.options[:client_secret] = SiteSetting.github_client_secret
|
|
|
|
},
|
2017-07-27 21:20:09 -04:00
|
|
|
scope: "user:email"
|
2013-08-25 21:04:16 -04:00
|
|
|
end
|
2017-08-21 19:14:26 -04:00
|
|
|
|
2017-08-22 23:46:50 -04:00
|
|
|
private
|
2017-08-21 19:14:26 -04:00
|
|
|
|
|
|
|
def retrieve_avatar(user, data)
|
2017-08-22 23:46:50 -04:00
|
|
|
return unless data[:image].present? && user && user.user_avatar&.custom_upload_id.blank?
|
2017-08-21 19:14:26 -04:00
|
|
|
|
2017-08-22 23:46:50 -04:00
|
|
|
Jobs.enqueue(:download_avatar_from_url, url: data[:image], user_id: user.id, override_gravatar: false)
|
2017-08-21 19:14:26 -04:00
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|