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_initialized = false;
74 m_authenticated = false;
77 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
79 for_each(m_attributes.begin(),
81 xmltooling::cleanup<Attribute>())
86 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
87 const gss_eap_attr_provider *ctx)
89 const gss_eap_shib_attr_provider *shib;
91 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
95 m_authenticated = false;
97 shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
99 m_attributes = duplicateAttributes(shib->getAttributes());
100 m_authenticated = shib->authenticated();
103 m_initialized = true;
109 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
110 const gss_cred_id_t gssCred,
111 const gss_ctx_id_t gssCtx)
113 const gss_eap_saml_assertion_provider *saml;
114 gss_buffer_desc exportedCtx = GSS_C_EMPTY_BUFFER;
115 OM_uint32 major, minor;
117 gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
120 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
123 saml = static_cast<const gss_eap_saml_assertion_provider *>
124 (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
126 auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
129 * For now, leave ApplicationID defaulted.
130 * Later on, we could allow this via config option to the mechanism
131 * or rely on an SPRequest interface to pass in a URI identifying the
135 if (gssCred != GSS_C_NO_CREDENTIAL &&
136 gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
137 resolver->setApplicationID((const char *)nameBuf.value);
138 gss_release_buffer(&minor, &nameBuf);
142 major = gssEapExportSecContext(&minor, gssCtx, &exportedCtx);
143 if (major == GSS_S_COMPLETE) {
144 resolver->addToken(&exportedCtx);
145 gss_release_buffer(&minor, &exportedCtx);
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) {
160 m_initialized = true;
166 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
170 assert(m_initialized);
172 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
173 a != m_attributes.end();
176 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
177 s != (*a)->getAliases().end();
179 if (attr->length == (*s).length() &&
180 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
190 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
191 const gss_buffer_t attr,
192 const gss_buffer_t value)
194 string attrStr((char *)attr->value, attr->length);
195 vector <string> ids(1, attrStr);
196 SimpleAttribute *a = new SimpleAttribute(ids);
198 assert(m_initialized);
200 if (value->length != 0) {
201 string valueStr((char *)value->value, value->length);
203 a->getValues().push_back(valueStr);
206 m_attributes.push_back(a);
207 m_authenticated = false;
213 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
217 assert(m_initialized);
219 i = getAttributeIndex(attr);
221 m_attributes.erase(m_attributes.begin() + i);
223 m_authenticated = false;
229 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
232 assert(m_initialized);
234 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
235 a != m_attributes.end();
238 gss_buffer_desc attribute;
240 attribute.value = (void *)((*a)->getId());
241 attribute.length = strlen((char *)attribute.value);
243 if (!addAttribute(m_manager, this, &attribute, data))
251 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
253 const Attribute *ret = NULL;
255 assert(m_initialized);
257 for (vector<Attribute *>::const_iterator a = m_attributes.begin();
258 a != m_attributes.end();
261 for (vector<string>::const_iterator s = (*a)->getAliases().begin();
262 s != (*a)->getAliases().end();
264 if (attr->length == (*s).length() &&
265 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
278 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
282 gss_buffer_t display_value,
285 const Attribute *shibAttr = NULL;
287 int nvalues, i = *more;
289 assert(m_initialized);
293 shibAttr = getAttribute(attr);
294 if (shibAttr == NULL)
297 nvalues = shibAttr->valueCount();
301 else if (i >= nvalues)
304 buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
305 buf.length = strlen((char *)buf.value);
307 if (buf.length != 0) {
309 duplicateBuffer(buf, value);
311 if (display_value != NULL)
312 duplicateBuffer(buf, display_value);
315 if (authenticated != NULL)
316 *authenticated = m_authenticated;
317 if (complete != NULL)
327 gss_eap_shib_attr_provider::mapToAny(int authenticated,
328 gss_buffer_t type_id GSSEAP_UNUSED) const
332 assert(m_initialized);
334 if (authenticated && !m_authenticated)
335 return (gss_any_t)NULL;
337 vector <Attribute *>v = duplicateAttributes(m_attributes);
339 output = (gss_any_t)new vector <Attribute *>(v);
345 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
346 gss_any_t input) const
348 assert(m_initialized);
350 vector <Attribute *> *v = ((vector <Attribute *> *)input);
355 gss_eap_shib_attr_provider::prefix(void) const
361 gss_eap_shib_attr_provider::name(void) const
367 gss_eap_shib_attr_provider::jsonRepresentation(void) const
371 if (m_initialized == false)
372 return obj; /* don't export incomplete context */
374 JSONObject attrs = JSONObject::array();
376 for (vector<Attribute*>::const_iterator a = m_attributes.begin();
377 a != m_attributes.end(); ++a) {
378 DDF attr = (*a)->marshall();
379 JSONObject jobj(attr);
383 obj.set("attributes", attrs);
385 obj.set("authenticated", m_authenticated);
391 gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
394 if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
397 assert(m_authenticated == false);
398 assert(m_attributes.size() == 0);
400 JSONObject attrs = obj["attributes"];
401 size_t nelems = attrs.size();
403 for (size_t i = 0; i < nelems; i++) {
404 DDF attr = attrs.get(i).ddf();
405 Attribute *attribute = Attribute::unmarshall(attr);
406 m_attributes.push_back(attribute);
409 m_authenticated = obj["authenticated"].integer();
410 m_initialized = true;
416 gss_eap_shib_attr_provider::init(void)
418 if (!ShibbolethResolver::init())
421 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
427 gss_eap_shib_attr_provider::finalize(void)
429 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
430 ShibbolethResolver::term();
434 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
435 std::exception &e) const
437 if (typeid(e) == typeid(AttributeException))
438 *minor = GSSEAP_SHIB_ATTR_FAILURE;
439 else if (typeid(e) == typeid(AttributeExtractionException))
440 *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
441 else if (typeid(e) == typeid(AttributeFilteringException))
442 *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
443 else if (typeid(e) == typeid(AttributeResolutionException))
444 *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
445 else if (typeid(e) == typeid(ConfigurationException))
446 *minor = GSSEAP_SHIB_CONFIG_FAILURE;
447 else if (typeid(e) == typeid(ListenerException))
448 *minor = GSSEAP_SHIB_LISTENER_FAILURE;
450 return GSS_S_CONTINUE_NEEDED;
452 return GSS_S_FAILURE;
455 gss_eap_attr_provider *
456 gss_eap_shib_attr_provider::createAttrContext(void)
458 return new gss_eap_shib_attr_provider;
462 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
464 DDF obj = src->marshall();
465 Attribute *attribute = Attribute::unmarshall(obj);
472 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
474 vector <Attribute *> dst;
476 for (vector<Attribute *>::const_iterator a = src.begin();
479 dst.push_back(duplicateAttribute(*a));
485 gssEapLocalAttrProviderInit(OM_uint32 *minor)
487 if (!gss_eap_shib_attr_provider::init()) {
488 *minor = GSSEAP_SHIB_INIT_FAILURE;
489 return GSS_S_FAILURE;
491 return GSS_S_COMPLETE;
495 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
497 gss_eap_shib_attr_provider::finalize();
500 return GSS_S_COMPLETE;