Next integration phase, metadata and trust conversion.
[shibboleth/cpp-sp.git] / test / shibtest.cpp
1 /*
2  *  Copyright 2001-2005 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/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     try {
85         const IApplication* app=conf.getINI()->getApplication(a_param);
86         if (!app)
87             throw SAMLException("specified <Application> section not found in configuration");
88
89         auto_ptr_XMLCh domain(q_param);
90         auto_ptr_XMLCh handle(h_param);
91         auto_ptr_XMLCh format(f_param);
92         auto_ptr_XMLCh resource(app->getString("providerId").second);
93
94         auto_ptr<SAMLRequest> req(
95             new SAMLRequest(
96                 new SAMLAttributeQuery(
97                     new SAMLSubject(
98                         new SAMLNameIdentifier(
99                             handle.get(),
100                             domain.get(),
101                             format.get() ? format.get() : shibspconstants::SHIB1_NAMEID_FORMAT_URI
102                             )
103                         ),
104                     resource.get(),
105                     app->getAttributeDesignators().clone()
106                     )
107                 )
108             );
109
110         MetadataProvider* m=app->getMetadataProvider();
111         xmltooling::Locker locker(m);
112         const EntityDescriptor* site=m->getEntityDescriptor(domain.get());
113         if (!site)
114             throw MetadataException("Unable to locate specified origin site's metadata.");
115
116         // Try to locate an AA role.
117         const AttributeAuthorityDescriptor* AA=site->getAttributeAuthorityDescriptor(saml::XML::SAML11_PROTOCOL_ENUM);
118         if (!AA)
119             throw MetadataException("Unable to locate metadata for origin site's Attribute Authority.");
120
121         ShibHTTPHook::ShibHTTPHookCallContext ctx(app->getCredentialUse(site),AA);
122
123         SAMLResponse* response=NULL;
124         const vector<AttributeService*>& endpoints=AA->getAttributeServices();
125         for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
126             try {
127                 // Get a binding object for this protocol.
128                 const SAMLBinding* binding = app->getBinding((*ep)->getBinding());
129                 if (!binding) {
130                     continue;
131                 }
132                 response=binding->send((*ep)->getLocation(), *(req.get()), &ctx);
133             }
134             catch (exception&) {
135             }
136         }
137
138         if (!response)
139             throw opensaml::BindingException("unable to successfully query for attributes");
140
141         // Run it through the AAP. Note that we could end up with an empty response!
142         Iterator<SAMLAssertion*> a=response->getAssertions();
143         for (unsigned long c=0; c < a.size();) {
144             try {
145                 shibboleth::AAP::apply(app->getAAPProviders(),*(a[c]),AA);
146                 c++;
147             }
148             catch (SAMLException&) {
149                 response->removeAssertion(c);
150             }
151         }
152
153         Iterator<SAMLAssertion*> i=response->getAssertions();
154         if (i.hasNext())
155         {
156             SAMLAssertion* a=i.next();
157             cout << "Issuer: "; xmlout(cout,a->getIssuer()); cout << endl;
158             const SAMLDateTime* exp=a->getNotOnOrAfter();
159             cout << "Expires: ";
160             if (exp)
161               xmlout(cout,exp->getRawData());
162             else
163                 cout << "None";
164             cout << endl;
165
166             Iterator<SAMLStatement*> j=a->getStatements();
167             if (j.hasNext())
168             {
169                 SAMLAttributeStatement* s=dynamic_cast<SAMLAttributeStatement*>(j.next());
170                 if (s)
171                 {
172                     const SAMLNameIdentifier* sub=s->getSubject()->getNameIdentifier();
173                     cout << "Format: "; xmlout(cout,sub->getFormat()); cout << endl;
174                     cout << "Domain: "; xmlout(cout,sub->getNameQualifier()); cout << endl;
175                     cout << "Handle: "; xmlout(cout,sub->getName()); cout << endl;
176
177                     Iterator<SAMLAttribute*> attrs=s->getAttributes();
178                     while (attrs.hasNext())
179                     {
180                         SAMLAttribute* attr=attrs.next();
181                         cout << "Attribute Name: "; xmlout(cout,attr->getName()); cout << endl;
182                         Iterator<const XMLCh*> vals=attr->getValues();
183                         while (vals.hasNext())
184                         {
185                             cout << "Attribute Value: ";
186                             xmlout(cout,vals.next());
187                             cout << endl;
188                         }
189                     }
190                 }
191             }
192         }
193     }
194     catch(exception& e)
195     {
196         cerr << "caught an exception: " << e.what() << endl;
197     }
198
199     conf.shutdown();
200     return 0;
201 }