Policy rule redesign for factor out issuer handling.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactDecoder.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  * 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/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"
30
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/ReplayCache.h>
34
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml1p;
37 using namespace opensaml;
38 using namespace xmltooling;
39 using namespace log4cpp;
40 using namespace std;
41
42 namespace opensaml {
43     namespace saml1p {              
44         MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const DOMElement* const & e)
45         {
46             return new SAML1ArtifactDecoder(e);
47         }
48     };
49 };
50
51 SAML1ArtifactDecoder::SAML1ArtifactDecoder(const DOMElement* e) {}
52
53 XMLObject* SAML1ArtifactDecoder::decode(
54     string& relayState,
55     const GenericRequest& genericRequest,
56     SecurityPolicy& policy
57     ) const
58 {
59 #ifdef _DEBUG
60     xmltooling::NDC ndc("decode");
61 #endif
62     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
63
64     log.debug("validating input");
65     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
66     if (!httpRequest) {
67         log.error("unable to cast request to HTTPRequest type");
68         return NULL;
69     }
70     if (strcmp(httpRequest->getMethod(),"GET"))
71         return NULL;
72     vector<const char*> SAMLart;
73     const char* TARGET = httpRequest->getParameter("TARGET");
74     if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
75         return NULL;
76     relayState = TARGET;
77
78     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
79         throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
80
81     // Import the artifacts.
82     vector<SAMLArtifact*> artifacts;
83     for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
84         try {
85             log.debug("processing encoded artifact (%s)", *raw);
86             
87             // Check replay.
88             ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
89             if (replayCache) {
90                 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
91                     log.error("replay detected of artifact (%s)", *raw);
92                     throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
93                 }
94             }
95             else
96                 log.warn("replay cache was not provided, this is a serious security risk!");
97
98             artifacts.push_back(SAMLArtifact::parse(*raw));
99         }
100         catch (ArtifactException&) {
101             log.error("error parsing artifact (%s)", *raw);
102             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
103             throw;
104         }
105         catch (XMLToolingException&) {
106             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
107             throw;
108         }
109     }
110     
111     log.debug("attempting to determine source of artifact(s)...");
112     const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifacts.front());
113     if (!provider) {
114         log.error(
115             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
116             SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
117             );
118         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
119         throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
120     }
121     
122     if (log.isDebugEnabled()) {
123         auto_ptr_char issuer(provider->getEntityID());
124         log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
125     }
126
127     // Mock up an Issuer object for the policy.
128     auto_ptr<saml2::Issuer> issuer(saml2::IssuerBuilder::buildIssuer());
129     issuer->setName(provider->getEntityID());
130     policy.setIssuer(issuer.get());
131     issuer.release();   // owned by policy now
132     
133     log.debug("attempting to find artifact issuing role...");
134     const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML11_PROTOCOL_ENUM);
135     if (!roledesc)
136         roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML10_PROTOCOL_ENUM);
137     if (!roledesc || !dynamic_cast<const IDPSSODescriptor*>(roledesc)) {
138         log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
139         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
140         throw BindingException("Unable to find compatible metadata role for artifact issuer.");
141     }
142     policy.setIssuerMetadata(roledesc);
143     
144     try {
145         auto_ptr<Response> response(
146             m_artifactResolver->resolve(artifacts, dynamic_cast<const IDPSSODescriptor&>(*roledesc), policy)
147             );
148         
149         policy.evaluate(*(response.get()), &genericRequest);
150         
151         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
152         return response.release();
153     }
154     catch (XMLToolingException&) {
155         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
156         throw;
157     }
158 }