2 * Copyright (c) 2011, 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 <xmltooling/XMLObject.h>
54 #include <saml/saml2/core/Assertions.h>
56 #include <shibsp/exceptions.h>
57 #include <shibsp/attribute/SimpleAttribute.h>
58 #include <shibresolver/resolver.h>
62 #include "gssapiP_eap.h"
64 using namespace shibsp;
65 using namespace shibresolver;
66 using namespace opensaml::saml2md;
67 using namespace opensaml;
68 using namespace xmltooling;
71 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
73 m_authenticated = false;
76 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
78 for_each(m_attributes.begin(),
80 xmltooling::cleanup<Attribute>())
85 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
86 const gss_eap_attr_provider *ctx)
88 const gss_eap_shib_attr_provider *shib;
90 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
94 m_authenticated = false;
96 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
98 m_attributes = duplicateAttributes(shib->getAttributes());
99 m_authenticated = shib->authenticated();
106 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
107 const gss_cred_id_t gssCred,
108 const gss_ctx_id_t gssCtx)
110 const gss_eap_saml_assertion_provider *saml;
111 gss_buffer_desc exportedCtx = GSS_C_EMPTY_BUFFER;
112 OM_uint32 major, minor;
115 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
117 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
120 saml = static_cast<const gss_eap_saml_assertion_provider *>
121 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
123 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
126 * For now, leave ApplicationID defaulted.
127 * Later on, we could allow this via config option to the mechanism
128 * or rely on an SPRequest interface to pass in a URI identifying the
132 if (gssCred != GSS_C_NO_CREDENTIAL &&
133 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
134 resolver->setApplicationID((const char *)nameBuf.value);
135 gss_release_buffer(&minor, &nameBuf);
139 major = gssEapExportSecContext(&minor, gssCtx, &exportedCtx,
140 EXPORT_CTX_FLAG_DISABLE_LOCAL_ATTRS);
141 if (major == GSS_S_COMPLETE) {
142 resolver->addToken(&exportedCtx);
143 gss_release_buffer(&minor, &exportedCtx);
146 m_authenticated = true;
148 if (saml != NULL && saml->getAssertion() != NULL) {
149 resolver->addToken(saml->getAssertion());
150 m_authenticated = saml->authenticated();
155 m_attributes = resolver->getResolvedAttributes();
156 resolver->getResolvedAttributes().clear();
157 } catch (exception &e) {
159 fprintf(stderr, "%s", e.what());
167 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
171 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
172 a != m_attributes.end();
175 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
176 s != (*a)->getAliases().end();
178 if (attr->length == (*s).length() &&
179 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
189 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
190 const gss_buffer_t attr,
191 const gss_buffer_t value)
193 string attrStr((char *)attr->value, attr->length);
194 vector <string> ids(1, attrStr);
195 SimpleAttribute *a = new SimpleAttribute(ids);
197 if (value->length != 0) {
198 string valueStr((char *)value->value, value->length);
200 a->getValues().push_back(valueStr);
203 m_attributes.push_back(a);
204 m_authenticated = false;
210 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
214 i = getAttributeIndex(attr);
216 m_attributes.erase(m_attributes.begin() + i);
218 m_authenticated = false;
224 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
227 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
228 a != m_attributes.end();
231 gss_buffer_desc attribute;
233 attribute.value = (void *)((*a)->getId());
234 attribute.length = strlen((char *)attribute.value);
236 if (!addAttribute(m_manager, this, &attribute, data))
244 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
246 const Attribute *ret = NULL;
248 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
249 a != m_attributes.end();
252 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
253 s != (*a)->getAliases().end();
255 if (attr->length == (*s).length() &&
256 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
269 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
273 gss_buffer_t display_value,
276 const Attribute *shibAttr = NULL;
278 int nvalues, i = *more;
282 shibAttr = getAttribute(attr);
283 if (shibAttr == NULL)
286 nvalues = shibAttr->valueCount();
290 else if (i >= nvalues)
293 buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
294 buf.length = strlen((char *)buf.value);
296 if (buf.length != 0) {
298 duplicateBuffer(buf, value);
300 if (display_value != NULL)
301 duplicateBuffer(buf, display_value);
304 if (authenticated != NULL)
305 *authenticated = m_authenticated;
306 if (complete != NULL)
316 gss_eap_shib_attr_provider::mapToAny(int authenticated,
317 gss_buffer_t type_id GSSEAP_UNUSED) const
321 if (authenticated && !m_authenticated)
322 return (gss_any_t)NULL;
324 vector <Attribute *>v = duplicateAttributes(m_attributes);
326 output = (gss_any_t)new vector <Attribute *>(v);
332 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
333 gss_any_t input) const
335 vector <Attribute *> *v = ((vector <Attribute *> *)input);
340 gss_eap_shib_attr_provider::prefix(void) const
346 gss_eap_shib_attr_provider::name(void) const
352 gss_eap_shib_attr_provider::jsonRepresentation(void) const
356 obj.set("authenticated", m_authenticated);
358 JSONObject attrs = JSONObject::array();
360 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
361 a != m_attributes.end(); ++a) {
362 DDF attr = (*a)->marshall();
363 JSONObject jobj(attr);
367 obj.set("attributes", attrs);
373 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
376 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
379 assert(m_authenticated == false);
380 assert(m_attributes.size() == 0);
382 m_authenticated = obj["authenticated"].integer();
384 JSONObject attrs = obj["attributes"];
385 size_t nelems = attrs.size();
387 for (size_t i = 0; i < nelems; i++) {
388 DDF attr = attrs.get(i).ddf();
389 Attribute *attribute = Attribute::unmarshall(attr);
390 m_attributes.push_back(attribute);
397 gss_eap_shib_attr_provider::init(void)
399 if (!ShibbolethResolver::init())
402 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
408 gss_eap_shib_attr_provider::finalize(void)
410 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
411 ShibbolethResolver::term();
415 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
416 std::exception &e) const
418 if (typeid(e) == typeid(AttributeException))
419 *minor = GSSEAP_SHIB_ATTR_FAILURE;
420 else if (typeid(e) == typeid(AttributeExtractionException))
421 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
422 else if (typeid(e) == typeid(AttributeFilteringException))
423 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
424 else if (typeid(e) == typeid(AttributeResolutionException))
425 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
426 else if (typeid(e) == typeid(ConfigurationException))
427 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
428 else if (typeid(e) == typeid(ListenerException))
429 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
431 return GSS_S_CONTINUE_NEEDED;
433 return GSS_S_FAILURE;
436 gss_eap_attr_provider *
437 gss_eap_shib_attr_provider::createAttrContext(void)
439 return new gss_eap_shib_attr_provider;
443 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
445 DDF obj = src->marshall();
446 Attribute *attribute = Attribute::unmarshall(obj);
453 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
455 vector <Attribute *> dst;
457 for (vector<Attribute *>::const_iterator a = src.begin();
460 dst.push_back(duplicateAttribute(*a));
466 gssEapLocalAttrProviderInit(OM_uint32 *minor)
468 if (!gss_eap_shib_attr_provider::init()) {
469 *minor = GSSEAP_SHIB_INIT_FAILURE;
470 return GSS_S_FAILURE;
472 return GSS_S_COMPLETE;
476 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
478 gss_eap_shib_attr_provider::finalize();
481 return GSS_S_COMPLETE;