use of org.opensaml.common.impl.SAMLObjectContentReference in project product-is by wso2.
the class SAML2SSOTestBase method setSignature.
/**
* Add Signature to SAML POST request
*
* @param request SAML authentication request.
* @param signatureAlgorithm Signature Algorithm.
* @param digestAlgorithm Digest algorithm to be used while digesting message.
* @param includeCert Whether to include certificate in request or not.
* @throws Exception
*/
protected void setSignature(RequestAbstractType request, String signatureAlgorithm, String digestAlgorithm, boolean includeCert, X509Credential x509Credential) throws Exception {
doBootstrap();
if (StringUtils.isEmpty(signatureAlgorithm)) {
signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA;
}
if (StringUtils.isEmpty(digestAlgorithm)) {
digestAlgorithm = XML_DIGEST_ALGORITHM_SHA1;
}
Signature signature = (Signature) buildXMLObject(Signature.DEFAULT_ELEMENT_NAME);
signature.setSigningCredential(x509Credential);
signature.setSignatureAlgorithm(signatureAlgorithm);
signature.setCanonicalizationAlgorithm(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
if (includeCert) {
KeyInfo keyInfo = (KeyInfo) buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME);
X509Data data = (X509Data) buildXMLObject(X509Data.DEFAULT_ELEMENT_NAME);
org.opensaml.xml.signature.X509Certificate cert = (org.opensaml.xml.signature.X509Certificate) buildXMLObject(org.opensaml.xml.signature.X509Certificate.DEFAULT_ELEMENT_NAME);
String value = null;
value = org.apache.xml.security.utils.Base64.encode(x509Credential.getEntityCertificate().getEncoded());
cert.setValue(value);
data.getX509Certificates().add(cert);
keyInfo.getX509Datas().add(data);
signature.setKeyInfo(keyInfo);
}
request.setSignature(signature);
((SAMLObjectContentReference) signature.getContentReferences().get(0)).setDigestAlgorithm(digestAlgorithm);
List<Signature> signatureList = new ArrayList<Signature>();
signatureList.add(signature);
// Marshall and Sign
MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory();
Marshaller marshaller = marshallerFactory.getMarshaller(request);
marshaller.marshall(request);
org.apache.xml.security.Init.init();
Signer.signObjects(signatureList);
}
Aggregations