Skip to content

Commit

Permalink
Add method that returns whether 2nd factor is not authenticated but s…
Browse files Browse the repository at this point in the history
…etup
  • Loading branch information
janko committed Nov 18, 2024
1 parent eabfa69 commit a9adcd6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion demo-site/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<% 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? %>
<% if rodauth.two_factor_partially_authenticated? %>
<li><a href="/multifactor-auth">Authenticate Using Additional Factor</a></li>
<% else %>
<li><a href="/multifactor-manage">Manage Multifactor Authentication</a></li>
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_partially_authenticated?
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 if two_factor_partially_authenticated?
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_partially_authenticated?
logged_in? && !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 a9adcd6

Please sign in to comment.