org.opensaml.xml.XMLObjectBuilderFactory Java Examples

The following examples show how to use org.opensaml.xml.XMLObjectBuilderFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CasHTTPSOAP11Encoder.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
@Override
protected Envelope buildSOAPMessage(final SAMLObject samlMessage) {
    final XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

    final SOAPObjectBuilder<Envelope> envBuilder =
            (SOAPObjectBuilder<Envelope>) builderFactory.getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
    final Envelope envelope = envBuilder.buildObject(
            SOAPConstants.SOAP11_NS, Envelope.DEFAULT_ELEMENT_LOCAL_NAME, OPENSAML_11_SOAP_NS_PREFIX);

    final SOAPObjectBuilder<Body> bodyBuilder =
            (SOAPObjectBuilder<Body>) builderFactory.getBuilder(Body.DEFAULT_ELEMENT_NAME);
    final Body body = bodyBuilder.buildObject(
            SOAPConstants.SOAP11_NS, Body.DEFAULT_ELEMENT_LOCAL_NAME, OPENSAML_11_SOAP_NS_PREFIX);

    body.getUnknownXMLObjects().add(samlMessage);
    envelope.setBody(body);

    return envelope;
}
 
Example #2
Source File: KeyInfoHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds an {@link RSAKeyValue} XMLObject from the Java security RSA public key type.
 * 
 * @param rsaPubKey a native Java {@link RSAPublicKey}
 * @return an {@link RSAKeyValue} XMLObject
 */
public static RSAKeyValue buildRSAKeyValue(RSAPublicKey rsaPubKey) {
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    RSAKeyValue rsaKeyValue = (RSAKeyValue) builderFactory
        .getBuilder(RSAKeyValue.DEFAULT_ELEMENT_NAME)
        .buildObject(RSAKeyValue.DEFAULT_ELEMENT_NAME);
    Modulus modulus = (Modulus) builderFactory
        .getBuilder(Modulus.DEFAULT_ELEMENT_NAME)
        .buildObject(Modulus.DEFAULT_ELEMENT_NAME);
    Exponent exponent = (Exponent) builderFactory
        .getBuilder(Exponent.DEFAULT_ELEMENT_NAME)
        .buildObject(Exponent.DEFAULT_ELEMENT_NAME);
    
    modulus.setValueBigInt(rsaPubKey.getModulus());
    rsaKeyValue.setModulus(modulus);
    
    exponent.setValueBigInt(rsaPubKey.getPublicExponent());
    rsaKeyValue.setExponent(exponent);
    
    return rsaKeyValue;
}
 
Example #3
Source File: KeyInfoHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a {@link DSAKeyValue} XMLObject from the Java security DSA public key type.
 * 
 * @param dsaPubKey a native Java {@link DSAPublicKey}
 * @return an {@link DSAKeyValue} XMLObject
 */
public static DSAKeyValue buildDSAKeyValue(DSAPublicKey dsaPubKey) {
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    DSAKeyValue dsaKeyValue = (DSAKeyValue) builderFactory
        .getBuilder(DSAKeyValue.DEFAULT_ELEMENT_NAME)
        .buildObject(DSAKeyValue.DEFAULT_ELEMENT_NAME);
    Y y = (Y) builderFactory.getBuilder(Y.DEFAULT_ELEMENT_NAME).buildObject(Y.DEFAULT_ELEMENT_NAME);
    G g = (G) builderFactory.getBuilder(G.DEFAULT_ELEMENT_NAME).buildObject(G.DEFAULT_ELEMENT_NAME);
    P p = (P) builderFactory.getBuilder(P.DEFAULT_ELEMENT_NAME).buildObject(P.DEFAULT_ELEMENT_NAME);
    Q q = (Q) builderFactory.getBuilder(Q.DEFAULT_ELEMENT_NAME).buildObject(Q.DEFAULT_ELEMENT_NAME);
    
    y.setValueBigInt(dsaPubKey.getY());
    dsaKeyValue.setY(y);
    
    g.setValueBigInt(dsaPubKey.getParams().getG());
    dsaKeyValue.setG(g);
    
    p.setValueBigInt(dsaPubKey.getParams().getP());
    dsaKeyValue.setP(p);
    
    q.setValueBigInt(dsaPubKey.getParams().getQ());
    dsaKeyValue.setQ(q);
    
    return dsaKeyValue;
}
 
Example #4
Source File: HTTPSOAP11Encoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds the SOAP message to be encoded.
 * 
 * @param samlMessage body of the SOAP message
 * 
 * @return the SOAP message
 */
@SuppressWarnings("unchecked")
protected Envelope buildSOAPMessage(SAMLObject samlMessage) {
    log.debug("Building SOAP message");
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

    SOAPObjectBuilder<Envelope> envBuilder = (SOAPObjectBuilder<Envelope>) builderFactory
            .getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
    Envelope envelope = envBuilder.buildObject();

    log.debug("Adding SAML message to the SOAP message's body");
    SOAPObjectBuilder<Body> bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory
            .getBuilder(Body.DEFAULT_ELEMENT_NAME);
    Body body = bodyBuilder.buildObject();
    body.getUnknownXMLObjects().add(samlMessage);
    envelope.setBody(body);

    return envelope;
}
 
Example #5
Source File: HTTPSOAP11Encoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds the SOAP message to be encoded.
 * 
 * @param samlMessage body of the SOAP message
 * 
 * @return the SOAP message
 */
@SuppressWarnings("unchecked")
protected Envelope buildSOAPMessage(SAMLObject samlMessage) {
    if (log.isDebugEnabled()) {
        log.debug("Building SOAP message");
    }
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

    SOAPObjectBuilder<Envelope> envBuilder = (SOAPObjectBuilder<Envelope>) builderFactory
            .getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
    Envelope envelope = envBuilder.buildObject();

    if (log.isDebugEnabled()) {
        log.debug("Adding SAML message to the SOAP message's body");
    }
    SOAPObjectBuilder<Body> bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory
            .getBuilder(Body.DEFAULT_ELEMENT_NAME);
    Body body = bodyBuilder.buildObject();
    body.getUnknownXMLObjects().add(samlMessage);
    envelope.setBody(body);

    return envelope;
}
 
Example #6
Source File: SOAP11Encoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** Constructor. */
@SuppressWarnings("unchecked")
public SOAP11Encoder() {
    super();
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    envBuilder = (SOAPObjectBuilder<Envelope>) builderFactory.getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
    bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory.getBuilder(Body.DEFAULT_ELEMENT_NAME);
}
 
Example #7
Source File: Encrypter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 */
public Encrypter() {
    UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
    encryptedDataUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedData.DEFAULT_ELEMENT_NAME);
    encryptedKeyUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedKey.DEFAULT_ELEMENT_NAME);

    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    keyInfoBuilder = (XMLSignatureBuilder<KeyInfo>) builderFactory.getBuilder(KeyInfo.DEFAULT_ELEMENT_NAME);

    jcaProviderName = null;
}
 
Example #8
Source File: HandlerChainAwareHTTPSOAP11Encoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** Constructor. */
public HandlerChainAwareHTTPSOAP11Encoder() {
    super();
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    envBuilder = (SOAPObjectBuilder<Envelope>) builderFactory.getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
    bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory.getBuilder(Body.DEFAULT_ELEMENT_NAME);
}
 
Example #9
Source File: ArtifactBindingHelper.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param artifactResolutionRequest
 * @return
 */
protected Envelope generateSOAPEnvelope(ArtifactResolve artifactResolutionRequest) {
    XMLObjectBuilderFactory xmlObjectBuilderFactory = Configuration.getBuilderFactory();
    Envelope envelope = (Envelope) xmlObjectBuilderFactory.getBuilder(Envelope.DEFAULT_ELEMENT_NAME).buildObject(Envelope.DEFAULT_ELEMENT_NAME);
    Body body = (Body) xmlObjectBuilderFactory.getBuilder(Body.DEFAULT_ELEMENT_NAME).buildObject(Body.DEFAULT_ELEMENT_NAME);

    body.getUnknownXMLObjects().add(artifactResolutionRequest);
    envelope.setBody(body);

    return envelope;
}
 
Example #10
Source File: SOAPHelper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Build a SOAP 1.1. Fault element.
 * 
 * @param faultCode the 'faultcode' QName (required)
 * @param faultString the 'faultstring' value (required)
 * @param faultActor the 'faultactor' value (may be null)
 * @param detailChildren the 'detail' child elements
 * @param detailAttributes the 'detail' element attributes
 * @return the new Fault element object
 */
public static Fault buildSOAP11Fault(QName faultCode, String faultString, String faultActor,
        List<XMLObject> detailChildren, Map<QName, String> detailAttributes) {
    if (faultCode == null) {
        throw new IllegalArgumentException("Argument for 'faultcode' may not be null");
    }
    if (faultString == null) {
        throw new IllegalArgumentException("Argument for 'faultstring' may not be null");
    }
    
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory(); 
    
    Fault faultObj =  (Fault) builderFactory.getBuilder(Fault.DEFAULT_ELEMENT_NAME)
        .buildObject(Fault.DEFAULT_ELEMENT_NAME);
    FaultCode faultCodeObj =  (FaultCode) builderFactory.getBuilder(FaultCode.DEFAULT_ELEMENT_NAME)
        .buildObject(FaultCode.DEFAULT_ELEMENT_NAME);
    FaultString faultStringObj =  (FaultString) builderFactory.getBuilder(FaultString.DEFAULT_ELEMENT_NAME)
        .buildObject(FaultString.DEFAULT_ELEMENT_NAME);
    
    faultCodeObj.setValue(faultCode);
    faultObj.setCode(faultCodeObj);
    
    faultStringObj.setValue(faultString);
    faultObj.setMessage(faultStringObj);
    
    if (faultActor != null) {
        FaultActor faultActorObj =  (FaultActor) builderFactory.getBuilder(FaultActor.DEFAULT_ELEMENT_NAME)
            .buildObject(FaultActor.DEFAULT_ELEMENT_NAME);
        faultActorObj.setValue(faultActor);
        faultObj.setActor(faultActorObj);
    }
        
    Detail detailObj = null;
    if (detailChildren != null && !detailChildren.isEmpty()) {
        detailObj = (Detail) builderFactory.getBuilder(Detail.DEFAULT_ELEMENT_NAME)
            .buildObject(Detail.DEFAULT_ELEMENT_NAME);
        for (XMLObject xo : detailChildren) {
            if (xo != null) {
                detailObj.getUnknownXMLObjects().add(xo);
            }
        }
    }
    if (detailAttributes != null && !detailAttributes.isEmpty()) {
        if (detailObj == null) {
            detailObj = (Detail) builderFactory.getBuilder(Detail.DEFAULT_ELEMENT_NAME)
                .buildObject(Detail.DEFAULT_ELEMENT_NAME);
        }
        for (Entry<QName,String> entry : detailAttributes.entrySet()) {
            if (entry.getKey() != null && entry.getValue() != null) {
                detailObj.getUnknownAttributes().put(entry.getKey(), entry.getValue());
            }
        }
    }
    if (detailObj != null && 
            (!detailObj.getUnknownXMLObjects().isEmpty() || !detailObj.getUnknownAttributes().isEmpty())) {
        faultObj.setDetail(detailObj);
    }
    
    return faultObj;
}
 
Example #11
Source File: SAML2TokenBuilder.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
@Override
public void createStatement(GenericIdentityProviderData ipData, RahasData rahasData)
        throws IdentityProviderException {
    if (log.isDebugEnabled()) {
        log.debug("Begin SAML statement creation.");
    }
    attributeStmt = (AttributeStatement) buildXMLObject(AttributeStatement.DEFAULT_ELEMENT_NAME);

    Map<String, RequestedClaimData> mapClaims = ipData.getRequestedClaims();

    if (rahasData.getAppliesToAddress() != null) {
        appilesTo = rahasData.getAppliesToAddress();
    }

    Iterator<RequestedClaimData> ite = mapClaims.values().iterator();

    while (ite.hasNext()) {
        RequestedClaimData claim = ite.next();
        String uri = claim.getUri();

        int index = uri.lastIndexOf("/");
        String attrName = uri.substring(index + 1, uri.length());
        String attrNamespace = uri.substring(0, index);

        Attribute attribute = (Attribute) buildXMLObject(Attribute.DEFAULT_ELEMENT_NAME);
        attribute.setName(attrName);
        attribute.setNameFormat(attrNamespace);

        XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

        // TODO remove this else if condition after WSO2 IS supports claim
        // types properly
        if (claim.getUri().equals(IdentityConstants.CLAIM_PPID)) {
            XSBase64BinaryBuilder ppidValueBuilder = (XSBase64BinaryBuilder) builderFactory
                    .getBuilder(XSBase64Binary.TYPE_NAME);
            XSBase64Binary ppidValue = ppidValueBuilder.buildObject(
                    AttributeValue.DEFAULT_ELEMENT_NAME, XSBase64Binary.TYPE_NAME);
            ppidValue.setValue(claim.getValue());
            attribute.getAttributeValues().add(ppidValue);
        } else {
            XSStringBuilder attributeValueBuilder = (XSStringBuilder) builderFactory
                    .getBuilder(XSString.TYPE_NAME);

            XSString stringValue = attributeValueBuilder.buildObject(
                    AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
            stringValue.setValue(claim.getValue());
            attribute.getAttributeValues().add(stringValue);
        }
        attributeStmt.getAttributes().add(attribute);
    }
}
 
Example #12
Source File: SAML1TokenBuilder.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
@Override
public void createStatement(GenericIdentityProviderData ipData, RahasData rahasData)
        throws IdentityProviderException {
    if (log.isDebugEnabled()) {
        log.debug("Begin SAML statement creation.");
    }
    attributeStmt = (AttributeStatement) buildXMLObject(AttributeStatement.DEFAULT_ELEMENT_NAME);

    Subject subject = (Subject) buildXMLObject(Subject.DEFAULT_ELEMENT_NAME);
    SubjectConfirmation subjectConf =
            (SubjectConfirmation) buildXMLObject(SubjectConfirmation.DEFAULT_ELEMENT_NAME);
    ConfirmationMethod confMethod = (ConfirmationMethod) buildXMLObject(ConfirmationMethod.DEFAULT_ELEMENT_NAME);
    confMethod.setConfirmationMethod(CONF_KEY);
    subjectConf.getConfirmationMethods().add(confMethod);
    subject.setSubjectConfirmation(subjectConf);

    attributeStmt.setSubject(subject);

    Map<String, RequestedClaimData> mapClaims = ipData.getRequestedClaims();

    if (rahasData.getAppliesToAddress() != null) {
        appilesTo = rahasData.getAppliesToAddress();
    }

    Iterator<RequestedClaimData> ite = mapClaims.values().iterator();

    while (ite.hasNext()) {
        RequestedClaimData claim = ite.next();
        String uri = claim.getUri();

        int index = uri.lastIndexOf("/");
        String attrName = uri.substring(index + 1, uri.length());
        String attrNamespace = uri.substring(0, index);

        Attribute attribute = (Attribute) buildXMLObject(Attribute.DEFAULT_ELEMENT_NAME);
        attribute.setAttributeName(attrName);
        attribute.setAttributeNamespace(attrNamespace);

        XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
        XSStringBuilder attributeValueBuilder = (XSStringBuilder) builderFactory.getBuilder(XSString.TYPE_NAME);

        XSString stringValue =
                attributeValueBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
        stringValue.setValue(claim.getValue());
        attribute.getAttributeValues().add(stringValue);

        attributeStmt.getAttributes().add(attribute);
    }
}
 
Example #13
Source File: SAMLClient.java    From saml-sdk-java with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private String createAuthnRequest(String requestId)
    throws SAMLException
{
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

    SAMLObjectBuilder<AuthnRequest> builder =
        (SAMLObjectBuilder<AuthnRequest>) builderFactory
        .getBuilder(AuthnRequest.DEFAULT_ELEMENT_NAME);

    SAMLObjectBuilder<Issuer> issuerBuilder =
        (SAMLObjectBuilder<Issuer>) builderFactory
        .getBuilder(Issuer.DEFAULT_ELEMENT_NAME);

    AuthnRequest request = builder.buildObject();
    request.setAssertionConsumerServiceURL(spConfig.getAcs().toString());
    request.setDestination(idpConfig.getLoginUrl().toString());
    request.setIssueInstant(new DateTime());
    request.setID(requestId);

    Issuer issuer = issuerBuilder.buildObject();
    issuer.setValue(spConfig.getEntityId());
    request.setIssuer(issuer);

    try {
        // samlobject to xml dom object
        Element elem = Configuration.getMarshallerFactory()
            .getMarshaller(request)
            .marshall(request);

        // and to a string...
        Document document = elem.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document
            .getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        serializer.getDomConfig().setParameter("xml-declaration", false);
        return serializer.writeToString(elem);
    }
    catch (MarshallingException e) {
        throw new SAMLException(e);
    }
}