2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * SAML2ArtifactEncoder.cpp
20 * SAML 2.0 HTTP-Artifact binding message encoder
24 #include "exceptions.h"
25 #include "saml2/binding/SAML2Artifact.h"
26 #include "saml2/binding/SAML2ArtifactEncoder.h"
27 #include "saml2/core/Protocols.h"
29 #include <log4cpp/Category.hh>
30 #include <xmltooling/util/NDC.h>
32 using namespace opensaml::saml2p;
33 using namespace opensaml;
34 using namespace xmlsignature;
35 using namespace xmltooling;
36 using namespace log4cpp;
41 MessageEncoder* SAML_DLLLOCAL SAML2ArtifactEncoderFactory(const DOMElement* const & e)
43 return new SAML2ArtifactEncoder(e);
48 SAML2ArtifactEncoder::SAML2ArtifactEncoder(const DOMElement* e) {}
50 SAML2ArtifactEncoder::~SAML2ArtifactEncoder() {}
52 void SAML2ArtifactEncoder::encode(
53 map<string,string>& outputFields,
55 const char* recipientID,
56 const char* relayState,
57 const CredentialResolver* credResolver,
58 const XMLCh* sigAlgorithm
62 xmltooling::NDC ndc("encode");
64 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Artifact");
65 log.debug("validating input");
68 if (xmlObject->getParent())
69 throw BindingException("Cannot encode XML content with parent.");
71 StatusResponseType* response = NULL;
72 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
74 response = dynamic_cast<StatusResponseType*>(xmlObject);
76 throw BindingException("XML content for SAML 2.0 HTTP-Artifact Encoder must be a SAML 2.0 protocol message.");
78 ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
80 throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires ArtifactMap be set in configuration.");
82 // Obtain a fresh artifact.
83 if (!m_artifactGenerator)
84 throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires an ArtifactGenerator instance.");
85 log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
86 auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML2Artifact(recipientID));
89 // Signature based on native XML signing.
90 if (request ? request->getSignature() : response->getSignature()) {
91 log.debug("message already signed, skipping signature operation");
94 log.debug("signing the message");
97 Signature* sig = buildSignature(credResolver, sigAlgorithm);
100 request ? request->setSignature(sig) : response->setSignature(sig);
102 // Sign response while marshalling.
103 vector<Signature*> sigs(1,sig);
104 xmlObject->marshall((DOMDocument*)NULL,&sigs);
108 // Pass back output fields.
109 outputFields["SAMLart"] = artifact->encode();
111 outputFields["RelayState"] = relayState;
113 // Store the message. Last step in storage will be to delete the XML.
114 log.debug("storing artifact and content in map");
115 mapper->storeContent(xmlObject, artifact.get(), recipientID);
117 log.debug("message encoded");