Relax restriction on HTTP method.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactDecoder.cpp
1 /*
2  *  Copyright 2001-2007 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  * SAML1ArtifactDecoder.cpp
19  *
20  * SAML 1.x Artifact binding/profile message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SAMLArtifact.h"
26 #include "saml1/binding/SAML1MessageDecoder.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30
31 #include <xmltooling/logging.h>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReplayCache.h>
35
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace opensaml {
44     namespace saml1p {
45         class SAML_DLLLOCAL SAML1ArtifactDecoder : public SAML1MessageDecoder
46         {
47         public:
48             SAML1ArtifactDecoder() {}
49             virtual ~SAML1ArtifactDecoder() {}
50
51             xmltooling::XMLObject* decode(
52                 std::string& relayState,
53                 const GenericRequest& genericRequest,
54                 SecurityPolicy& policy
55                 ) const;
56         };
57
58         MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
59         {
60             return new SAML1ArtifactDecoder();
61         }
62     };
63 };
64
65 XMLObject* SAML1ArtifactDecoder::decode(
66     string& relayState,
67     const GenericRequest& genericRequest,
68     SecurityPolicy& policy
69     ) const
70 {
71 #ifdef _DEBUG
72     xmltooling::NDC ndc("decode");
73 #endif
74     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
75
76     log.debug("validating input");
77     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
78     if (!httpRequest)
79         throw BindingException("Unable to cast request object to HTTPRequest type.");
80     vector<const char*> SAMLart;
81     const char* TARGET = httpRequest->getParameter("TARGET");
82     if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
83         throw BindingException("Request missing SAMLart or TARGET query string parameters.");
84     relayState = TARGET;
85
86     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
87         throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
88
89     // Import the artifacts.
90     vector<SAMLArtifact*> artifacts;
91     for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
92         try {
93             log.debug("processing encoded artifact (%s)", *raw);
94
95             // Check replay.
96             ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
97             if (replayCache) {
98                 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
99                     log.error("replay detected of artifact (%s)", *raw);
100                     throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
101                 }
102             }
103             else
104                 log.warn("replay cache was not provided, this is a serious security risk!");
105
106             artifacts.push_back(SAMLArtifact::parse(*raw));
107         }
108         catch (ArtifactException&) {
109             log.error("error parsing artifact (%s)", *raw);
110             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
111             throw;
112         }
113         catch (XMLToolingException&) {
114             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
115             throw;
116         }
117     }
118
119     log.debug("attempting to determine source of artifact(s)...");
120     MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
121     mc.artifact = artifacts.front();
122     mc.role = policy.getRole();
123     mc.protocol = samlconstants::SAML11_PROTOCOL_ENUM;
124     mc.protocol2 = samlconstants::SAML10_PROTOCOL_ENUM;
125     pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
126     if (!provider.first) {
127         log.error(
128             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
129             SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
130             );
131         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
132         throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
133     }
134
135     if (log.isDebugEnabled()) {
136         auto_ptr_char issuer(provider.first->getEntityID());
137         log.debug("artifact issued by (%s)", issuer.get());
138     }
139
140     if (!provider.second || !dynamic_cast<const IDPSSODescriptor*>(provider.second)) {
141         log.error("unable to find compatible SAML 1.x role (%s) in metadata", policy.getRole()->toString().c_str());
142         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
143         throw BindingException("Unable to find compatible metadata role for artifact issuer.");
144     }
145     // Set Issuer for the policy.
146     policy.setIssuer(provider.first->getEntityID());
147     policy.setIssuerMetadata(provider.second);
148
149     try {
150         log.debug("calling ArtifactResolver...");
151         auto_ptr<Response> response(
152             m_artifactResolver->resolve(artifacts, dynamic_cast<const IDPSSODescriptor&>(*provider.second), policy)
153             );
154
155         // The policy should be enforced against the Response by the resolve step.
156
157         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
158         return response.release();
159     }
160     catch (XMLToolingException&) {
161         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
162         throw;
163     }
164 }