You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. module Omniauthable
  3. extend ActiveSupport::Concern
  4. TEMP_EMAIL_PREFIX = 'change@me'
  5. TEMP_EMAIL_REGEX = /\A#{TEMP_EMAIL_PREFIX}/.freeze
  6. included do
  7. devise :omniauthable
  8. def omniauth_providers
  9. Devise.omniauth_configs.keys
  10. end
  11. def email_present?
  12. email && email !~ TEMP_EMAIL_REGEX
  13. end
  14. end
  15. class_methods do
  16. def find_for_oauth(auth, signed_in_resource = nil)
  17. # EOLE-SSO Patch
  18. auth.uid = (auth.uid[0][:uid] || auth.uid[0][:user]) if auth.uid.is_a? Hashie::Array
  19. identity = Identity.find_for_oauth(auth)
  20. # If a signed_in_resource is provided it always overrides the existing user
  21. # to prevent the identity being locked with accidentally created accounts.
  22. # Note that this may leave zombie accounts (with no associated identity) which
  23. # can be cleaned up at a later date.
  24. user = signed_in_resource || identity.user
  25. user ||= create_for_oauth(auth)
  26. if identity.user.nil?
  27. identity.user = user
  28. identity.save!
  29. end
  30. user
  31. end
  32. def create_for_oauth(auth)
  33. # Check if the user exists with provided email. If no email was provided,
  34. # we assign a temporary email and ask the user to verify it on
  35. # the next step via Auth::SetupController.show
  36. strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
  37. assume_verified = strategy&.security&.assume_email_is_verified
  38. email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
  39. email = auth.info.verified_email || auth.info.email
  40. user = User.find_by(email: email) if email_is_verified
  41. return user unless user.nil?
  42. user = User.new(user_params_from_auth(email, auth))
  43. begin
  44. user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image)
  45. rescue Mastodon::UnexpectedResponseError
  46. user.account.avatar_remote_url = nil
  47. end
  48. user.confirm! if email_is_verified
  49. user.save!
  50. user
  51. end
  52. private
  53. def user_params_from_auth(email, auth)
  54. {
  55. email: email || "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
  56. agreement: true,
  57. external: true,
  58. account_attributes: {
  59. username: ensure_unique_username(ensure_valid_username(auth.uid)),
  60. display_name: auth.info.full_name || auth.info.name || [auth.info.first_name, auth.info.last_name].join(' '),
  61. },
  62. }
  63. end
  64. def ensure_unique_username(starting_username)
  65. username = starting_username
  66. i = 0
  67. while Account.exists?(username: username, domain: nil)
  68. i += 1
  69. username = "#{starting_username}_#{i}"
  70. end
  71. username
  72. end
  73. def ensure_valid_username(starting_username)
  74. starting_username = starting_username.split('@')[0]
  75. temp_username = starting_username.gsub(/[^a-z0-9_]+/i, '')
  76. temp_username.truncate(30, omission: '')
  77. end
  78. end
  79. end