package at.asitplus.eidas.specific.modules.auth.idaustria.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import at.asitplus.eidas.specific.modules.auth.idaustria.IdAustriaAuthConstants; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.idp.controller.AbstractProcessEngineSignalController; import lombok.extern.slf4j.Slf4j; /** * Controller that receives the response from ID Austria system. * * @author tlenz * */ @Slf4j @Controller public class IdAustriaAuthSignalController extends AbstractProcessEngineSignalController { public static final String HTTP_PARAM_RELAYSTATE = "RelayState"; /** * Default constructor with logging. * */ public IdAustriaAuthSignalController() { super(); log.debug("Registering servlet " + getClass().getName() + " with mappings '" + IdAustriaAuthConstants.ENDPOINT_POST + "' and '" + IdAustriaAuthConstants.ENDPOINT_REDIRECT + "'."); } /** * HTTP end-point for incoming SAML2 Respone from ID Austrian System. * * @param req HTTP request * @param resp HTTP response * @throws IOException In case of a HTTP communication error * @throws EaafException In case of a state-validation problem */ @RequestMapping(value = { IdAustriaAuthConstants.ENDPOINT_POST, IdAustriaAuthConstants.ENDPOINT_REDIRECT }, method = { RequestMethod.POST, RequestMethod.GET }) public void performEidasAuthentication(HttpServletRequest req, HttpServletResponse resp) throws IOException, EaafException { signalProcessManagement(req, resp); } /** * Read the PendingRequestId from SAML2 RelayState parameter. */ @Override public String getPendingRequestId(HttpServletRequest request) { String relayState = StringEscapeUtils.escapeHtml4(request.getParameter(HTTP_PARAM_RELAYSTATE)); if (StringUtils.isNotEmpty(relayState)) { try { String pendingReqId = transactionStorage.get(relayState, String.class); if (StringUtils.isNotEmpty(pendingReqId)) { return pendingReqId; } else { log.info("SAML2 RelayState from request is unknown. Can NOT restore session ... "); } } catch (EaafException e) { log.error("Can NOT map SAML2 RelayState to pendingRequestId", e); } finally { transactionStorage.remove(relayState); } } else { log.info("No SAML2 relaystate. Can NOT restore session ... "); } return null; } }