Converting existing users to use Google Auth with a secondary email

AI-generated summary

The discussion addresses how to migrate existing Discourse users to Google SSO while preserving their personal email addresses for notifications. ian937 seeks to switch authentication to association-specific Google accounts without requiring non-technical users to manually add emails, aiming to block personal Google logins. He proposes adding Google emails as secondary addresses via a Ruby script and configuring OAuth2 settings, including using “Allowed email domains” for restriction.

olivia validates the approach, noting that Discourse matches Google logins via secondary emails. She recommends using the google oauth2 hd setting instead of allowed email domains for domain restriction and advises keeping auth overrides email disabled to maintain personal emails for notifications. Regarding the provided Ruby script, she points out two errors: the confirmed_at attribute is invalid for UserEmail, and the descriptive comment requires a # prefix. She suggests simplifying the creation step to user.user_emails.create!(email: domain_email, primary: false) and emphasizes testing with a backup before disabling local logins.

ian937 confirms he has updated the script and will proceed with the migration after notifying users to verify their Google credentials.

We run a private Discourse site for our association with a little over 200 active users. Currently the vast majority are signed up with their personal email accounts rather than the Google account we create for them (because the Google accounts didn’t exist when we first setup Discourse, and a lot of people are more likely to check their personal email for notifications anyway).
Now I want to switch to using the Google accounts as the authentication source, but I want to try and avoid having to get users to manually change or add their google account emails themselves (because trying to get non-technical volunteers to do something like that always turns into a nightmare for me). I also want to avoid users with private google accounts using those for SSO, and force all users to SSO to our Google domain accounts.

Having done some Googling, I’ve come up with a possible plan to achieve this, but want to try and confirm that I’m on the right track. Here’s the plan:

  1. Add all the Google email addresses as secondary emails (I’ve already enabled Secondary email addresses in the settings)
  2. Setup an OAuth2 consent and credentials, as per the Discourse docs
  3. Enter the credentials in Admin > Login & Authentication > OAuth2, google oauth2 client id & google oauth2 client secret. Then enable the setting enable google oauth2 logins.
  4. Restrict authentication to our domain by setting Admin > Login & Authentication, Allowed email domains - is this necessary?

Also, Google’s AI suggested using this Ruby script to automate adding the email addresses:

require 'csv'

csv_path = "/shared/users_map.csv"
updated = 0
skipped = 0

CSV.foreach(csv_path) do |personal_email, domain_email|
personal_email = personal_email&.strip&.downcase
domain_email = domain_email&.strip&.downcase
next if personal_email.blank? || domain_email.blank?

user = User.find_by_email(personal_email)

if user.nil?
puts "NOT FOUND: #{personal_email}"
skipped += 1
next
end

if UserEmail.exists?(email: domain_email)
puts "ALREADY EXISTS: #{domain_email} (User: #{user.username})"
skipped += 1
next
end

Attach the domain email as a confirmed secondary email

UserEmail.create!(
user_id: user.id,
email: domain_email,
primary: false,
confirmed_at: Time.zone.now
)

puts "LINKED: #{user.username} -> #{domain_email}"
updated += 1
end

puts "\nDone! Updated: #{updated}, Skipped/Errors: #{skipped}"

Any feedback appreciated!

Your plan looks right. Discourse can match Google logins to existing accounts through secondary emails, so users can keep their personal emails for notifications.

For the domain restriction, use google oauth2 hd rather than allowed email domains. Also, leave auth overrides email disabled to keep their primary email addresses unchanged.

For the script, UserEmail doesn’t have a confirmed_at column, so remove that. The Attach the domain email... line also needs a # in front of it.

The create step can be:

user.user_emails.create!(email: domain_email, primary: false)

I’d take a backup and test with a couple of users first, especially since the script skips email verification. Check that Google login takes them to their existing accounts and that personal Google accounts are blocked. Once that’s working, you can disable local logins.

Thanks @olivia I’ve added the secondary emails with the change you suggested to the ruby script.
Now I need to notify everyone to make sure they know their google logins, then I’ll do the rest of the process once they’ve had a bit of time to sort that out.

Cheers :smiley: