-
Notifications
You must be signed in to change notification settings - Fork 166
Add EmailNormalizer (LG-10313) #8818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require 'resolv' | ||
|
|
||
| # Class to help normalize email addresses for services like Gmail that let users | ||
| # add extra things after a + | ||
| class EmailNormalizer | ||
| attr_reader :email | ||
|
|
||
| # @param [#to_s] email | ||
| def initialize(email) | ||
| @email = Mail::Address.new(email.to_s) | ||
| end | ||
|
|
||
| # @return [String] | ||
| def normalized_email | ||
| if gmail? | ||
| before_plus, _after_plus = email.local.split('+', 2) | ||
| [before_plus.tr('.', ''), email.domain].join('@') | ||
| else | ||
| email.to_s | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def gmail? | ||
| email.domain == 'gmail.com' || google_mx_record? | ||
| end | ||
|
|
||
| def google_mx_record? | ||
| return false if ENV['RAILS_OFFLINE'] | ||
|
|
||
| mx_records(email.domain).any? { |domain| domain.end_with?('google.com') } | ||
| end | ||
|
|
||
| def mx_records(domain) | ||
| Resolv::DNS.open do |dns| | ||
| dns.getresources(domain, Resolv::DNS::Resource::IN::MX). | ||
| map { |r| r.exchange.to_s } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, what does the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
quick lil demo: I ended up doing a slightly more realistic stub in 56a77c4
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went for the most realistic stub in 4a8ff43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I was looking at the Ruby docs and it didn't specify what https://ruby-doc.org/stdlib-3.1.1/libdoc/resolv/rdoc/Resolv/DNS/Resource/MX.html |
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe EmailNormalizer do | ||
| subject(:normalizer) { EmailNormalizer.new(email) } | ||
|
|
||
| describe '#normalized_email' do | ||
| subject(:normalized_email) { normalizer.normalized_email } | ||
|
|
||
| context 'with a non-gmail domain' do | ||
| let(:email) { 'foobar+123@example.com' } | ||
|
|
||
| it 'is the same email' do | ||
| expect(normalized_email).to eq(email) | ||
| end | ||
| end | ||
|
|
||
| context 'with a gmail domain' do | ||
| let(:email) { 'foo.bar+123@gmail.com' } | ||
|
|
||
| it 'removes . and anything after the +' do | ||
| expect(normalized_email).to eq('foobar@gmail.com') | ||
| end | ||
| end | ||
|
|
||
| context 'with a Google Apps domain' do | ||
| let(:email) { 'foo.bar.baz+123@example.com' } | ||
|
|
||
| before do | ||
| dns = instance_double( | ||
| 'Resolv::DNS', | ||
| getresources: [ | ||
| Resolv::DNS::Resource::IN::MX.new(1, Resolv::DNS::Name.new(%w[abcd l google com])), | ||
| ], | ||
| ) | ||
|
|
||
| allow(Resolv::DNS).to receive(:open).and_yield(dns) | ||
| end | ||
|
|
||
| it 'still removes . and anything after the +' do | ||
| expect(normalized_email).to eq('foobarbaz@example.com') | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mimicking the behavior we have for the
email_validatorgem so that CI and other test environments don't have to talk to the real internetidentity-idp/app/validators/form_email_validator.rb
Lines 10 to 14 in de5fa81