Search in sources :

Example 26 with ParsingException

use of org.keycloak.saml.common.exceptions.ParsingException in project keycloak by keycloak.

the class SAMLUIInfoParser method processSubElement.

@Override
protected void processSubElement(XMLEventReader xmlEventReader, UIInfoType target, SAMLMetadataQNames element, StartElement elementDetail) throws ParsingException {
    switch(element) {
        case DISPLAY_NAME:
            LocalizedNameType displayName = new LocalizedNameType(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_LANG));
            StaxParserUtil.advance(xmlEventReader);
            displayName.setValue(StaxParserUtil.getElementText(xmlEventReader));
            target.addDisplayName(displayName);
            break;
        case DESCRIPTION:
            LocalizedNameType description = new LocalizedNameType(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_LANG));
            StaxParserUtil.advance(xmlEventReader);
            description.setValue(StaxParserUtil.getElementText(xmlEventReader));
            target.addDescription(description);
            break;
        case KEYWORDS:
            KeywordsType keywords = new KeywordsType(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_LANG));
            target.addKeywords(keywords);
            break;
        case INFORMATION_URL:
            LocalizedURIType informationURL = new LocalizedURIType(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_LANG));
            StaxParserUtil.advance(xmlEventReader);
            informationURL.setValue(URI.create(StaxParserUtil.getElementText(xmlEventReader)));
            target.addInformationURL(informationURL);
            break;
        case PRIVACY_STATEMENT_URL:
            LocalizedURIType privacyStatementURL = new LocalizedURIType(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_LANG));
            StaxParserUtil.advance(xmlEventReader);
            privacyStatementURL.setValue(URI.create(StaxParserUtil.getElementText(xmlEventReader)));
            target.addPrivacyStatementURL(privacyStatementURL);
            break;
        case LOGO:
            LogoType logo = new LogoType(Integer.parseInt(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_HEIGHT)), Integer.parseInt(StaxParserUtil.getRequiredAttributeValue(elementDetail, ATTR_WIDTH)));
            String lang = StaxParserUtil.getAttributeValue(elementDetail, ATTR_LANG);
            if (lang != null)
                logo.setLang(lang);
            StaxParserUtil.advance(xmlEventReader);
            try {
                String logoValue = StaxParserUtil.getElementText(xmlEventReader).replaceAll("\\s+", "");
                logo.setValue(new URI(logoValue));
            } catch (URISyntaxException ex) {
                throw new ParsingException(ex);
            }
            target.addLogo(logo);
            break;
        default:
            throw LOGGER.parserUnknownTag(StaxParserUtil.getElementName(elementDetail), elementDetail.getLocation());
    }
}
Also used : LocalizedURIType(org.keycloak.dom.saml.v2.metadata.LocalizedURIType) LogoType(org.keycloak.dom.saml.v2.mdui.LogoType) KeywordsType(org.keycloak.dom.saml.v2.mdui.KeywordsType) LocalizedNameType(org.keycloak.dom.saml.v2.metadata.LocalizedNameType) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 27 with ParsingException

use of org.keycloak.saml.common.exceptions.ParsingException in project keycloak by keycloak.

the class SAMLAttributeValueParser method parseAnyTypeAsString.

public static String parseAnyTypeAsString(XMLEventReader xmlEventReader) throws ParsingException {
    try {
        XMLEvent event = xmlEventReader.peek();
        if (event.isStartElement()) {
            event = xmlEventReader.nextTag();
            StringWriter sw = new StringWriter();
            XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(sw);
            // QName tagName = event.asStartElement().getName();
            int tagLevel = 1;
            do {
                writer.add(event);
                event = (XMLEvent) xmlEventReader.next();
                if (event.isStartElement()) {
                    tagLevel++;
                }
                if (event.isEndElement()) {
                    tagLevel--;
                }
            } while (xmlEventReader.hasNext() && tagLevel > 0);
            writer.add(event);
            writer.flush();
            return sw.toString();
        } else {
            return StaxParserUtil.getElementText(xmlEventReader);
        }
    } catch (Exception e) {
        throw logger.parserError(e);
    }
}
Also used : StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEvent(javax.xml.stream.events.XMLEvent) ParsingException(org.keycloak.saml.common.exceptions.ParsingException)

Example 28 with ParsingException

use of org.keycloak.saml.common.exceptions.ParsingException in project keycloak by keycloak.

the class SamlMultiTenantResolver method resolve.

@Override
public SamlDeployment resolve(HttpFacade.Request request) {
    String realm = request.getQueryParamValue("realm");
    if (realm == null) {
        throw new IllegalStateException("Not able to resolve realm from the request path!");
    }
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + realm + "-keycloak-saml.xml");
    if (is == null) {
        throw new IllegalStateException("Not able to find the file /" + realm + "-keycloak-saml.xml");
    }
    ResourceLoader loader = new ResourceLoader() {

        @Override
        public InputStream getResourceAsStream(String path) {
            return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
        }
    };
    try {
        return new DeploymentBuilder().build(is, loader);
    } catch (ParsingException e) {
        throw new IllegalStateException("Cannot load SAML deployment", e);
    }
}
Also used : ResourceLoader(org.keycloak.adapters.saml.config.parsers.ResourceLoader) InputStream(java.io.InputStream) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) DeploymentBuilder(org.keycloak.adapters.saml.config.parsers.DeploymentBuilder)

Example 29 with ParsingException

use of org.keycloak.saml.common.exceptions.ParsingException in project keycloak by keycloak.

the class CreateAuthnRequestStepBuilder method createLoginRequestDocument.

protected Document createLoginRequestDocument() {
    if (this.forceLoginRequestDocument != null) {
        return this.forceLoginRequestDocument;
    }
    try {
        SAML2Request samlReq = new SAML2Request();
        AuthnRequestType loginReq = samlReq.createAuthnRequestType(UUID.randomUUID().toString(), assertionConsumerURL, this.authServerSamlUrl.toString(), issuer, requestBinding.getBindingUri());
        if (protocolBinding != null) {
            loginReq.setProtocolBinding(protocolBinding);
        }
        return SAML2Request.convert(loginReq);
    } catch (ConfigurationException | ParsingException | ProcessingException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) SAML2Request(org.keycloak.saml.processing.api.saml.v2.request.SAML2Request) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Example 30 with ParsingException

use of org.keycloak.saml.common.exceptions.ParsingException in project keycloak by keycloak.

the class ArtifactResolutionService method invoke.

/**
 * This is the method called when a message is received by the endpoint.
 * It gets the message, extracts the ArtifactResolve message from the SOAP, creates a SOAP message containing
 * an ArtifactResponse message with the configured SAML message, and returns it.
 * @param msg The SOAP message received by the endpoint, in Source format
 * @return A StreamSource containing the ArtifactResponse
 */
@Override
public Source invoke(Source msg) {
    byte[] response;
    try (StringWriter w = new StringWriter()) {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(msg, new StreamResult(w));
        String s = w.toString();
        Document doc = Soap.extractSoapMessage(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
        SAMLDocumentHolder samlDoc = SAML2Request.getSAML2ObjectFromDocument(doc);
        if (samlDoc.getSamlObject() instanceof ArtifactResolveType) {
            lastArtifactResolve = (ArtifactResolveType) samlDoc.getSamlObject();
        } else {
            lastArtifactResolve = null;
        }
        Document artifactResponse = SamlProtocolUtils.convert(artifactResponseType);
        response = Soap.createMessage().addToBody(artifactResponse).getBytes();
    } catch (ProcessingException | ConfigurationException | TransformerException | ParsingException | IOException e) {
        throw new RuntimeException(e);
    }
    return new StreamSource(new ByteArrayInputStream(response));
}
Also used : ArtifactResolveType(org.keycloak.dom.saml.v2.protocol.ArtifactResolveType) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) TransformerException(javax.xml.transform.TransformerException) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Aggregations

ParsingException (org.keycloak.saml.common.exceptions.ParsingException)31 ConfigurationException (org.keycloak.saml.common.exceptions.ConfigurationException)14 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)14 InputStream (java.io.InputStream)11 Document (org.w3c.dom.Document)10 IOException (java.io.IOException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 DeploymentBuilder (org.keycloak.adapters.saml.config.parsers.DeploymentBuilder)7 ResourceLoader (org.keycloak.adapters.saml.config.parsers.ResourceLoader)7 FileNotFoundException (java.io.FileNotFoundException)6 SamlDeployment (org.keycloak.adapters.saml.SamlDeployment)6 FileInputStream (java.io.FileInputStream)5 AuthnRequestType (org.keycloak.dom.saml.v2.protocol.AuthnRequestType)5 Test (org.junit.Test)4 DefaultSamlDeployment (org.keycloak.adapters.saml.DefaultSamlDeployment)4 SamlDeploymentContext (org.keycloak.adapters.saml.SamlDeploymentContext)4 SAML2Request (org.keycloak.saml.processing.api.saml.v2.request.SAML2Request)4 Element (org.w3c.dom.Element)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3