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

Use create date over expiry on remember 2fa cookie #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def set_remember_two_factor_cookie(resource)

if expires_seconds && expires_seconds > 0
cookies.signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME] = {
value: "#{resource.class}-#{resource.public_send(Devise.second_factor_resource_id)}",
value: {
user_class: resource.class.to_s,
user_id: resource.public_send(Devise.second_factor_resource_id).to_s,
created_at: Time.current
},
expires: expires_seconds.seconds.from_now
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Warden::Manager.after_authentication do |user, auth, options|
if auth.env["action_dispatch.cookies"]
expected_cookie_value = "#{user.class}-#{user.public_send(Devise.second_factor_resource_id)}"
actual_cookie_value = auth.env["action_dispatch.cookies"].signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME]
bypass_by_cookie = actual_cookie_value == expected_cookie_value
cookie = auth.env["action_dispatch.cookies"].signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME]
bypass_by_cookie = cookie.present?
bypass_by_cookie &&= cookie["user_class"] == user.class.to_s
bypass_by_cookie &&= cookie["user_id"] == user.id.to_s
bypass_by_cookie &&= Time.current <= cookie["created_at"].to_datetime + user.class.remember_otp_session_for_seconds
end

if user.respond_to?(:need_two_factor_authentication?) && !bypass_by_cookie
Expand Down
13 changes: 13 additions & 0 deletions spec/features/two_factor_authenticatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@
expect(page).to have_content("Enter the code that was sent to you")
end

scenario "requires TFA code again after if expiry date changes" do
sms_sign_in

logout

User.remember_otp_session_for_seconds = 1.day
Timecop.travel(1.day.from_now)
login_as user
visit dashboard_path
expect(page).to have_content("You are signed in as Marissa")
expect(page).to have_content("Enter the code that was sent to you")
end

scenario 'TFA should be different for different users' do
sms_sign_in

Expand Down