Module: Gitlab::Utils::Email

Extended by:
Email
Included in:
Email
Defined in:
lib/gitlab/utils/email.rb

Defined Under Namespace

Classes: Deform, Masker, Symmetrical

Constant Summary collapse

EMAIL_REGEXP =
%r{(?>[a-zA-Z0-9]+|[\-._!#$%&'*+\/=?^{|}~]+){1,255}@[\w\-.]{1,253}\.{1}[a-zA-Z]{2,63}}
EMAIL_REGEXP_WITH_CAPTURING_GROUP =
/(#{EMAIL_REGEXP})/

Instance Method Summary collapse

Instance Method Details

#normalize_email(email) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/utils/email.rb', line 29

def normalize_email(email)
  return email unless email.is_a?(String)
  return email unless Devise.email_regexp.match?(email.strip)

  portions = email.downcase.strip.split('@')
  mailbox = portions.shift
  domain = portions.join

  mailbox_root = mailbox.split('+')[0]

  # Gmail addresses strip the "." from their emails.
  # For example, [email protected] is the same as [email protected]
  mailbox_root = mailbox_root.tr('.', '') if domain == 'gmail.com'

  [mailbox_root, domain].join('@')
end

#obfuscate_emails_in_text(text) ⇒ Object

Runs email address obfuscation on the given text.



21
22
23
24
25
26
27
# File 'lib/gitlab/utils/email.rb', line 21

def obfuscate_emails_in_text(text)
  return text unless text.present?

  text.gsub(EMAIL_REGEXP_WITH_CAPTURING_GROUP) do |email|
    obfuscated_email(email, deform: true)
  end
end

#obfuscated_email(email, deform: false) ⇒ Object

Replaces most visible characters with * to obfuscate an email address deform adds a fix number of * to ensure the address cannot be guessed. Also obfuscates TLD with **



13
14
15
16
17
18
# File 'lib/gitlab/utils/email.rb', line 13

def obfuscated_email(email, deform: false)
  return email if email.empty?

  masker_class = deform ? Deform : Symmetrical
  masker_class.new(email).masked
end