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

uk.gov.ida.saml.metadata.transformers.KeyDescriptorFinder Maven / Gradle / Ivy

There is a newer version: 3.4.6-15872
Show newest version
package uk.gov.ida.saml.metadata.transformers;

import org.opensaml.saml.saml2.metadata.KeyDescriptor;
import org.opensaml.security.credential.UsageType;
import org.slf4j.event.Level;
import uk.gov.ida.saml.core.validation.SamlTransformationErrorException;
import uk.gov.ida.saml.core.validation.SamlValidationSpecificationFailure;
import uk.gov.ida.saml.core.errors.SamlTransformationErrorFactory;
import java.util.List;

public class KeyDescriptorFinder {

    public KeyDescriptor find(
            List keyDescriptors,
            UsageType usageType,
            String entityId) {

        return keyDescriptors.stream()
                .filter(keyDescriptor -> keyDescriptor.getUse().equals(usageType))
                .filter(keyDescriptor -> (keyDescriptor.getKeyInfo().getKeyNames().isEmpty() || entityId == null || keyDescriptor.getKeyInfo().getKeyNames().get(0).getValue().equals(entityId)))
                .findFirst()
                .orElseThrow(() -> throwError(usageType, entityId));
    }

    private SamlTransformationErrorException throwError(UsageType usageType, String entityId) {
        SamlValidationSpecificationFailure failure = SamlTransformationErrorFactory.missingKey(usageType.toString(), entityId);
        return new SamlTransformationErrorException(failure.getErrorMessage(), Level.ERROR);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy