2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright 2001-2009 Internet2
35 * Licensed under the Apache License, Version 2.0 (the "License");
36 * you may not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
39 * http://www.apache.org/licenses/LICENSE-2.0
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS,
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
49 * Local attribute provider implementation.
52 #include <shibsp/exceptions.h>
53 #include <shibsp/attribute/SimpleAttribute.h>
55 #include <shibresolver/resolver.h>
59 #include "gssapiP_eap.h"
61 using namespace shibsp;
62 using namespace shibresolver;
63 using namespace opensaml::saml2md;
64 using namespace opensaml;
65 using namespace xmltooling;
68 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
70 m_authenticated = false;
73 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
75 for_each(m_attributes.begin(),
77 xmltooling::cleanup<Attribute>())
82 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
83 const gss_eap_attr_provider *ctx)
85 const gss_eap_shib_attr_provider *shib;
87 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
91 m_authenticated = false;
93 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
95 m_attributes = duplicateAttributes(shib->getAttributes());
96 m_authenticated = shib->authenticated();
103 addRadiusAttribute(const gss_eap_attr_provider *provider,
104 const gss_buffer_t attribute,
107 const gss_eap_shib_attr_provider *shib;
108 const gss_eap_radius_attr_provider *radius;
109 int authenticated, complete, more = -1;
110 vector <string> attributeIds(1);
113 radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
114 shib = static_cast<const gss_eap_shib_attr_provider *>(data);
116 assert(radius != NULL && shib != NULL);
118 string attributeName =
119 gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
121 attributeIds.push_back(attributeName);
122 a = new SimpleAttribute(attributeIds);
127 gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
130 if (!radius->getAttribute(attribute,
138 string attributeValue((char *)value.value, value.length);
139 a->getValues().push_back(attributeValue);
141 gss_release_buffer(&minor, &value);
144 shib->getAttributes().push_back(a);
150 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
151 const gss_cred_id_t gssCred,
152 const gss_ctx_id_t gssCtx)
154 const gss_eap_saml_assertion_provider *saml;
155 const gss_eap_radius_attr_provider *radius;
156 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
157 ShibbolethResolver *resolver;
160 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
163 saml = static_cast<const gss_eap_saml_assertion_provider *>
164 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
165 radius = static_cast<const gss_eap_radius_attr_provider *>
166 (m_manager->getProvider(ATTR_TYPE_RADIUS));
168 resolver = ShibbolethResolver::create();
170 if (gssCred != GSS_C_NO_CREDENTIAL &&
171 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
172 resolver->setApplicationID((const char *)nameBuf.value);
174 m_authenticated = false;
176 if (radius != NULL) {
177 radius->getAttributeTypes(addRadiusAttribute, (void *)this);
178 m_authenticated = radius->authenticated();
181 if (saml != NULL && saml->getAssertion() != NULL) {
182 resolver->addToken(saml->getAssertion());
184 m_authenticated = saml->authenticated();
189 m_attributes = resolver->getResolvedAttributes();
190 resolver->getResolvedAttributes().clear();
192 gss_release_buffer(&minor, &nameBuf);
200 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
204 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
205 a != m_attributes.end();
208 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
209 s != (*a)->getAliases().end();
211 if (attr->length == (*s).length() &&
212 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
222 gss_eap_shib_attr_provider::setAttribute(int complete,
223 const gss_buffer_t attr,
224 const gss_buffer_t value)
226 string attrStr((char *)attr->value, attr->length);
227 vector <string> ids(1, attrStr);
228 SimpleAttribute *a = new SimpleAttribute(ids);
230 if (value->length != 0) {
231 string valueStr((char *)value->value, value->length);
233 a->getValues().push_back(valueStr);
236 m_attributes.push_back(a);
237 m_authenticated = false;
243 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
247 i = getAttributeIndex(attr);
249 m_attributes.erase(m_attributes.begin() + i);
251 m_authenticated = false;
257 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
260 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
261 a != m_attributes.end();
264 gss_buffer_desc attribute;
266 attribute.value = (void *)((*a)->getId());
267 attribute.length = strlen((char *)attribute.value);
269 if (!addAttribute(this, &attribute, data))
277 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
279 const Attribute *ret = NULL;
281 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
282 a != m_attributes.end();
285 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
286 s != (*a)->getAliases().end();
288 if (attr->length == (*s).length() &&
289 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
302 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
306 gss_buffer_t display_value,
309 const Attribute *shibAttr = NULL;
311 int nvalues, i = *more;
315 shibAttr = getAttribute(attr);
316 if (shibAttr == NULL)
319 nvalues = shibAttr->valueCount();
323 else if (i >= nvalues)
326 buf.value = (void *)shibAttr->getString(*more);
327 buf.length = strlen((char *)buf.value);
329 if (buf.length != 0) {
331 duplicateBuffer(buf, value);
333 if (display_value != NULL)
334 duplicateBuffer(buf, display_value);
337 if (authenticated != NULL)
338 *authenticated = m_authenticated;
339 if (complete != NULL)
349 gss_eap_shib_attr_provider::mapToAny(int authenticated,
350 gss_buffer_t type_id) const
354 if (authenticated && !m_authenticated)
355 return (gss_any_t)NULL;
357 vector <Attribute *>v = duplicateAttributes(m_attributes);
359 output = (gss_any_t)new vector <Attribute *>(v);
365 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
366 gss_any_t input) const
368 vector <Attribute *> *v = ((vector <Attribute *> *)input);
373 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
379 buffer->value = NULL;
381 obj.addmember("version").integer(1);
382 obj.addmember("authenticated").integer(m_authenticated);
384 attrs = obj.addmember("attributes").list();
385 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
386 a != m_attributes.end(); ++a) {
387 DDF attr = (*a)->marshall();
393 string str = sink.str();
395 duplicateBuffer(str, buffer);
401 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
402 const gss_buffer_t buffer)
404 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
407 if (buffer->length == 0)
410 assert(m_authenticated == false);
411 assert(m_attributes.size() == 0);
414 string str((const char *)buffer->value, buffer->length);
415 istringstream source(str);
419 if (obj["version"].integer() != 1)
422 m_authenticated = (obj["authenticated"].integer() != 0);
424 DDF attrs = obj["attributes"];
425 DDF attr = attrs.first();
426 while (!attr.isnull()) {
427 Attribute *attribute = Attribute::unmarshall(attr);
428 m_attributes.push_back(attribute);
438 gss_eap_shib_attr_provider::init(void)
440 if (!ShibbolethResolver::init())
443 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
445 gss_eap_shib_attr_provider::createAttrContext);
451 gss_eap_shib_attr_provider::finalize(void)
453 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
454 ShibbolethResolver::term();
457 gss_eap_attr_provider *
458 gss_eap_shib_attr_provider::createAttrContext(void)
460 return new gss_eap_shib_attr_provider;
464 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
466 DDF obj = src->marshall();
467 Attribute *attribute = Attribute::unmarshall(obj);
474 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
476 vector <Attribute *> dst;
478 for (vector<Attribute *>::const_iterator a = src.begin();
481 dst.push_back(duplicateAttribute(*a));
487 gssEapLocalAttrProviderInit(OM_uint32 *minor)
489 if (!gss_eap_shib_attr_provider::init()) {
490 *minor = GSSEAP_SHIB_INIT_FAILURE;
491 return GSS_S_FAILURE;
493 return GSS_S_COMPLETE;
497 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
499 gss_eap_shib_attr_provider::finalize();
500 return GSS_S_COMPLETE;