omniauthable.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_omniauth(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_omniauth(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 ||= reattach_for_auth(auth)
  26. user ||= create_for_auth(auth)
  27. if identity.user.nil?
  28. identity.user = user
  29. identity.save!
  30. end
  31. user
  32. end
  33. private
  34. def reattach_for_auth(auth)
  35. # If allowed, check if a user exists with the provided email address,
  36. # and return it if they does not have an associated identity with the
  37. # current authentication provider.
  38. # This can be used to provide a choice of alternative auth providers
  39. # or provide smooth gradual transition between multiple auth providers,
  40. # but this is discouraged because any insecure provider will put *all*
  41. # local users at risk, regardless of which provider they registered with.
  42. return unless ENV['ALLOW_UNSAFE_AUTH_PROVIDER_REATTACH'] == 'true'
  43. email, email_is_verified = email_from_auth(auth)
  44. return unless email_is_verified
  45. user = User.find_by(email: email)
  46. return if user.nil? || Identity.exists?(provider: auth.provider, user_id: user.id)
  47. user
  48. end
  49. def create_for_auth(auth)
  50. # Create a user for the given auth params. If no email was provided,
  51. # we assign a temporary email and ask the user to verify it on
  52. # the next step via Auth::SetupController.show
  53. email, email_is_verified = email_from_auth(auth)
  54. user = User.new(user_params_from_auth(email, auth))
  55. user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image)
  56. user.skip_confirmation! if email_is_verified
  57. user.save!
  58. user
  59. end
  60. def email_from_auth(auth)
  61. strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
  62. assume_verified = strategy&.security&.assume_email_is_verified
  63. email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
  64. email = auth.info.verified_email || auth.info.email
  65. [email, email_is_verified]
  66. end
  67. def user_params_from_auth(email, auth)
  68. {
  69. email: email || "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
  70. agreement: true,
  71. external: true,
  72. account_attributes: {
  73. username: ensure_unique_username(ensure_valid_username(auth.uid)),
  74. display_name: auth.info.full_name || auth.info.name || [auth.info.first_name, auth.info.last_name].join(' '),
  75. },
  76. }
  77. end
  78. def ensure_unique_username(starting_username)
  79. username = starting_username
  80. i = 0
  81. while Account.exists?(username: username, domain: nil)
  82. i += 1
  83. username = "#{starting_username}_#{i}"
  84. end
  85. username
  86. end
  87. def ensure_valid_username(starting_username)
  88. starting_username = starting_username.split('@')[0]
  89. temp_username = starting_username.gsub(/[^a-z0-9_]+/i, '')
  90. validated_username = temp_username.truncate(30, omission: '')
  91. validated_username
  92. end
  93. end
  94. end