5b0704fde7483ff10c25280eb98e43ef90d6fb87
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1POSTEncoder.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  * SAML1POSTEncoder.cpp
19  * 
20  * SAML 1.x POST binding/profile message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml1/binding/SAML1POSTEncoder.h"
26 #include "saml1/core/Protocols.h"
27
28 #include <log4cpp/Category.hh>
29 #include <xercesc/util/Base64.hpp>
30 #include <xmltooling/util/NDC.h>
31
32 using namespace opensaml::saml1p;
33 using namespace opensaml;
34 using namespace xmlsignature;
35 using namespace xmltooling;
36 using namespace log4cpp;
37 using namespace std;
38
39 namespace opensaml {
40     namespace saml1p {              
41         MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const DOMElement* const & e)
42         {
43             return new SAML1POSTEncoder(e);
44         }
45     };
46 };
47
48 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e) {}
49
50 SAML1POSTEncoder::~SAML1POSTEncoder() {}
51
52 void SAML1POSTEncoder::encode(
53     map<string,string>& outputFields,
54     XMLObject* xmlObject,
55     const char* recipientID,
56     const char* relayState,
57     const CredentialResolver* credResolver,
58     const XMLCh* sigAlgorithm
59     ) const
60 {
61 #ifdef _DEBUG
62     xmltooling::NDC ndc("encode");
63 #endif
64     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
65     log.debug("validating input");
66     
67     outputFields.clear();
68     if (xmlObject->getParent())
69         throw BindingException("Cannot encode XML content with parent.");
70     Response* response = dynamic_cast<Response*>(xmlObject);
71     if (!response)
72         throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
73     if (!relayState)
74         throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
75     
76     DOMElement* rootElement = NULL;
77     if (credResolver) {
78         // Signature based on native XML signing.
79         if (response->getSignature()) {
80             log.debug("response already signed, skipping signature operation");
81         }
82         else {
83             log.debug("signing and marshalling the response");
84
85             // Build a Signature.
86             Signature* sig = buildSignature(credResolver, sigAlgorithm);
87             response->setSignature(sig);
88     
89             // Sign response while marshalling.
90             vector<Signature*> sigs(1,sig);
91             rootElement = response->marshall((DOMDocument*)NULL,&sigs);
92         }
93     }
94     else {
95         log.debug("marshalling the response");
96         rootElement = response->marshall();
97     }
98     
99     string xmlbuf;
100     XMLHelper::serialize(rootElement, xmlbuf);
101     unsigned int len=0;
102     XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
103     if (out) {
104         xmlbuf.erase();
105         xmlbuf.append(reinterpret_cast<char*>(out),len);
106         XMLString::release(&out);
107     }
108     else {
109         throw BindingException("Base64 encoding of XML failed.");
110     }
111     
112     // Pass back output fields.
113     outputFields["SAMLResponse"] = xmlbuf;
114     outputFields["TARGET"] = relayState;
115
116     // Cleanup by destroying XML.
117     delete xmlObject;
118
119     log.debug("message encoded");
120 }