eec06cb2d609f99b6d5444112750664526a45df1
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ArtifactEncoder.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SAML2ArtifactEncoder.cpp
19  * 
20  * SAML 2.0 HTTP-Artifact binding message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml/binding/ArtifactMap.h"
26 #include "saml2/binding/SAML2Artifact.h"
27 #include "saml2/binding/SAML2ArtifactEncoder.h"
28 #include "saml2/core/Protocols.h"
29
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/util/NDC.h>
32
33 using namespace opensaml::saml2p;
34 using namespace opensaml;
35 using namespace xmlsignature;
36 using namespace xmltooling;
37 using namespace log4cpp;
38 using namespace std;
39
40 namespace opensaml {
41     namespace saml2p {              
42         MessageEncoder* SAML_DLLLOCAL SAML2ArtifactEncoderFactory(const DOMElement* const & e)
43         {
44             return new SAML2ArtifactEncoder(e);
45         }
46     };
47 };
48
49 SAML2ArtifactEncoder::SAML2ArtifactEncoder(const DOMElement* e) {}
50
51 SAML2ArtifactEncoder::~SAML2ArtifactEncoder() {}
52
53 void SAML2ArtifactEncoder::encode(
54     map<string,string>& outputFields,
55     XMLObject* xmlObject,
56     const char* recipientID,
57     const char* relayState,
58     const CredentialResolver* credResolver,
59     const XMLCh* sigAlgorithm
60     ) const
61 {
62 #ifdef _DEBUG
63     xmltooling::NDC ndc("encode");
64 #endif
65     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Artifact");
66     log.debug("validating input");
67     
68     outputFields.clear();
69     if (xmlObject->getParent())
70         throw BindingException("Cannot encode XML content with parent.");
71
72     StatusResponseType* response = NULL;
73     RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
74     if (!request)
75         response = dynamic_cast<StatusResponseType*>(xmlObject);
76     if (!response)
77         throw BindingException("XML content for SAML 2.0 HTTP-Artifact Encoder must be a SAML 2.0 protocol message.");
78     
79     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
80     if (!mapper)
81         throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires ArtifactMap be set in configuration.");
82
83     // Obtain a fresh artifact.
84     if (!m_artifactGenerator)
85         throw BindingException("SAML 2.0 HTTP-Artifact Encoder requires an ArtifactGenerator instance.");
86     log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
87     auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML2Artifact(recipientID));
88
89     if (credResolver) {
90         // Signature based on native XML signing.
91         if (request ? request->getSignature() : response->getSignature()) {
92             log.debug("message already signed, skipping signature operation");
93         }
94         else {
95             log.debug("signing the message");
96
97             // Build a Signature.
98             Signature* sig = buildSignature(credResolver, sigAlgorithm);
99             
100             // Append Signature.
101             request ? request->setSignature(sig) : response->setSignature(sig);    
102         
103             // Sign response while marshalling.
104             vector<Signature*> sigs(1,sig);
105             xmlObject->marshall((DOMDocument*)NULL,&sigs);
106         }
107     }
108     
109     // Pass back output fields.
110     outputFields["SAMLart"] = artifact->encode();
111     if (relayState)
112         outputFields["RelayState"] = relayState;
113
114     // Store the message. Last step in storage will be to delete the XML.
115     log.debug("storing artifact and content in map");
116     mapper->storeContent(xmlObject, artifact.get(), recipientID);
117
118     log.debug("message encoded");
119 }