2018-02-04 04:42:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-01 11:00:41 +00:00
|
|
|
module User::Omniauthable
|
2018-02-04 04:42:13 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
TEMP_EMAIL_PREFIX = 'change@me'
|
2023-04-26 23:46:18 +00:00
|
|
|
TEMP_EMAIL_REGEX = /\A#{TEMP_EMAIL_PREFIX}/
|
2018-02-04 04:42:13 +00:00
|
|
|
|
|
|
|
included do
|
2019-03-14 01:13:42 +00:00
|
|
|
devise :omniauthable
|
|
|
|
|
2018-02-04 04:42:13 +00:00
|
|
|
def omniauth_providers
|
|
|
|
Devise.omniauth_configs.keys
|
|
|
|
end
|
|
|
|
|
2022-03-09 11:07:35 +00:00
|
|
|
def email_present?
|
2018-02-04 04:42:13 +00:00
|
|
|
email && email !~ TEMP_EMAIL_REGEX
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2024-02-14 14:16:07 +00:00
|
|
|
def find_for_omniauth(auth, signed_in_resource = nil)
|
2018-02-04 04:42:13 +00:00
|
|
|
# EOLE-SSO Patch
|
|
|
|
auth.uid = (auth.uid[0][:uid] || auth.uid[0][:user]) if auth.uid.is_a? Hashie::Array
|
2024-02-14 14:16:07 +00:00
|
|
|
identity = Identity.find_for_omniauth(auth)
|
2018-02-04 04:42:13 +00:00
|
|
|
|
|
|
|
# If a signed_in_resource is provided it always overrides the existing user
|
|
|
|
# to prevent the identity being locked with accidentally created accounts.
|
|
|
|
# Note that this may leave zombie accounts (with no associated identity) which
|
|
|
|
# can be cleaned up at a later date.
|
2019-09-16 18:42:29 +00:00
|
|
|
user = signed_in_resource || identity.user
|
2024-02-14 14:16:07 +00:00
|
|
|
user ||= reattach_for_auth(auth)
|
|
|
|
user ||= create_for_auth(auth)
|
2018-02-04 04:42:13 +00:00
|
|
|
|
|
|
|
if identity.user.nil?
|
|
|
|
identity.user = user
|
|
|
|
identity.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2024-02-14 14:16:07 +00:00
|
|
|
private
|
2018-02-04 04:42:13 +00:00
|
|
|
|
2024-02-14 14:16:07 +00:00
|
|
|
def reattach_for_auth(auth)
|
|
|
|
# If allowed, check if a user exists with the provided email address,
|
|
|
|
# and return it if they does not have an associated identity with the
|
|
|
|
# current authentication provider.
|
|
|
|
|
|
|
|
# This can be used to provide a choice of alternative auth providers
|
|
|
|
# or provide smooth gradual transition between multiple auth providers,
|
|
|
|
# but this is discouraged because any insecure provider will put *all*
|
|
|
|
# local users at risk, regardless of which provider they registered with.
|
|
|
|
|
|
|
|
return unless ENV['ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH'] == 'true'
|
2019-09-16 18:42:29 +00:00
|
|
|
|
2024-02-14 14:16:07 +00:00
|
|
|
email, email_is_verified = email_from_auth(auth)
|
|
|
|
return unless email_is_verified
|
2019-09-16 18:42:29 +00:00
|
|
|
|
2024-02-14 14:16:07 +00:00
|
|
|
user = User.find_by(email: email)
|
|
|
|
return if user.nil? || Identity.exists?(provider: auth.provider, user_id: user.id)
|
|
|
|
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_for_auth(auth)
|
|
|
|
# Create a user for the given auth params. If no email was provided,
|
|
|
|
# we assign a temporary email and ask the user to verify it on
|
|
|
|
# the next step via Auth::SetupController.show
|
|
|
|
|
|
|
|
email, email_is_verified = email_from_auth(auth)
|
2019-09-16 18:42:29 +00:00
|
|
|
|
|
|
|
user = User.new(user_params_from_auth(email, auth))
|
|
|
|
|
2023-01-13 15:40:06 +00:00
|
|
|
begin
|
2023-02-18 11:37:47 +00:00
|
|
|
user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image)
|
2023-01-13 15:40:06 +00:00
|
|
|
rescue Mastodon::UnexpectedResponseError
|
|
|
|
user.account.avatar_remote_url = nil
|
|
|
|
end
|
|
|
|
|
2024-01-15 18:04:58 +00:00
|
|
|
user.mark_email_as_confirmed! if email_is_verified
|
2018-02-04 04:42:13 +00:00
|
|
|
user.save!
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2024-02-14 14:16:07 +00:00
|
|
|
def email_from_auth(auth)
|
|
|
|
strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
|
|
|
|
assume_verified = strategy&.security&.assume_email_is_verified
|
|
|
|
email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
|
|
|
|
email = auth.info.verified_email || auth.info.email
|
|
|
|
|
|
|
|
[email, email_is_verified]
|
|
|
|
end
|
2018-02-04 04:42:13 +00:00
|
|
|
|
2019-09-16 18:42:29 +00:00
|
|
|
def user_params_from_auth(email, auth)
|
2018-02-04 04:42:13 +00:00
|
|
|
{
|
2018-10-08 02:50:11 +00:00
|
|
|
email: email || "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
|
2019-01-25 11:36:54 +00:00
|
|
|
agreement: true,
|
2019-04-25 00:49:25 +00:00
|
|
|
external: true,
|
2018-02-04 04:42:13 +00:00
|
|
|
account_attributes: {
|
2022-03-09 11:07:35 +00:00
|
|
|
username: ensure_unique_username(ensure_valid_username(auth.uid)),
|
|
|
|
display_name: auth.info.full_name || auth.info.name || [auth.info.first_name, auth.info.last_name].join(' '),
|
2018-02-04 04:42:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_unique_username(starting_username)
|
|
|
|
username = starting_username
|
|
|
|
i = 0
|
|
|
|
|
2020-04-30 12:39:05 +00:00
|
|
|
while Account.exists?(username: username, domain: nil)
|
2018-02-04 04:42:13 +00:00
|
|
|
i += 1
|
|
|
|
username = "#{starting_username}_#{i}"
|
|
|
|
end
|
|
|
|
|
|
|
|
username
|
|
|
|
end
|
2022-03-09 11:07:35 +00:00
|
|
|
|
|
|
|
def ensure_valid_username(starting_username)
|
|
|
|
starting_username = starting_username.split('@')[0]
|
|
|
|
temp_username = starting_username.gsub(/[^a-z0-9_]+/i, '')
|
2023-02-08 06:06:50 +00:00
|
|
|
temp_username.truncate(30, omission: '')
|
2022-03-09 11:07:35 +00:00
|
|
|
end
|
2018-02-04 04:42:13 +00:00
|
|
|
end
|
|
|
|
end
|