-
Notifications
You must be signed in to change notification settings - Fork 4
Ensure Full Test Coverage #107
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3eef32b
chore: add redis container setup in build_and_run_app.sh for local de…
tsviz 4c133df
chore: remove CSV export functionality from AppController and SalesDAO
tsviz 12d0891
Add 'Remember Me' checkbox to login form
tsviz c46ffa4
Add 'Remember Me' feature and corresponding tests
tsviz d891f15
Implement Multi-Factor Authentication (MFA) feature
tsviz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.codejava; | ||
|
||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
import java.util.Collection; | ||
|
||
public class MfaAuthentication extends UsernamePasswordAuthenticationToken { | ||
|
||
private boolean mfaAuthenticated; | ||
private User user; | ||
|
||
public MfaAuthentication(User user, Object credentials, | ||
Collection<? extends GrantedAuthority> authorities, | ||
boolean mfaAuthenticated) { | ||
super(user.getUsername(), credentials, authorities); | ||
this.user = user; | ||
this.mfaAuthenticated = mfaAuthenticated; | ||
} | ||
|
||
public boolean isMfaAuthenticated() { | ||
return mfaAuthenticated; | ||
} | ||
|
||
public void setMfaAuthenticated(boolean mfaAuthenticated) { | ||
this.mfaAuthenticated = mfaAuthenticated; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
@Override | ||
public boolean isAuthenticated() { | ||
// Only consider the authentication fully complete if MFA has been verified | ||
// for users who have MFA enabled | ||
if (user.isMfaEnabled()) { | ||
return super.isAuthenticated() && mfaAuthenticated; | ||
} | ||
return super.isAuthenticated(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package net.codejava; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class MfaAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private static final String MFA_VERIFICATION_URL = "/mfa/verify"; | ||
private static final String MFA_SETUP_URL = "/mfa/setup"; | ||
private static final String LOGIN_URL = "/login"; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
HttpSession session = request.getSession(); | ||
|
||
// Skip the filter for login and MFA-related URLs | ||
String requestURI = request.getRequestURI(); | ||
if (requestURI.equals(LOGIN_URL) || requestURI.startsWith(MFA_VERIFICATION_URL) || | ||
requestURI.startsWith(MFA_SETUP_URL) || requestURI.startsWith("/css/") || | ||
requestURI.startsWith("/js/")) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
// Check if the user is authenticated with MFA | ||
if (authentication instanceof MfaAuthentication) { | ||
MfaAuthentication mfaAuthentication = (MfaAuthentication) authentication; | ||
|
||
// If MFA is needed but not yet verified | ||
if (mfaAuthentication.getUser().isMfaEnabled() && !mfaAuthentication.isMfaAuthenticated()) { | ||
// Store the requested URL in the session | ||
session.setAttribute("REQUESTED_URL", requestURI); | ||
|
||
// Redirect to MFA verification page | ||
response.sendRedirect(MFA_VERIFICATION_URL); | ||
return; | ||
} | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Failure to use secure cookies Medium
Copilot Autofix
AI about 2 months ago
To fix the issue, the
secure
flag must be explicitly set on therememberMeCookie
before adding it to the response. This ensures that the cookie is only transmitted over secure HTTPS connections. The change involves calling thesetSecure(true)
method on therememberMeCookie
object before theresponse.addCookie(rememberMeCookie)
line.