Skip to content

Commit

Permalink
Add method that returns whether 2FA is satisfied
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Nov 17, 2024
1 parent eabfa69 commit 0ea3c89
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
6 changes: 3 additions & 3 deletions demo-site/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<% if rodauth.logged_in_via_remember_key? %>
<li><a href="/confirm-password">Confirm Password</a></li>
<% end %>
<% if rodauth.uses_two_factor_authentication? && !rodauth.two_factor_authenticated? %>
<li><a href="/multifactor-auth">Authenticate Using Additional Factor</a></li>
<% else %>
<% if rodauth.two_factor_authentication_satisfied? %>
<li><a href="/multifactor-manage">Manage Multifactor Authentication</a></li>
<% else %>
<li><a href="/multifactor-auth">Authenticate Using Additional Factor</a></li>
<% end %>
</ul>
<% else %>
Expand Down
19 changes: 6 additions & 13 deletions lib/rodauth/features/two_factor_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,12 @@ def two_factor_modifications_require_password?
end

def authenticated?
# False if not authenticated via single factor
return false unless super

# True if already authenticated via 2nd factor
return true if two_factor_authenticated?

# True if authenticated via single factor and 2nd factor not setup
!uses_two_factor_authentication?
super && two_factor_authentication_satisfied?
end

def require_authentication
super

# Avoid database query if already authenticated via 2nd factor
return if two_factor_authenticated?

require_two_factor_authenticated if uses_two_factor_authentication?
require_two_factor_authenticated unless two_factor_authentication_satisfied?
end

def require_two_factor_setup
Expand Down Expand Up @@ -188,6 +177,10 @@ def two_factor_password_match?(password)
end
end

def two_factor_authentication_satisfied?
two_factor_authenticated? || !uses_two_factor_authentication?
end

def two_factor_authenticated?
authenticated_by && authenticated_by.length >= 2
end
Expand Down

0 comments on commit 0ea3c89

Please sign in to comment.