2 * Copyright 2001-2007 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 * SAML2ArtifactDecoder.cpp
20 * SAML 2.0 Artifact binding message decoder
24 #include "exceptions.h"
25 #include "binding/MessageDecoder.h"
26 #include "saml2/binding/SAML2Artifact.h"
27 #include "saml2/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReplayCache.h>
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml2p;
38 using namespace opensaml::saml2;
39 using namespace opensaml;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 class SAML_DLLLOCAL SAML2ArtifactDecoder : public MessageDecoder
49 SAML2ArtifactDecoder(const DOMElement* e) {}
50 virtual ~SAML2ArtifactDecoder() {}
52 xmltooling::XMLObject* decode(
53 std::string& relayState,
54 const GenericRequest& genericRequest,
55 SecurityPolicy& policy
59 MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const DOMElement* const & e)
61 return new SAML2ArtifactDecoder(e);
66 XMLObject* SAML2ArtifactDecoder::decode(
68 const GenericRequest& genericRequest,
69 SecurityPolicy& policy
73 xmltooling::NDC ndc("decode");
75 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Artifact");
77 log.debug("validating input");
78 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
80 log.error("unable to cast request to HTTPRequest type");
83 const char* SAMLart = httpRequest->getParameter("SAMLart");
86 const char* state = httpRequest->getParameter("RelayState");
90 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
91 throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
93 // Import the artifact.
94 SAMLArtifact* artifact=NULL;
96 log.debug("processing encoded artifact (%s)", SAMLart);
99 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
101 if (!replayCache->check("SAML2Artifact", SAMLart, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
102 log.error("replay detected of artifact (%s)", SAMLart);
103 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
107 log.warn("replay cache was not provided, this is a serious security risk!");
109 artifact = SAMLArtifact::parse(SAMLart);
111 catch (ArtifactException&) {
112 log.error("error parsing artifact (%s)", SAMLart);
117 auto_ptr<SAML2Artifact> artifact2(dynamic_cast<SAML2Artifact*>(artifact));
118 if (!artifact2.get()) {
119 throw BindingException("Artifact binding requires SAML 2.0 artifact.");
123 log.debug("attempting to determine source of artifact...");
124 const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifact);
127 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
128 SAMLArtifact::toHex(artifact->getBytes()).c_str()
130 throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
133 if (log.isDebugEnabled()) {
134 auto_ptr_char issuer(provider->getEntityID());
135 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
138 // Mock up an Issuer object for the policy.
139 auto_ptr<Issuer> issuer(IssuerBuilder::buildIssuer());
140 issuer->setName(provider->getEntityID());
141 policy.setIssuer(issuer.get());
142 issuer.release(); // owned by policy now
144 log.debug("attempting to find artifact issuing role...");
145 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
146 if (!roledesc || !dynamic_cast<const SSODescriptorType*>(roledesc)) {
147 log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
148 throw BindingException("Unable to find compatible metadata role for artifact issuer.");
150 policy.setIssuerMetadata(roledesc);
152 log.debug("calling ArtifactResolver...");
153 auto_ptr<ArtifactResponse> response(
154 m_artifactResolver->resolve(*(artifact2.get()), dynamic_cast<const SSODescriptorType&>(*roledesc), policy)
157 // The policy should be enforced against the ArtifactResponse by the resolve step.
159 // Extract payload and check that message.
160 XMLObject* payload = response->getPayload();
161 policy.evaluate(*payload, &genericRequest);
163 // Return the payload only.