Search in sources :

Example 1 with SAML11SubjectLocalityType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType in project keycloak by keycloak.

the class SAML11AssertionWriter method write.

/**
 * Write an {@code AuthnStatementType} to stream
 *
 * @param authnStatement
 * @param out
 *
 * @throws ProcessingException
 */
public void write(SAML11AuthenticationStatementType authnStatement) throws ProcessingException {
    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, SAML11Constants.AUTHENTICATION_STATEMENT, SAML11Constants.ASSERTION_11_NSURI);
    XMLGregorianCalendar authnInstant = authnStatement.getAuthenticationInstant();
    if (authnInstant != null) {
        StaxUtil.writeAttribute(writer, SAML11Constants.AUTHENTICATION_INSTANT, authnInstant.toString());
    }
    URI authMethod = authnStatement.getAuthenticationMethod();
    if (authMethod != null) {
        StaxUtil.writeAttribute(writer, SAML11Constants.AUTHENTICATION_METHOD, authMethod.toString());
    }
    SAML11SubjectType subject = authnStatement.getSubject();
    if (subject != null)
        write(subject);
    SAML11SubjectLocalityType locality = authnStatement.getSubjectLocality();
    if (locality != null)
        write(locality);
    List<SAML11AuthorityBindingType> authorities = authnStatement.getAuthorityBindingType();
    for (SAML11AuthorityBindingType authority : authorities) {
        write(authority);
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : SAML11AuthorityBindingType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorityBindingType) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) SAML11SubjectLocalityType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType) URI(java.net.URI)

Example 2 with SAML11SubjectLocalityType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType in project keycloak by keycloak.

the class SAML11ParserUtil method parseAuthenticationStatement.

/**
 * Parse the AuthnStatement inside the assertion
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AuthenticationStatementType parseAuthenticationStatement(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, SAML11Constants.AUTHENTICATION_STATEMENT);
    Attribute authMethod = startElement.getAttributeByName(new QName(SAML11Constants.AUTHENTICATION_METHOD));
    if (authMethod == null)
        throw logger.parserRequiredAttribute(SAML11Constants.AUTHENTICATION_METHOD);
    Attribute authInstant = startElement.getAttributeByName(new QName(SAML11Constants.AUTHENTICATION_INSTANT));
    if (authInstant == null)
        throw logger.parserRequiredAttribute(SAML11Constants.AUTHENTICATION_INSTANT);
    SAML11AuthenticationStatementType authStat = new SAML11AuthenticationStatementType(URI.create(StaxParserUtil.getAttributeValue(authMethod)), XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(authInstant)));
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent == null)
            break;
        if (xmlEvent instanceof EndElement) {
            xmlEvent = StaxParserUtil.getNextEvent(xmlEventReader);
            EndElement endElement = (EndElement) xmlEvent;
            String endElementTag = StaxParserUtil.getElementName(endElement);
            if (endElementTag.equals(SAML11Constants.AUTHENTICATION_STATEMENT))
                break;
            else
                throw logger.parserUnknownEndElement(endElementTag, xmlEvent.getLocation());
        }
        startElement = null;
        if (xmlEvent instanceof StartElement) {
            startElement = (StartElement) xmlEvent;
        } else {
            startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        }
        if (startElement == null)
            break;
        String tag = StaxParserUtil.getElementName(startElement);
        if (JBossSAMLConstants.SUBJECT.get().equalsIgnoreCase(tag)) {
            SAML11SubjectParser subjectParser = new SAML11SubjectParser();
            SAML11SubjectType subject = (SAML11SubjectType) subjectParser.parse(xmlEventReader);
            SAML11SubjectStatementType subStat = new SAML11SubjectStatementType();
            subStat.setSubject(subject);
            authStat.setSubject(subject);
        } else if (JBossSAMLConstants.SUBJECT_LOCALITY.get().equals(tag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            SAML11SubjectLocalityType subjectLocalityType = new SAML11SubjectLocalityType();
            Attribute address = startElement.getAttributeByName(new QName(SAML11Constants.IP_ADDRESS));
            if (address != null) {
                subjectLocalityType.setIpAddress(StaxParserUtil.getAttributeValue(address));
            }
            Attribute dns = startElement.getAttributeByName(new QName(SAML11Constants.DNS_ADDRESS));
            if (dns != null) {
                subjectLocalityType.setDnsAddress(StaxParserUtil.getAttributeValue(dns));
            }
            authStat.setSubjectLocality(subjectLocalityType);
            StaxParserUtil.validate(StaxParserUtil.getNextEndElement(xmlEventReader), JBossSAMLConstants.SUBJECT_LOCALITY.get());
        } else if (SAML11Constants.AUTHORITY_BINDING.equals(tag)) {
            Attribute authorityKindAttr = startElement.getAttributeByName(new QName(SAML11Constants.AUTHORITY_KIND));
            if (authorityKindAttr == null)
                throw logger.parserRequiredAttribute("AuthorityKind");
            Attribute locationAttr = startElement.getAttributeByName(new QName(SAML11Constants.LOCATION));
            if (locationAttr == null)
                throw logger.parserRequiredAttribute("Location");
            URI location = URI.create(StaxParserUtil.getAttributeValue(locationAttr));
            Attribute bindingAttr = startElement.getAttributeByName(new QName(SAML11Constants.BINDING));
            if (bindingAttr == null)
                throw logger.parserRequiredAttribute("Binding");
            URI binding = URI.create(StaxParserUtil.getAttributeValue(bindingAttr));
            QName authorityKind = QName.valueOf(StaxParserUtil.getAttributeValue(authorityKindAttr));
            SAML11AuthorityBindingType authorityBinding = new SAML11AuthorityBindingType(authorityKind, location, binding);
            authStat.add(authorityBinding);
        } else
            throw logger.parserUnknownTag("", startElement.getLocation());
    }
    return authStat;
}
Also used : SAML11AuthorityBindingType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorityBindingType) SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) SAML11SubjectParser(org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser) URI(java.net.URI) StartElement(javax.xml.stream.events.StartElement) SAML11AuthenticationStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11SubjectLocalityType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType) SAML11SubjectStatementType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)

Aggregations

URI (java.net.URI)2 SAML11AuthorityBindingType (org.keycloak.dom.saml.v1.assertion.SAML11AuthorityBindingType)2 SAML11SubjectLocalityType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType)2 SAML11SubjectType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectType)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 QName (javax.xml.namespace.QName)1 Attribute (javax.xml.stream.events.Attribute)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 SAML11AuthenticationStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType)1 SAML11SubjectStatementType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)1 SAML11SubjectParser (org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser)1