use JSON instead of DDF marshalling
[mech_eap.git] / util_shib.cpp
index 8a26b1b..14757d1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, JANET(UK)
+ * Copyright (c) 2011, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -31,7 +31,7 @@
  */
 /*
  * Copyright 2001-2009 Internet2
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * limitations under the License.
  */
 
-#include "gssapiP_eap.h"
+/*
+ * Local attribute provider implementation.
+ */
+
+#include <xmltooling/XMLObject.h>
+
+#include <saml/saml2/core/Assertions.h>
 
-#include <shibsp/Application.h>
 #include <shibsp/exceptions.h>
-#include <shibsp/SPConfig.h>
-#include <shibsp/ServiceProvider.h>
-#include <shibsp/attribute/Attribute.h>
 #include <shibsp/attribute/SimpleAttribute.h>
-#include <shibsp/attribute/resolver/ResolutionContext.h>
-#include <shibsp/handler/AssertionConsumerService.h>
-#include <shibsp/metadata/MetadataProviderCriteria.h>
-#include <shibsp/util/SPConstants.h>
+#include <shibresolver/resolver.h>
 
-#include <saml/saml1/core/Assertions.h>
-#include <saml/saml2/core/Assertions.h>
-#include <saml/saml2/metadata/Metadata.h>
-#include <xercesc/util/XMLUniDefs.hpp>
-#include <xmltooling/XMLToolingConfig.h>
-#include <xmltooling/util/XMLHelper.h>
+#include <sstream>
 
-#include "resolver.h"
+#include "gssapiP_eap.h"
 
 using namespace shibsp;
 using namespace shibresolver;
 using namespace opensaml::saml2md;
 using namespace opensaml;
-using namespace xmltooling::logging;
 using namespace xmltooling;
-using namespace xercesc;
 using namespace std;
 
+gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
+{
+    m_authenticated = false;
+}
+
+gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
+{
+    for_each(m_attributes.begin(),
+             m_attributes.end(),
+             xmltooling::cleanup<Attribute>())
+        ;
+}
+
 bool
 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
                                                     const gss_eap_attr_provider *ctx)
@@ -98,12 +103,13 @@ gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *mana
 }
 
 bool
-addRadiusAttribute(const gss_eap_attr_provider *provider,
+addRadiusAttribute(const gss_eap_attr_ctx *manager,
+                   const gss_eap_attr_provider *provider,
                    const gss_buffer_t attribute,
                    void *data)
 {
-    const gss_eap_shib_attr_provider *shib;
     const gss_eap_radius_attr_provider *radius;
+    const gss_eap_shib_attr_provider *shib;
     int authenticated, complete, more = -1;
     vector <string> attributeIds(1);
     SimpleAttribute *a;
@@ -114,7 +120,7 @@ addRadiusAttribute(const gss_eap_attr_provider *provider,
     assert(radius != NULL && shib != NULL);
 
     string attributeName =
-        gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
+        manager->composeAttributeName(ATTR_TYPE_RADIUS, attribute);
 
     attributeIds.push_back(attributeName);
     a = new SimpleAttribute(attributeIds);
@@ -151,21 +157,33 @@ gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
 {
     const gss_eap_saml_assertion_provider *saml;
     const gss_eap_radius_attr_provider *radius;
+#if 0
     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
-    ShibbolethResolver *resolver = NULL;
     OM_uint32 minor;
-
+#endif
     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
         return false;
 
     saml = static_cast<const gss_eap_saml_assertion_provider *>
-        (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
+        (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
     radius = static_cast<const gss_eap_radius_attr_provider *>
-        (manager->getProvider(ATTR_TYPE_RADIUS));
+        (m_manager->getProvider(ATTR_TYPE_RADIUS));
+
+    auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
 
+    /*
+     * For now, leave ApplicationID defaulted.
+     * Later on, we could allow this via config option to the mechanism
+     * or rely on an SPRequest interface to pass in a URI identifying the
+     * acceptor.
+     */
+#if 0
     if (gssCred != GSS_C_NO_CREDENTIAL &&
-        gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
+        gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
         resolver->setApplicationID((const char *)nameBuf.value);
+        gss_release_buffer(&minor, &nameBuf);
+    }
+#endif
 
     m_authenticated = false;
 
@@ -180,24 +198,20 @@ gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
             m_authenticated = saml->authenticated();
     }
 
-    resolver->resolveAttributes(m_attributes);
-
-    gss_release_buffer(&minor, &nameBuf);
-
-    delete resolver;
+    try {
+        resolver->resolve();
+        m_attributes = resolver->getResolvedAttributes();
+        resolver->getResolvedAttributes().clear();
+    } catch (exception &e) {
+#if 0
+        fprintf(stderr, "%s", e.what());
+#endif
+    }
 
     return true;
 }
 
-gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
-{
-    for_each(m_attributes.begin(),
-             m_attributes.end(),
-             xmltooling::cleanup<Attribute>())
-        ;
-}
-
-int
+ssize_t
 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
 {
     int i = 0;
@@ -219,29 +233,28 @@ gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
     return -1;
 }
 
-void
-gss_eap_shib_attr_provider::setAttribute(int complete,
+bool
+gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
                                          const gss_buffer_t attr,
                                          const gss_buffer_t value)
 {
     string attrStr((char *)attr->value, attr->length);
-    vector <string> ids(1);
-
-    ids.push_back(attrStr);
-
+    vector <string> ids(1, attrStr);
     SimpleAttribute *a = new SimpleAttribute(ids);
 
     if (value->length != 0) {
         string valueStr((char *)value->value, value->length);
 
-        a->getValues().push_back(valueStr);        
+        a->getValues().push_back(valueStr);
     }
 
     m_attributes.push_back(a);
     m_authenticated = false;
+
+    return true;
 }
 
-void
+bool
 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
 {
     int i;
@@ -251,6 +264,8 @@ gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
         m_attributes.erase(m_attributes.begin() + i);
 
     m_authenticated = false;
+
+    return true;
 }
 
 bool
@@ -266,7 +281,7 @@ gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
         attribute.value = (void *)((*a)->getId());
         attribute.length = strlen((char *)attribute.value);
 
-        if (!addAttribute(this, &attribute, data))
+        if (!addAttribute(m_manager, this, &attribute, data))
             return false;
     }
 
@@ -323,7 +338,7 @@ gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
     else if (i >= nvalues)
         return false;
 
-    buf.value = (void *)shibAttr->getString(*more);
+    buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
     buf.length = strlen((char *)buf.value);
 
     if (buf.length != 0) {
@@ -334,7 +349,7 @@ gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
             duplicateBuffer(buf, display_value);
     }
 
-    if (authenticated != NULL) 
+    if (authenticated != NULL)
         *authenticated = m_authenticated;
     if (complete != NULL)
         *complete = false;
@@ -347,10 +362,13 @@ gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
 
 gss_any_t
 gss_eap_shib_attr_provider::mapToAny(int authenticated,
-                                     gss_buffer_t type_id) const
+                                     gss_buffer_t type_id GSSEAP_UNUSED) const
 {
     gss_any_t output;
 
+    if (authenticated && !m_authenticated)
+        return (gss_any_t)NULL;
+
     vector <Attribute *>v = duplicateAttributes(m_attributes);
 
     output = (gss_any_t)new vector <Attribute *>(v);
@@ -359,75 +377,179 @@ gss_eap_shib_attr_provider::mapToAny(int authenticated,
 }
 
 void
-gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
+gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
                                                   gss_any_t input) const
 {
     vector <Attribute *> *v = ((vector <Attribute *> *)input);
     delete v;
 }
 
-void
-gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
+const char *
+gss_eap_shib_attr_provider::prefix(void) const
+{
+    return NULL;
+}
+
+const char *
+gss_eap_shib_attr_provider::name(void) const
+{
+    return "local";
+}
+
+static json_t *
+ddfToJson(DDF &ddf)
+{
+    json_t *json;
+
+    if (ddf.isstruct()) {
+        DDF elem = ddf.first();
+        json = json_array();
+        if (json == NULL)
+            throw new std::bad_alloc;
+
+        while (!elem.isnull()) {
+            if (json_array_append_new(json, ddfToJson(elem)) != 0) {
+                json_decref(json);
+                throw new std::bad_alloc;
+            }
+
+            elem = ddf.next();
+        }
+    } else if (ddf.islist()) {
+        DDF elem = ddf.first();
+        json = json_object();
+        if (json == NULL)
+            throw new std::bad_alloc;
+
+        while (!elem.isnull()) {
+            if (json_object_set(json, elem.name(), ddfToJson(elem)) != 0) {
+                json_decref(json);
+                throw new std::bad_alloc;
+            }
+
+            elem = ddf.next();
+        }
+    } else if (ddf.isstring()) {
+        json = json_string(ddf.string());
+    } else if (ddf.isint()) {
+        json = json_integer(ddf.integer());
+    } else if (ddf.isfloat()) {
+        json = json_real(ddf.floating());
+    } else if (ddf.isempty() || ddf.ispointer()) {
+        json = json_object();
+    } else if (ddf.isnull()) {
+        json = json_null();
+    } else {
+        assert(0 && "Invalid DDF object");
+    }
+
+    if (json == NULL)
+        throw new std::bad_alloc;
+
+    return json;
+}
+
+static DDF
+jsonToDdf(json_t *json)
 {
-    DDF obj(NULL);
-    DDF attrs(NULL);
+    DDF ddf(NULL);
 
-    buffer->length = 0;
-    buffer->value = NULL;
+    switch (json_typeof(json)) {
+    case JSON_OBJECT: {
+        void *iter = json_object_iter(json);
 
-    obj.addmember("version").integer(1);
-    obj.addmember("authenticated").integer(m_authenticated);
+        while (iter != NULL) {
+            const char *key = json_object_iter_key(iter);
+            json_t *value = json_object_iter_value(iter);
+            ddf.add(jsonToDdf(value).name(key));
+            iter = json_object_iter_next(json, iter);
+        }
+        break;
+    }
+    case JSON_ARRAY: {
+        size_t i;
+
+        for (i = 0; i < json_array_size(json); i++) {
+            DDF value = jsonToDdf(json_array_get(json, i));
+            ddf.add(value);
+        }
+        break;
+    }
+    case JSON_STRING:
+        ddf.string(json_string_value(json));
+        break;
+    case JSON_INTEGER:
+        ddf.integer(json_integer_value(json));
+        break;
+    case JSON_REAL:
+        ddf.floating(json_real_value(json));
+        break;
+    case JSON_TRUE:
+        ddf.integer(1L);
+        break;
+    case JSON_FALSE:
+        ddf.integer(0L);
+        break;
+    case JSON_NULL:
+        break;
+    }
+
+    return ddf;
+}
+
+json_t *
+gss_eap_shib_attr_provider::jsonRepresentation(void) const
+{
+    json_t *obj, *attrs;
+
+    obj = json_object();
+    if (obj == NULL)
+        throw new std::bad_alloc;
+
+    /* FIXME check json_object_set_new return value */
+    json_object_set_new(obj, "authenticated", json_integer(m_authenticated));
+
+    attrs = json_array();
+    if (attrs == NULL) {
+        json_decref(obj);
+        throw new std::bad_alloc;
+    }
 
-    attrs = obj.addmember("attributes").list();
     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
          a != m_attributes.end(); ++a) {
         DDF attr = (*a)->marshall();
-        attrs.add(attr);
+        /* FIXME check json_array_append_new return value */
+        json_array_append_new(attrs, ddfToJson(attr));
     }
 
-    ostringstream sink;
-    sink << attrs;
-    string str = sink.str();
+    json_object_set_new(obj, "attributes", attrs);
 
-    duplicateBuffer(str, buffer);
-
-    attrs.destroy();
+    return obj;
 }
 
 bool
-gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
-                                           const gss_buffer_t buffer)
+gss_eap_shib_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
+                                              json_t *obj)
 {
-    if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
-        return false;
+    size_t i;
+    json_t *attrs;
 
-    if (buffer->length == 0)
-        return true;
+    if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
+        return false;
 
     assert(m_authenticated == false);
     assert(m_attributes.size() == 0);
 
-    DDF obj(NULL);
-    string str((const char *)buffer->value, buffer->length);
-    istringstream source(str);
+    m_authenticated = json_integer_value(json_object_get(obj, "authenticated"));
 
-    source >> obj;
+    attrs = json_object_get(obj, "attributes");
 
-    if (obj["version"].integer() != 1)
-        return false;
-
-    m_authenticated = (obj["authenticated"].integer() != 0);
-
-    DDF attrs = obj["attributes"];
-    DDF attr = attrs.first();
-    while (!attr.isnull()) {
+    for (i = 0; i < json_array_size(attrs); i++) {
+        DDF attr = jsonToDdf(json_array_get(attrs, i));
         Attribute *attribute = Attribute::unmarshall(attr);
         m_attributes.push_back(attribute);
-        attr = attrs.next();
     }
 
-    attrs.destroy();
-
     return true;
 }
 
@@ -437,9 +559,7 @@ gss_eap_shib_attr_provider::init(void)
     if (!ShibbolethResolver::init())
         return false;
 
-    gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
-                                       NULL,
-                                       gss_eap_shib_attr_provider::createAttrContext);
+    gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, createAttrContext);
 
     return true;
 }
@@ -447,8 +567,30 @@ gss_eap_shib_attr_provider::init(void)
 void
 gss_eap_shib_attr_provider::finalize(void)
 {
-    ShibbolethResolver::term();
     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
+    ShibbolethResolver::term();
+}
+
+OM_uint32
+gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
+                                         std::exception &e) const
+{
+    if (typeid(e) == typeid(AttributeException))
+        *minor = GSSEAP_SHIB_ATTR_FAILURE;
+    else if (typeid(e) == typeid(AttributeExtractionException))
+        *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
+    else if (typeid(e) == typeid(AttributeFilteringException))
+        *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
+    else if (typeid(e) == typeid(AttributeResolutionException))
+        *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
+    else if (typeid(e) == typeid(ConfigurationException))
+        *minor = GSSEAP_SHIB_CONFIG_FAILURE;
+    else if (typeid(e) == typeid(ListenerException))
+        *minor = GSSEAP_SHIB_LISTENER_FAILURE;
+    else
+        return GSS_S_CONTINUE_NEEDED;
+
+    return GSS_S_FAILURE;
 }
 
 gss_eap_attr_provider *
@@ -460,10 +602,8 @@ gss_eap_shib_attr_provider::createAttrContext(void)
 Attribute *
 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
 {
-    Attribute *attribute;
-
     DDF obj = src->marshall();
-    attribute = Attribute::unmarshall(obj);
+    Attribute *attribute = Attribute::unmarshall(obj);
     obj.destroy();
 
     return attribute;
@@ -481,3 +621,22 @@ gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
 
     return dst;
 }
+
+OM_uint32
+gssEapLocalAttrProviderInit(OM_uint32 *minor)
+{
+    if (!gss_eap_shib_attr_provider::init()) {
+        *minor = GSSEAP_SHIB_INIT_FAILURE;
+        return GSS_S_FAILURE;
+    }
+    return GSS_S_COMPLETE;
+}
+
+OM_uint32
+gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
+{
+    gss_eap_shib_attr_provider::finalize();
+
+    *minor = 0;
+    return GSS_S_COMPLETE;
+}