1cd434e89293a0689ef88119e2c0661dcf7d145f
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ArtifactDecoder.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  * SAML2ArtifactDecoder.cpp
19  * 
20  * SAML 2.0 Artifact binding message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "saml/binding/SAMLArtifact.h"
27 #include "saml2/binding/SAML2Artifact.h"
28 #include "saml2/binding/SAML2ArtifactDecoder.h"
29 #include "saml2/core/Protocols.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/MetadataProvider.h"
32
33 #include <log4cpp/Category.hh>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/ReplayCache.h>
36
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmltooling;
42 using namespace log4cpp;
43 using namespace std;
44
45 namespace opensaml {
46     namespace saml2p {              
47         MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const DOMElement* const & e)
48         {
49             return new SAML2ArtifactDecoder(e);
50         }
51     };
52 };
53
54 SAML2ArtifactDecoder::SAML2ArtifactDecoder(const DOMElement* e) {}
55
56 SAML2ArtifactDecoder::~SAML2ArtifactDecoder() {}
57
58 XMLObject* SAML2ArtifactDecoder::decode(
59     string& relayState,
60     const GenericRequest& genericRequest,
61     SecurityPolicy& policy
62     ) const
63 {
64 #ifdef _DEBUG
65     xmltooling::NDC ndc("decode");
66 #endif
67     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Artifact");
68
69     log.debug("validating input");
70     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
71     if (!httpRequest) {
72         log.error("unable to cast request to HTTPRequest type");
73         return NULL;
74     }
75     const char* SAMLart = httpRequest->getParameter("SAMLart");
76     if (!SAMLart)
77         return NULL;
78     const char* state = httpRequest->getParameter("RelayState");
79     if (state)
80         relayState = state;
81
82     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
83         throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
84
85     // Import the artifact.
86     SAMLArtifact* artifact=NULL;
87     try {
88         log.debug("processing encoded artifact (%s)", SAMLart);
89         
90         // Check replay.
91         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
92         if (replayCache) {
93             if (!replayCache->check("SAML2Artifact", SAMLart, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
94                 log.error("replay detected of artifact (%s)", SAMLart);
95                 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
96             }
97         }
98         else
99             log.warn("replay cache was not provided, this is a serious security risk!");
100
101         artifact = SAMLArtifact::parse(SAMLart);
102     }
103     catch (ArtifactException&) {
104         log.error("error parsing artifact (%s)", SAMLart);
105         throw;
106     }
107     
108     // Check the type.
109     auto_ptr<SAML2Artifact> artifact2(dynamic_cast<SAML2Artifact*>(artifact));
110     if (!artifact2.get()) {
111         throw BindingException("Artifact binding requires SAML 2.0 artifact.");
112         delete artifact;
113     }
114     
115     log.debug("attempting to determine source of artifact...");
116     const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifact);
117     if (!provider) {
118         log.error(
119             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
120             SAMLArtifact::toHex(artifact->getBytes()).c_str()
121             );
122         throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
123     }
124     
125     if (log.isDebugEnabled()) {
126         auto_ptr_char issuer(provider->getEntityID());
127         log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
128     }
129     
130     log.debug("attempting to find artifact issuing role...");
131     const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
132     if (!roledesc || !dynamic_cast<const SSODescriptorType*>(roledesc)) {
133         log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
134         BindingException ex("Unable to find compatible metadata role for artifact issuer.");
135         annotateException(&ex,provider); // throws it
136     }
137     
138     try {
139         auto_ptr<ArtifactResponse> response(
140             m_artifactResolver->resolve(*(artifact2.get()), dynamic_cast<const SSODescriptorType&>(*roledesc), policy)
141             );
142         
143         policy.evaluate(genericRequest, *(response.get()));
144
145         // Extract payload and check that message.
146         XMLObject* payload = response->getPayload();
147         policy.evaluate(genericRequest, *payload);
148
149         // Return the payload only.
150         response.release();
151         payload->detach(); 
152         return payload;
153     }
154     catch (XMLToolingException& ex) {
155         annotateException(&ex,roledesc,false);
156         throw;
157     }
158 }