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.
20 * Tool to exercise SP attribute subsystems.
23 #if defined (_MSC_VER) || defined(__BORLANDC__)
24 # include "config_win32.h"
30 # define _CRT_NONSTDC_NO_DEPRECATE 1
31 # define _CRT_SECURE_NO_DEPRECATE 1
34 #include <shibsp/Application.h>
35 #include <shibsp/exceptions.h>
36 #include <shibsp/SPConfig.h>
37 #include <shibsp/ServiceProvider.h>
38 #include <shibsp/attribute/Attribute.h>
39 #include <shibsp/attribute/resolver/ResolutionContext.h>
40 #include <shibsp/handler/AssertionConsumerService.h>
41 #include <shibsp/util/SPConstants.h>
43 #include <saml/saml1/core/Assertions.h>
44 #include <saml/saml2/core/Assertions.h>
45 #include <saml/saml2/metadata/Metadata.h>
46 #include <xercesc/util/XMLUniDefs.hpp>
47 #include <xmltooling/XMLToolingConfig.h>
48 #include <xmltooling/util/XMLHelper.h>
50 using namespace shibsp;
51 using namespace opensaml::saml2md;
52 using namespace opensaml;
53 using namespace xmltooling::logging;
54 using namespace xmltooling;
55 using namespace xercesc;
58 #if defined (_MSC_VER)
59 #pragma warning( push )
60 #pragma warning( disable : 4250 )
63 class ResolverTest : public shibsp::AssertionConsumerService
66 ResolverTest(const DOMElement* e, const char* appId)
67 : shibsp::AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".Utilities.ResolverTest")) {
69 virtual ~ResolverTest() {}
71 ResolutionContext* resolveAttributes (
72 const Application& application,
73 const RoleDescriptor* issuer,
74 const XMLCh* protocol,
75 const saml1::NameIdentifier* v1nameid,
76 const saml2::NameID* nameid,
77 const XMLCh* authncontext_class,
78 const XMLCh* authncontext_decl,
79 const vector<const Assertion*>* tokens
81 return shibsp::AssertionConsumerService::resolveAttributes(
82 application, issuer, protocol, v1nameid, nameid, authncontext_class, authncontext_decl, tokens
87 string implementProtocol(
88 const Application& application,
89 const HTTPRequest& httpRequest,
90 SecurityPolicy& policy,
91 const PropertySet* settings,
92 const XMLObject& xmlObject
94 throw FatalProfileException("Should never be called.");
98 #if defined (_MSC_VER)
99 #pragma warning( pop )
104 cerr << "usage: resolvertest -n <name> -i <IdP> -p <protocol> [-f <format URI> -a <application id>]" << endl;
105 cerr << " resolvertest [-a <application id>] < assertion.xml" << endl;
108 int main(int argc,char* argv[])
115 const XMLCh* protocol = NULL;
119 for (int i=1; i<argc; i++) {
120 if (!strcmp(argv[i],"-n") && i+1<argc)
122 else if (!strcmp(argv[i],"-f") && i+1<argc)
124 else if (!strcmp(argv[i],"-i") && i+1<argc)
126 else if (!strcmp(argv[i],"-p") && i+1<argc)
128 else if (!strcmp(argv[i],"-saml10"))
129 protocol=samlconstants::SAML10_PROTOCOL_ENUM;
130 else if (!strcmp(argv[i],"-saml11"))
131 protocol=samlconstants::SAML11_PROTOCOL_ENUM;
132 else if (!strcmp(argv[i],"-saml2"))
133 protocol=samlconstants::SAML20P_NS;
134 else if (!strcmp(argv[i],"-a") && i+1<argc)
138 if (n_param && !i_param) {
143 path=getenv("SHIBSP_SCHEMAS");
146 config=getenv("SHIBSP_CONFIG");
148 config=SHIBSP_CONFIG;
152 XMLToolingConfig::getConfig().log_config(getenv("SHIBSP_LOGGING") ? getenv("SHIBSP_LOGGING") : SHIBSP_LOGGING);
154 SPConfig& conf=SPConfig::getConfig();
158 SPConfig::AttributeResolution |
159 SPConfig::Credentials |
160 SPConfig::OutOfProcess
162 if (!conf.init(path))
168 protocol = XMLString::transcode(prot);
178 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
179 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
180 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
181 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
182 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
183 auto_ptr_XMLCh src(config);
184 dummy->setAttributeNS(NULL,path,src.get());
185 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
187 conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
188 conf.getServiceProvider()->init();
195 ServiceProvider* sp=conf.getServiceProvider();
198 Category& log = Category::getInstance(SHIBSP_LOGCAT".Utility.ResolverTest");
200 const Application* app = sp->getApplication(a_param);
202 log.error("unknown application ID (%s)", a_param);
209 ResolutionContext* ctx;
212 auto_ptr_XMLCh issuer(i_param);
213 auto_ptr_XMLCh name(n_param);
214 auto_ptr_XMLCh format(f_param);
216 MetadataProvider* m=app->getMetadataProvider();
217 xmltooling::Locker mlocker(m);
218 MetadataProvider::Criteria mc(i_param, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
219 pair<const EntityDescriptor*,const RoleDescriptor*> site=m->getEntityDescriptor(mc);
221 throw MetadataException("Unable to locate metadata for IdP ($1).", params(1,i_param));
224 auto_ptr<saml2::NameID> v2name(saml2::NameIDBuilder::buildNameID());
225 v2name->setName(name.get());
226 v2name->setFormat(format.get());
227 saml1::NameIdentifier* v1name = NULL;
228 if (!XMLString::equals(protocol, samlconstants::SAML20P_NS)) {
229 v1name = saml1::NameIdentifierBuilder::buildNameIdentifier();
230 v1name->setName(name.get());
231 v1name->setFormat(format.get());
232 v1name->setNameQualifier(issuer.get());
235 ResolverTest rt(NULL, a_param);
237 ctx = rt.resolveAttributes(*app, site.second, protocol, v1name, v2name.get(), NULL, NULL, NULL);
245 // Try and load assertion from stdin.
246 DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(cin);
247 XercesJanitor<DOMDocument> docjan(doc);
248 auto_ptr<XMLObject> token(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
251 // Get the issuer and protocol and NameIDs.
252 const XMLCh* issuer = NULL;
253 const saml1::NameIdentifier* v1name = NULL;
254 saml2::NameID* v2name = NULL;
255 saml2::Assertion* a2 = dynamic_cast<saml2::Assertion*>(token.get());
256 saml1::Assertion* a1 = dynamic_cast<saml1::Assertion*>(token.get());
258 const saml2::Issuer* iss = a2->getIssuer();
259 issuer = iss ? iss->getName() : NULL;
260 protocol = samlconstants::SAML20P_NS;
261 v2name = a2->getSubject() ? a2->getSubject()->getNameID() : NULL;
264 issuer = a1->getIssuer();
265 if (a1->getMinorVersion().first && a1->getMinorVersion().second == 0)
266 protocol = samlconstants::SAML10_PROTOCOL_ENUM;
268 protocol = samlconstants::SAML11_PROTOCOL_ENUM;
269 v1name = a1->getAuthenticationStatements().size() ?
270 a1->getAuthenticationStatements().front()->getSubject()->getNameIdentifier() : NULL;
271 // Normalize the SAML 1.x NameIdentifier...
272 v2name = saml2::NameIDBuilder::buildNameID();
273 v2name->setName(v1name->getName());
274 v2name->setFormat(v1name->getFormat());
275 v2name->setNameQualifier(v1name->getNameQualifier());
278 throw FatalProfileException("Unknown assertion type.");
284 throw FatalProfileException("Unable to determine issuer.");
287 MetadataProvider* m=app->getMetadataProvider();
288 xmltooling::Locker mlocker(m);
289 MetadataProvider::Criteria mc(issuer, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
290 pair<const EntityDescriptor*,const RoleDescriptor*> site=m->getEntityDescriptor(mc);
292 auto_ptr_char temp(issuer);
293 throw MetadataException("Unable to locate metadata for IdP ($1).", params(1,temp.get()));
296 vector<const Assertion*> tokens(1, dynamic_cast<Assertion*>(token.get()));
297 ResolverTest rt(NULL, a_param);
299 ctx = rt.resolveAttributes(*app, site.second, protocol, v1name, v2name, NULL, NULL, &tokens);
308 auto_ptr<ResolutionContext> wrapper(ctx);
309 for (vector<Attribute*>::const_iterator a = ctx->getResolvedAttributes().begin(); a != ctx->getResolvedAttributes().end(); ++a) {
311 for (vector<string>::const_iterator s = (*a)->getAliases().begin(); s != (*a)->getAliases().end(); ++s)
312 cout << "ID: " << *s << endl;
313 for (vector<string>::const_iterator s = (*a)->getSerializedValues().begin(); s != (*a)->getSerializedValues().end(); ++s)
314 cout << "Value: " << *s << endl;
318 catch(exception& ex) {
319 log.error(ex.what());