updated code
This commit is contained in:
@@ -2,13 +2,13 @@ package net.gepafin.tendermanagement.config;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -26,10 +26,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.HubEntity;
|
||||
import net.gepafin.tendermanagement.entities.SamlResponseEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.SamlResponseStatusEnum;
|
||||
import net.gepafin.tendermanagement.repositories.SamlResponseRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.service.HubService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
@@ -47,6 +50,9 @@ public class SamlSuccessHandler implements AuthenticationSuccessHandler {
|
||||
|
||||
@Value("${fe.base.url}")
|
||||
private String feBaseUrl;
|
||||
|
||||
@Autowired
|
||||
private HubService hubService;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
|
||||
@@ -59,16 +65,6 @@ public class SamlSuccessHandler implements AuthenticationSuccessHandler {
|
||||
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);
|
||||
|
||||
|
||||
// Extracting raw SAML response
|
||||
String samlResponse = samlAuth.getSaml2Response();
|
||||
@@ -89,8 +85,28 @@ public class SamlSuccessHandler implements AuthenticationSuccessHandler {
|
||||
logger.info("SAML Response ID: " + responseId);
|
||||
logger.info("InResponseTo: " + inResponseTo);
|
||||
logger.info("IssueInstant: " + issueInstant);
|
||||
|
||||
SamlResponseEntity samlResponseLogEntity = samlResponseLogRepository
|
||||
.findByInResponseToAndStatus(inResponseTo, SamlResponseStatusEnum.INITIATED.getValue())
|
||||
.orElseThrow(() -> new CustomValidationException(Status.BAD_REQUEST,
|
||||
Translator.toLocale(GepafinConstant.INVALID_REQUEST)));
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String userAttributesJson = objectMapper.writeValueAsString(userAttributes);
|
||||
|
||||
samlResponseLogEntity.setAuthenticationObject(userAttributesJson);
|
||||
samlResponseLogEntity.setToken(token);
|
||||
samlResponseLogEntity.setStatus(SamlResponseStatusEnum.SUCCESS.getValue());
|
||||
samlResponseLogEntity.setInResponseTo(inResponseTo);
|
||||
samlResponseLogEntity.setSamlId(responseId);
|
||||
samlResponseLogEntity.setIssueInstant(issueInstant);
|
||||
samlResponseLogRepository.save(samlResponseLogEntity);
|
||||
|
||||
HubEntity hub = hubService.getHubByUuid(samlResponseLogEntity.getHubUuid());
|
||||
|
||||
String redirectUrl = feBaseUrl;
|
||||
if (Boolean.FALSE.equals(StringUtils.isEmpty(hub.getDomainName()))) {
|
||||
redirectUrl = hub.getDomainName();
|
||||
}
|
||||
|
||||
logger.info("SAML login successful for user: " + principal.getName());
|
||||
String cf = userAttributes.get("CodiceFiscale").get(0).toString();
|
||||
|
||||
Reference in New Issue
Block a user