Skip to content

Commit 0ea3c89

Browse files
committed
Add method that returns whether 2FA is satisfied
1 parent eabfa69 commit 0ea3c89

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

demo-site/views/index.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
<% if rodauth.logged_in_via_remember_key? %>
1717
<li><a href="/confirm-password">Confirm Password</a></li>
1818
<% end %>
19-
<% if rodauth.uses_two_factor_authentication? && !rodauth.two_factor_authenticated? %>
20-
<li><a href="/multifactor-auth">Authenticate Using Additional Factor</a></li>
21-
<% else %>
19+
<% if rodauth.two_factor_authentication_satisfied? %>
2220
<li><a href="/multifactor-manage">Manage Multifactor Authentication</a></li>
21+
<% else %>
22+
<li><a href="/multifactor-auth">Authenticate Using Additional Factor</a></li>
2323
<% end %>
2424
</ul>
2525
<% else %>

lib/rodauth/features/two_factor_base.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,12 @@ def two_factor_modifications_require_password?
124124
end
125125

126126
def authenticated?
127-
# False if not authenticated via single factor
128-
return false unless super
129-
130-
# True if already authenticated via 2nd factor
131-
return true if two_factor_authenticated?
132-
133-
# True if authenticated via single factor and 2nd factor not setup
134-
!uses_two_factor_authentication?
127+
super && two_factor_authentication_satisfied?
135128
end
136129

137130
def require_authentication
138131
super
139-
140-
# Avoid database query if already authenticated via 2nd factor
141-
return if two_factor_authenticated?
142-
143-
require_two_factor_authenticated if uses_two_factor_authentication?
132+
require_two_factor_authenticated unless two_factor_authentication_satisfied?
144133
end
145134

146135
def require_two_factor_setup
@@ -188,6 +177,10 @@ def two_factor_password_match?(password)
188177
end
189178
end
190179

180+
def two_factor_authentication_satisfied?
181+
two_factor_authenticated? || !uses_two_factor_authentication?
182+
end
183+
191184
def two_factor_authenticated?
192185
authenticated_by && authenticated_by.length >= 2
193186
end

0 commit comments

Comments
 (0)