All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.gov.ida.saml.hub.transformers.inbound.AuthenticationStatusUnmarshallerBase Maven / Gradle / Ivy

package uk.gov.ida.saml.hub.transformers.inbound;

import net.shibboleth.utilities.java.support.xml.SerializeSupport;
import org.opensaml.saml.saml2.core.Status;
import org.opensaml.saml.saml2.core.StatusMessage;
import uk.gov.ida.saml.core.domain.IdaStatus;
import uk.gov.ida.saml.hub.domain.AuthenticationStatusFactory;

import java.util.Optional;

import static java.text.MessageFormat.format;

public abstract class AuthenticationStatusUnmarshallerBase {

    private final SamlStatusToAuthenticationStatusCodeMapper statusMapper;
    private final AuthenticationStatusFactory statusFactory;

    public AuthenticationStatusUnmarshallerBase(
            final SamlStatusToAuthenticationStatusCodeMapper statusMapper,
            final AuthenticationStatusFactory authenticationStatusFactory) {

        this.statusMapper = statusMapper;
        this.statusFactory = authenticationStatusFactory;
    }

    public U fromSaml(final Status samlStatus) {
        final T status = getStatus(samlStatus);
        final String message = getStatusMessage(samlStatus).orElse(null);
        return statusFactory.create(status, message);
    }

    private T getStatus(final Status samlStatus) {
        return statusMapper.map(samlStatus).orElseThrow(() -> new IllegalStateException(
                format("Could not map status to an IdaStatus: {0}", SerializeSupport.nodeToString(samlStatus.getDOM()))
        ));
    }

    private Optional getStatusMessage(final Status samlStatus) {
        final StatusMessage statusMessage = samlStatus.getStatusMessage();
        return statusMessage != null ? Optional.of(statusMessage.getMessage()) : Optional.empty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy