package com.ningdatech.kqapi.user.helper;

import com.ningdatech.kqapi.security.password.UsernamePasswordAuthToken;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * <p>
 * LoginHelper
 * </p>
 *
 * @author WendyYang
 * @since 2023/10/31
 **/
@Component
@AllArgsConstructor
public class LoginHelper {

    private final PasswordEncoder passwordEncoder;

    public void checkPassword(UserDetails userDetails, UsernamePasswordAuthToken authToken)
            throws AuthenticationException {
        String presentedPassword = authToken.getCredentials().toString();
        if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
            throw new BadCredentialsException("用户名或密码错误");
        }
    }

}
