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

@@ -7,6 +7,7 @@ import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
@@ -17,48 +18,77 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.gepafin.tendermanagement.entities.SamlResponseLogEntity;
import net.gepafin.tendermanagement.repositories.SamlResponseLogRepository;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.SamlResponseEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.repositories.SamlResponseRepository;
import net.gepafin.tendermanagement.repositories.UserRepository;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
@Component
public class SamlSuccessHandler implements AuthenticationSuccessHandler{
public class SamlSuccessHandler implements AuthenticationSuccessHandler {
private final Logger logger = LoggerFactory.getLogger(SamlSuccessHandler.class);
@Autowired
private SamlResponseLogRepository samlResponseLogRepository;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
try {
// Cast the authentication object to Saml2Authentication
Saml2Authentication samlAuth = (Saml2Authentication) authentication;
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) samlAuth.getPrincipal();
private final Logger logger = LoggerFactory.getLogger(SamlSuccessHandler.class);
// Extract the user attributes from the principal
Map<String, List<Object>> userAttributes = principal.getAttributes();
@Autowired
private SamlResponseRepository samlResponseLogRepository;
// Log the user attributes for debugging purposes
logger.info("SAML User Attributes: " + userAttributes);
@Autowired
private UserRepository userRepository;
// Save the authentication details in the database (Optional)
SamlResponseLogEntity samlResponseLogEntity = new SamlResponseLogEntity();
samlResponseLogEntity.setAuthenticationObject(authentication.toString());
@Value("fe.base.url")
private String feBaseUrl;
// Convert user attributes to JSON and save in DB
ObjectMapper objectMapper = new ObjectMapper();
String userAttributesJson = objectMapper.writeValueAsString(userAttributes);
samlResponseLogEntity.setAuthenticationObject(userAttributesJson);
samlResponseLogRepository.save(samlResponseLogEntity);
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
try {
Saml2Authentication samlAuth = (Saml2Authentication) authentication;
Saml2AuthenticatedPrincipal principal = (Saml2AuthenticatedPrincipal) samlAuth.getPrincipal();
// Successful login logic
logger.info("SAML login successful for user: " + principal.getName());
response.sendRedirect("http://gepafin-staging-fe.s3-website.eu-central-1.amazonaws.com/login");
} catch (Exception e) {
logger.error("Error processing SAML success handler", e);
}
}
Map<String, List<Object>> userAttributes = principal.getAttributes();
String token = Utils.generateSecureToken();
logger.info("SAML User Attributes: " + userAttributes);
SamlResponseEntity samlResponseLogEntity = new SamlResponseEntity();
samlResponseLogEntity.setAuthenticationObject(authentication.toString());
ObjectMapper objectMapper = new ObjectMapper();
String userAttributesJson = objectMapper.writeValueAsString(userAttributes);
samlResponseLogEntity.setAuthenticationObject(userAttributesJson);
samlResponseLogEntity.setToken(token);
samlResponseLogRepository.save(samlResponseLogEntity);
// Successful login logic
logger.info("SAML login successful for user: " + principal.getName());
String cf = userAttributes.get("CodiceFiscale").get(0).toString();
UserEntity userEntity = userRepository.findByCodiceFiscale(cf).orElse(null);
if (userEntity == null) {
response.sendRedirect(feBaseUrl + "/registration?temp_token=" + token);
} else {
response.sendRedirect(feBaseUrl + "/login?temp_token=" + token);
}
} catch (Exception e) {
logger.error("Error processing SAML success handler", e);
}
}
public void validateToken(String token, String codiceFiscale) {
SamlResponseEntity samlResponseLogEntity = samlResponseLogRepository.findByToken(token);
if (samlResponseLogEntity == null) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_TOKEN_MSG));
}
Map<String, List<Object>> userAttributes = Utils
.convertStringIntoMap(samlResponseLogEntity.getAuthenticationObject());
String cf = userAttributes.get("CodiceFiscale").get(0).toString();
if (codiceFiscale == null || Boolean.FALSE.equals(codiceFiscale.equals(cf))) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.INVALID_TOKEN_MSG));
}
samlResponseLogRepository.delete(samlResponseLogEntity);
}
}