created new api's for login with spid

This commit is contained in:
rajesh
2024-09-25 20:12:34 +05:30
parent cdbb9c0072
commit d5524b7cc9
24 changed files with 407 additions and 149 deletions

View File

@@ -9,20 +9,22 @@ import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.enums.UserStatusEnum;
import net.gepafin.tendermanagement.model.request.*;
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
import net.gepafin.tendermanagement.model.response.UserSamlResponse;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import net.gepafin.tendermanagement.model.util.JWTToken;
import net.gepafin.tendermanagement.repositories.UserRepository;
import net.gepafin.tendermanagement.service.impl.AuthenticationService;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import java.security.SecureRandom;
import java.util.Base64;
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
@@ -43,24 +45,32 @@ public class UserDao {
@Autowired
private RoleDao roleDao;
public UserResponseBean createUser(UserReq userReq) {
public JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq) {
log.info("Creating user with email: {}", userReq.getEmail());
if (userRepository.existsByEmailIgnoreCase(userReq.getEmail())) {
log.error("User creation failed: Email {} already exists", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
}
if (!userReq.getPassword().equals(userReq.getConfPassword())) {
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale())) && userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
log.error("User creation failed: CodiceFiscale {} already exists", userReq.getCodiceFiscale());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.CODICE_FISCALE_EXISTS));
}
if(tempToken == null && (StringUtils.isEmpty(userReq.getPassword()) || StringUtils.isEmpty(userReq.getConfPassword()))) {
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_REQUEST));
}
if (tempToken == null && !userReq.getPassword().equals(userReq.getConfPassword())) {
log.error("User creation failed: Passwords do not match for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
}
if (userReq.getPassword().length() < 8) {
if (tempToken == null && userReq.getPassword().length() < 8) {
log.error("User creation failed: Password length is less than 8 characters for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
}
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
userEntity = userRepository.save(userEntity);
log.info("User created with ID: {}", userEntity.getId());
return convertUserEntityToUserResponse(userEntity);
return authService.getJWTTokenBean(userEntity, Boolean.TRUE);
}
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
@@ -88,15 +98,18 @@ public class UserDao {
private UserEntity convertUserRequestToUserEntity(UserReq userReq) {
UserEntity userEntity = new UserEntity();
userEntity.setPassword(passwordEncoder.encode(userReq.getPassword()));
if(Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getPassword()))) {
userEntity.setPassword(passwordEncoder.encode(userReq.getPassword()));
}
userEntity.setEmail(userReq.getEmail());
userEntity.setFirstName(userReq.getFirstName());
userEntity.setStatus(UserStatusEnum.PENDING_VERIFICATION.getValue());
userEntity.setStatus(UserStatusEnum.ACTIVE.getValue());
userEntity.setLastName(userReq.getLastName());
userEntity.setOrganization(userReq.getOrganization());
userEntity.setAddress(userReq.getAddress());
userEntity.setPhoneNumber(userReq.getPhoneNumber());
userEntity.setRoleEntity(roleDao.validateRole(userReq.getRoleId()));
userEntity.setCodiceFiscale(userReq.getCodiceFiscale());
return userEntity;
}
@@ -150,14 +163,6 @@ public class UserDao {
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
}
public String generateSecureToken() {
SecureRandom secureRandom = new SecureRandom();
byte[] tokenBytes = new byte[24];
secureRandom.nextBytes(tokenBytes);
String token = Base64.getUrlEncoder().withoutPadding().encodeToString(tokenBytes);
log.debug("Generated secure token: {}", token);
return token;
}
public String initiatePasswordReset(InitiatePasswordResetReq resetReq) {
UserEntity user = userRepository.findByEmail(resetReq.getEmail());
@@ -165,7 +170,7 @@ public class UserDao {
log.info("Password reset attempt for non-existent user: {}", resetReq.getEmail());
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG));
}
String token = generateSecureToken();
String token = Utils.generateSecureToken();
user.setResetPasswordToken(token);
userRepository.save(user);
log.info("Password reset token generated for user: {}", resetReq.getEmail());
@@ -227,4 +232,12 @@ public class UserDao {
return convertUserEntityToUserResponse(userEntity);
}
public JWTToken validateExistingUserToken(String token) {
return authService.validateExistingUserToken(token);
}
public UserSamlResponse validateNewUserToken(String token) {
return authService.validateNewUserToken(token);
}
}