eb6bd20c8c0978880e45a8fd6dcc309474e99cc2
[shibboleth/sp.git] / shibsp / binding / impl / ArtifactResolver.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  * ArtifactResolver.cpp
19  * 
20  * SAML artifact resolver for SP use.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "binding/ArtifactResolver.h"
27 #include "binding/SOAPClient.h"
28 #include "security/SecurityPolicy.h"
29
30 #include <saml/saml1/core/Protocols.h>
31 #include <saml/saml1/binding/SAML1SOAPClient.h>
32 #include <saml/saml2/core/Protocols.h>
33 #include <saml/saml2/binding/SAML2Artifact.h>
34 #include <saml/saml2/binding/SAML2SOAPClient.h>
35 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
36 #include <saml/util/SAMLConstants.h>
37
38 #include <log4cpp/Category.hh>
39
40 using namespace shibsp;
41 using namespace opensaml::saml1p;
42 using namespace opensaml::saml2;
43 using namespace opensaml::saml2p;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
46 using namespace log4cpp;
47 using namespace xmltooling;
48 using namespace std;
49
50 saml1p::Response* ArtifactResolver::resolve(
51     const vector<SAMLArtifact*>& artifacts,
52     const IDPSSODescriptor& idpDescriptor,
53     opensaml::SecurityPolicy& policy
54     ) const
55 {
56     MetadataCredentialCriteria mcc(idpDescriptor);
57     shibsp::SOAPClient soaper(dynamic_cast<shibsp::SecurityPolicy&>(policy));
58
59     auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
60     saml1p::Response* response=NULL;
61     const vector<ArtifactResolutionService*>& endpoints=idpDescriptor.getArtifactResolutionServices();
62     for (vector<ArtifactResolutionService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
63         try {
64             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
65                 continue;
66             auto_ptr_char loc((*ep)->getLocation());
67             saml1p::Request* request = saml1p::RequestBuilder::buildRequest();
68             request->setMinorVersion(idpDescriptor.hasSupport(samlconstants::SAML11_PROTOCOL_ENUM) ? 1 : 0);
69             for (vector<SAMLArtifact*>::const_iterator a = artifacts.begin(); a!=artifacts.end(); ++a) {
70                 auto_ptr_XMLCh artbuf((*a)->encode().c_str());
71                 AssertionArtifact* aa = AssertionArtifactBuilder::buildAssertionArtifact();
72                 aa->setArtifact(artbuf.get());
73                 request->getAssertionArtifacts().push_back(aa);
74             }
75
76             SAML1SOAPClient client(soaper);
77             client.sendSAML(request, mcc, loc.get());
78             response = client.receiveSAML();
79         }
80         catch (exception& ex) {
81             Category::getInstance(SHIBSP_LOGCAT".ArtifactResolver").error("exception resolving SAML 1.x artifact(s): %s", ex.what());
82             soaper.reset();
83         }
84     }
85
86     if (!response)
87         throw BindingException("Unable to successfully resolve artifact(s).");
88     return response;
89 }
90
91 ArtifactResponse* ArtifactResolver::resolve(
92     const SAML2Artifact& artifact,
93     const SSODescriptorType& ssoDescriptor,
94     opensaml::SecurityPolicy& policy
95     ) const
96 {
97     MetadataCredentialCriteria mcc(ssoDescriptor);
98     shibsp::SecurityPolicy& sppolicy = dynamic_cast<shibsp::SecurityPolicy&>(policy);
99     shibsp::SOAPClient soaper(sppolicy);
100
101     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
102     ArtifactResponse* response=NULL;
103     const vector<ArtifactResolutionService*>& endpoints=ssoDescriptor.getArtifactResolutionServices();
104     for (vector<ArtifactResolutionService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
105         try {
106             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
107                 continue;
108             auto_ptr_char loc((*ep)->getLocation());
109             auto_ptr_XMLCh issuer(sppolicy.getApplication().getString("entityID").second);
110             ArtifactResolve* request = ArtifactResolveBuilder::buildArtifactResolve();
111             Issuer* iss = IssuerBuilder::buildIssuer();
112             request->setIssuer(iss);
113             iss->setName(issuer.get());
114             auto_ptr_XMLCh artbuf(artifact.encode().c_str());
115             Artifact* a = ArtifactBuilder::buildArtifact();
116             a->setArtifact(artbuf.get());
117             request->setArtifact(a);
118
119             SAML2SOAPClient client(soaper);
120             client.sendSAML(request, mcc, loc.get());
121             StatusResponseType* srt = client.receiveSAML();
122             if (!(response = dynamic_cast<ArtifactResponse*>(srt))) {
123                 delete srt;
124                 break;
125             }
126         }
127         catch (exception& ex) {
128             Category::getInstance(SHIBSP_LOGCAT".ArtifactResolver").error("exception resolving SAML 2.0 artifact: %s", ex.what());
129             soaper.reset();
130         }
131     }
132
133     if (!response)
134         throw BindingException("Unable to successfully resolve artifact.");
135     return response;
136 }