Skip to content
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

Unknown email sender config #225

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ def handle_resource_not_found
end

def call_after_session_save
return if @skip_after_session_save_callback

if Passwordless.config.after_session_save.arity == 2
if @skip_after_session_save_callback
if Passwordless.config.send_paranoid_email
Mailer.unknown_address(@session).deliver_now
end
elsif Passwordless.config.after_session_save.arity == 2
Passwordless.config.after_session_save.call(@session, request)
else
Passwordless.config.after_session_save.call(@session)
Expand Down
13 changes: 13 additions & 0 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,18 @@ def sign_in(session, token = nil, url_options = {})
subject: I18n.t("passwordless.mailer.sign_in.subject")
)
end

# sends an email when user attempts to login with unknown address
#
# @param session [Session] An instance of Passwordless::Session
def unknown_address(session)
email_field = session.authenticatable.class.passwordless_email_field
@email = session.authenticatable.send(email_field)

mail(
to: @email,
subject: I18n.t("passwordless.mailer.unknown_address.subject")
)
end
end
end
1 change: 1 addition & 0 deletions app/views/passwordless/mailer/unknown_address.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= t("passwordless.mailer.unknown_address.body", email: @email ) %>
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ en:

Alternatively you can use this link to sign in directly:
%{magic_link}
unknown_address:
subject: "Not Registered"
body: |-
We noticed a login attempt using your email, %{email}.

If you're seeing this email, that means that you don't currently have an
account associated with this email.
Maybe you have a different email that's associated with your account?

If this wasn't you, disregard this email.
1 change: 1 addition & 0 deletions lib/passwordless/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Configuration
)

option :paranoid, default: false
option :send_paranoid_email, default: false

def initialize
set_defaults!
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/passwordless/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ class << User
assert_equal "/users/sign_in/#{Session.last!.identifier}", path
end

test("POST /:passwordless_for/sign_in -> SUCCESS / not found,
paranoid enabled, send paranoid email") do
with_config(paranoid: true, send_paranoid_email: true) do
post("/users/sign_in", params: {passwordless: {email: "a@a"}})
end

assert_equal 1, ActionMailer::Base.deliveries.size
assert_nil Session.last.authenticatable

assert_equal 302, status

follow_redirect!
assert_equal "/users/sign_in/#{Session.last!.identifier}", path
end


test("POST /:passwordless_for/sign_in -> ERROR / not found and paranoid disabled") do
post("/users/sign_in", params: {passwordless: {email: "A@a"}})

Expand Down