cc76f7c0d94cb87796c440d1bc54a9dd268acfdd
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactEncoder.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  * SAML1ArtifactEncoder.cpp
19  * 
20  * SAML 1.x Artifact binding/profile message encoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml/binding/ArtifactMap.h"
26 #include "saml/binding/SAMLArtifact.h"
27 #include "saml1/binding/SAML1ArtifactEncoder.h"
28 #include "saml1/core/Assertions.h"
29
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/util/NDC.h>
32
33 using namespace opensaml::saml1;
34 using namespace opensaml::saml1p;
35 using namespace opensaml;
36 using namespace xmlsignature;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace opensaml {
42     namespace saml1p {              
43         MessageEncoder* SAML_DLLLOCAL SAML1ArtifactEncoderFactory(const DOMElement* const & e)
44         {
45             return new SAML1ArtifactEncoder(e);
46         }
47     };
48 };
49
50 SAML1ArtifactEncoder::SAML1ArtifactEncoder(const DOMElement* e) {}
51
52 SAML1ArtifactEncoder::~SAML1ArtifactEncoder() {}
53
54 void SAML1ArtifactEncoder::encode(
55     map<string,string>& outputFields,
56     XMLObject* xmlObject,
57     const char* recipientID,
58     const char* relayState,
59     const CredentialResolver* credResolver,
60     const XMLCh* sigAlgorithm
61     ) const
62 {
63 #ifdef _DEBUG
64     xmltooling::NDC ndc("encode");
65 #endif
66     Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1Artifact");
67     log.debug("validating input");
68     
69     outputFields.clear();
70     if (xmlObject->getParent())
71         throw BindingException("Cannot encode XML content with parent.");
72     Assertion* assertion = dynamic_cast<Assertion*>(xmlObject);
73     if (!assertion)
74         throw BindingException("XML content for SAML 1.x Artifact Encoder must be a SAML 1.x <Assertion>.");
75     if (!relayState)
76         throw BindingException("SAML 1.x Artifact Encoder requires relay state (TARGET) value.");
77     
78     // Signing is a protocol level issue, so no signing here...
79     
80     ArtifactMap* mapper = SAMLConfig::getConfig().getArtifactMap();
81     if (!mapper)
82         throw BindingException("SAML 1.x Artifact Encoder requires ArtifactMap be set in configuration.");
83
84     // Obtain a fresh artifact.
85     if (!m_artifactGenerator)
86         throw BindingException("SAML 1.x Artifact Encoder requires an ArtifactGenerator instance.");
87     log.debug("obtaining new artifact for relying party (%s)", recipientID ? recipientID : "unknown");
88     auto_ptr<SAMLArtifact> artifact(m_artifactGenerator->generateSAML1Artifact(recipientID));
89     
90     // Pass back output fields.
91     outputFields["SAMLart"] = artifact->encode();
92     outputFields["TARGET"] = relayState;
93
94     // Store the assertion. Last step in storage will be to delete the XML.
95     log.debug("storing artifact and content in map");
96     mapper->storeContent(xmlObject, artifact.get(), recipientID);
97
98     log.debug("message encoded");
99 }