Remove xmlproviders from build, deleted old AAP interface.
[shibboleth/cpp-sp.git] / test / shibtest.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 #ifdef WIN32
18 # define _CRT_NONSTDC_NO_DEPRECATE 1
19 # define _CRT_SECURE_NO_DEPRECATE 1
20 #endif
21
22 #include <shib-target/shib-target.h>
23 #include <shibsp/SPConfig.h>
24 #include <shibsp/util/SPConstants.h>
25
26 using namespace shibsp;
27 using namespace shibtarget;
28 using namespace opensaml::saml2md;
29 using namespace saml;
30 using namespace std;
31
32 int main(int argc,char* argv[])
33 {
34     char* h_param=NULL;
35     char* q_param=NULL;
36     char* f_param=NULL;
37     char* a_param=NULL;
38     char* path=NULL;
39     char* config=NULL;
40
41     for (int i=1; i<argc; i++) {
42         if (!strcmp(argv[i],"-c") && i+1<argc)
43             config=argv[++i];
44         else if (!strcmp(argv[i],"-d") && i+1<argc)
45             path=argv[++i];
46         else if (!strcmp(argv[i],"-h") && i+1<argc)
47             h_param=argv[++i];
48         else if (!strcmp(argv[i],"-q") && i+1<argc)
49             q_param=argv[++i];
50         else if (!strcmp(argv[i],"-f") && i+1<argc)
51             f_param=argv[++i];
52         else if (!strcmp(argv[i],"-a") && i+1<argc)
53             a_param=argv[++i];
54     }
55
56     if (!h_param || !q_param) {
57         cerr << "usage: shibtest -h <handle> -q <origin_site> [-f <format URI> -a <application_id> -d <schema path> -c <config>]" << endl;
58         exit(0);
59     }
60     
61     if (!path)
62         path=getenv("SHIBSCHEMAS");
63     if (!path)
64         path=SHIB_SCHEMAS;
65     if (!config)
66         config=getenv("SHIBCONFIG");
67     if (!config)
68         config=SHIB_CONFIG;
69     if (!a_param)
70         a_param="default";
71
72     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
73     SPConfig::getConfig().setFeatures(
74         SPConfig::Metadata |
75         SPConfig::Trust |
76         SPConfig::Credentials |
77         SPConfig::AAP |
78         SPConfig::OutOfProcess |
79         SPConfig::Caching
80         );
81     if (!conf.init(path) || !conf.load(config))
82         return -10;
83
84     ServiceProvider* sp=SPConfig::getConfig().getServiceProvider();
85     xmltooling::Locker locker(sp);
86
87     try {
88         const IApplication* app=dynamic_cast<const IApplication*>(sp->getApplication(a_param));
89         if (!app)
90             throw SAMLException("specified <Application> section not found in configuration");
91
92         auto_ptr_XMLCh domain(q_param);
93         auto_ptr_XMLCh handle(h_param);
94         auto_ptr_XMLCh format(f_param);
95         auto_ptr_XMLCh resource(app->getString("providerId").second);
96
97         auto_ptr<SAMLRequest> req(
98             new SAMLRequest(
99                 new SAMLAttributeQuery(
100                     new SAMLSubject(
101                         new SAMLNameIdentifier(
102                             handle.get(),
103                             domain.get(),
104                             format.get() ? format.get() : shibspconstants::SHIB1_NAMEID_FORMAT_URI
105                             )
106                         ),
107                     resource.get()
108                     )
109                 )
110             );
111
112         MetadataProvider* m=app->getMetadataProvider();
113         xmltooling::Locker locker(m);
114         const EntityDescriptor* site=m->getEntityDescriptor(domain.get());
115         if (!site)
116             throw MetadataException("Unable to locate specified origin site's metadata.");
117
118         // Try to locate an AA role.
119         const AttributeAuthorityDescriptor* AA=site->getAttributeAuthorityDescriptor(saml::XML::SAML11_PROTOCOL_ENUM);
120         if (!AA)
121             throw MetadataException("Unable to locate metadata for origin site's Attribute Authority.");
122
123         ShibHTTPHook::ShibHTTPHookCallContext ctx(app->getCredentialUse(site),AA);
124
125         SAMLResponse* response=NULL;
126         const vector<AttributeService*>& endpoints=AA->getAttributeServices();
127         for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
128             try {
129                 // Get a binding object for this protocol.
130                 const SAMLBinding* binding = app->getBinding((*ep)->getBinding());
131                 if (!binding) {
132                     continue;
133                 }
134                 response=binding->send((*ep)->getLocation(), *(req.get()), &ctx);
135             }
136             catch (exception&) {
137             }
138         }
139
140         if (!response)
141             throw opensaml::BindingException("unable to successfully query for attributes");
142
143         Iterator<SAMLAssertion*> i=response->getAssertions();
144         if (i.hasNext())
145         {
146             SAMLAssertion* a=i.next();
147             cout << "Issuer: "; xmlout(cout,a->getIssuer()); cout << endl;
148             const SAMLDateTime* exp=a->getNotOnOrAfter();
149             cout << "Expires: ";
150             if (exp)
151               xmlout(cout,exp->getRawData());
152             else
153                 cout << "None";
154             cout << endl;
155
156             Iterator<SAMLStatement*> j=a->getStatements();
157             if (j.hasNext())
158             {
159                 SAMLAttributeStatement* s=dynamic_cast<SAMLAttributeStatement*>(j.next());
160                 if (s)
161                 {
162                     const SAMLNameIdentifier* sub=s->getSubject()->getNameIdentifier();
163                     cout << "Format: "; xmlout(cout,sub->getFormat()); cout << endl;
164                     cout << "Domain: "; xmlout(cout,sub->getNameQualifier()); cout << endl;
165                     cout << "Handle: "; xmlout(cout,sub->getName()); cout << endl;
166
167                     Iterator<SAMLAttribute*> attrs=s->getAttributes();
168                     while (attrs.hasNext())
169                     {
170                         SAMLAttribute* attr=attrs.next();
171                         cout << "Attribute Name: "; xmlout(cout,attr->getName()); cout << endl;
172                         Iterator<const XMLCh*> vals=attr->getValues();
173                         while (vals.hasNext())
174                         {
175                             cout << "Attribute Value: ";
176                             xmlout(cout,vals.next());
177                             cout << endl;
178                         }
179                     }
180                 }
181             }
182         }
183     }
184     catch(exception& e)
185     {
186         cerr << "caught an exception: " << e.what() << endl;
187     }
188
189     conf.shutdown();
190     return 0;
191 }