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

fix(auth): Correct security authentication logic #19

Merged
merged 1 commit into from
Oct 27, 2023
Merged
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
@@ -1,7 +1,6 @@
package fptu.swp391.shoppingcart.user.authentication.service;

import fptu.swp391.shoppingcart.user.authentication.model.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -14,11 +13,14 @@

@Service
public class AuthenticationProviderService implements AuthenticationProvider {
@Autowired
private JpaUserDetailsService userDetailsService;
private final JpaUserDetailsService userDetailsService;

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
private final BCryptPasswordEncoder bCryptPasswordEncoder;

public AuthenticationProviderService(JpaUserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Expand All @@ -27,9 +29,9 @@ public Authentication authenticate(Authentication authentication) throws Authent

CustomUserDetails userDetails = userDetailsService.loadUserByUsername(username);

if (!userDetails.getUser().isEnabled() && (userDetails.getUser().getDisabledUntil().isAfter(LocalDateTime.now()))){
throw new BadCredentialsException("Account is disabled, please try again later until "
+ userDetails.getUser().getDisabledUntil());
if (!userDetails.getUser().isEnabled() || (userDetails.getUser().getDisabledUntil().isAfter(LocalDateTime.now()))) {
throw new BadCredentialsException("Account is disabled, please try again later until "
+ userDetails.getUser().getDisabledUntil());
}
if (bCryptPasswordEncoder.matches(password, userDetails.getPassword())) {
return new UsernamePasswordAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
Expand Down