2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML1ArtifactDecoder.cpp
20 * SAML 1.x Artifact binding/profile message decoder
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "saml/binding/SAMLArtifact.h"
27 #include "saml1/binding/SAML1ArtifactDecoder.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/ReplayCache.h>
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml1p;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const DOMElement* const & e)
46 return new SAML1ArtifactDecoder(e);
51 SAML1ArtifactDecoder::SAML1ArtifactDecoder(const DOMElement* e) {}
53 SAML1ArtifactDecoder::~SAML1ArtifactDecoder() {}
55 Response* SAML1ArtifactDecoder::decode(
57 const GenericRequest& genericRequest,
58 SecurityPolicy& policy
62 xmltooling::NDC ndc("decode");
64 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
66 log.debug("validating input");
67 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
69 log.error("unable to cast request to HTTPRequest type");
72 if (strcmp(httpRequest->getMethod(),"GET"))
74 vector<const char*> SAMLart;
75 const char* TARGET = httpRequest->getParameter("TARGET");
76 if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
80 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
81 throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
83 // Import the artifacts.
84 vector<SAMLArtifact*> artifacts;
85 for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
87 log.debug("processing encoded artifact (%s)", *raw);
90 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
92 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
93 log.error("replay detected of artifact (%s)", *raw);
94 throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
98 log.warn("replay cache was not provided, this is a serious security risk!");
100 artifacts.push_back(SAMLArtifact::parse(*raw));
102 catch (ArtifactException&) {
103 log.error("error parsing artifact (%s)", *raw);
104 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
107 catch (XMLToolingException&) {
108 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
113 log.debug("attempting to determine source of artifact(s)...");
114 const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifacts.front());
117 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
118 SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
120 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
121 throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
124 if (log.isDebugEnabled()) {
125 auto_ptr_char issuer(provider->getEntityID());
126 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
129 log.debug("attempting to find artifact issuing role...");
130 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML11_PROTOCOL_ENUM);
132 roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML10_PROTOCOL_ENUM);
133 if (!roledesc || !dynamic_cast<const IDPSSODescriptor*>(roledesc)) {
134 log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
135 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
136 BindingException ex("Unable to find compatible metadata role for artifact issuer.");
137 annotateException(&ex,provider); // throws it
141 auto_ptr<Response> response(
142 m_artifactResolver->resolve(artifacts, dynamic_cast<const IDPSSODescriptor&>(*roledesc), policy)
145 policy.evaluate(genericRequest, *(response.get()));
147 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
148 return response.release();
150 catch (XMLToolingException& ex) {
151 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
152 annotateException(&ex,roledesc,false);