Skip to content

Commit dc31e5c

Browse files
Gargronhiyuki2578
authored andcommitted
Fix SSO login not using existing account when e-mail is verified (mastodon#11862)
Fix mastodon#11472
1 parent 77460cc commit dc31e5c

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

app/models/concerns/omniauthable.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Omniauthable
44
extend ActiveSupport::Concern
55

66
TEMP_EMAIL_PREFIX = 'change@me'
7-
TEMP_EMAIL_REGEX = /\Achange@me/
7+
TEMP_EMAIL_REGEX = /\A#{TEMP_EMAIL_PREFIX}/.freeze
88

99
included do
1010
devise :omniauthable
@@ -28,8 +28,8 @@ def find_for_oauth(auth, signed_in_resource = nil)
2828
# to prevent the identity being locked with accidentally created accounts.
2929
# Note that this may leave zombie accounts (with no associated identity) which
3030
# can be cleaned up at a later date.
31-
user = signed_in_resource || identity.user
32-
user = create_for_oauth(auth) if user.nil?
31+
user = signed_in_resource || identity.user
32+
user ||= create_for_oauth(auth)
3333

3434
if identity.user.nil?
3535
identity.user = user
@@ -45,7 +45,18 @@ def create_for_oauth(auth)
4545
# exists, we assign a temporary email and ask the user to verify it on
4646
# the next step via Auth::SetupController.show
4747

48-
user = User.new(user_params_from_auth(auth))
48+
strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
49+
assume_verified = strategy&.security&.assume_email_is_verified
50+
email_is_verified = auth.info.verified || auth.info.verified_email || assume_verified
51+
email = auth.info.verified_email || auth.info.email
52+
email = nil unless email_is_verified
53+
54+
user = User.find_by(email: email) if email_is_verified
55+
56+
return user unless user.nil?
57+
58+
user = User.new(user_params_from_auth(email, auth))
59+
4960
user.account.avatar_remote_url = auth.info.image if auth.info.image =~ /\A#{URI.regexp(%w(http https))}\z/
5061
user.skip_confirmation!
5162
user.save!
@@ -54,22 +65,15 @@ def create_for_oauth(auth)
5465

5566
private
5667

57-
def user_params_from_auth(auth)
58-
strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
59-
assume_verified = strategy.try(:security).try(:assume_email_is_verified)
60-
email_is_verified = auth.info.verified || auth.info.verified_email || assume_verified
61-
email = auth.info.verified_email || auth.info.email
62-
email = email_is_verified && !User.exists?(email: auth.info.email) && email
63-
display_name = auth.info.full_name || [auth.info.first_name, auth.info.last_name].join(' ')
64-
68+
def user_params_from_auth(email, auth)
6569
{
6670
email: email || "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
6771
password: Devise.friendly_token[0, 20],
6872
agreement: true,
6973
external: true,
7074
account_attributes: {
7175
username: ensure_unique_username(auth.uid),
72-
display_name: display_name,
76+
display_name: auth.info.full_name || [auth.info.first_name, auth.info.last_name].join(' '),
7377
},
7478
}
7579
end

0 commit comments

Comments
 (0)