cleanup unused parameter warnings
[mech_eap.git] / util_saml.cpp
index 2017f3e..5346cc4 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
  * SUCH DAMAGE.
  */
 
+/*
+ * SAML attribute provider implementation.
+ */
+
 #include "gssapiP_eap.h"
 
 #include <sstream>
 #include <xmltooling/util/ParserPool.h>
 #include <xmltooling/util/DateTime.h>
 
+#include <saml/exceptions.h>
 #include <saml/saml1/core/Assertions.h>
 #include <saml/saml2/core/Assertions.h>
 #include <saml/saml2/metadata/Metadata.h>
+#include <saml/saml2/metadata/MetadataProvider.h>
 
 using namespace xmltooling;
 using namespace opensaml::saml2md;
@@ -155,17 +161,21 @@ gss_eap_saml_assertion_provider::parseAssertion(const gss_buffer_t buffer)
     DOMDocument *doc;
     const XMLObjectBuilder *b;
 
-    doc = XMLToolingConfig::getConfig().getParser().parse(istream);
-    if (doc == NULL)
-        return NULL;
+    try {
+        doc = XMLToolingConfig::getConfig().getParser().parse(istream);
+        if (doc == NULL)
+            return NULL;
 
-    b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
+        b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
 
 #ifdef __APPLE__
-    return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
+        return (saml2::Assertion *)((void *)b->buildFromDocument(doc));
 #else
-    return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
+        return dynamic_cast<saml2::Assertion *>(b->buildFromDocument(doc));
 #endif
+    } catch (exception &e) {
+        return NULL;
+    }
 }
 
 bool
@@ -184,7 +194,7 @@ gss_eap_saml_assertion_provider::getAttributeTypes(gss_eap_attr_enumeration_cb a
 }
 
 bool
-gss_eap_saml_assertion_provider::setAttribute(int complete,
+gss_eap_saml_assertion_provider::setAttribute(int complete GSSEAP_UNUSED,
                                               const gss_buffer_t attr,
                                               const gss_buffer_t value)
 {
@@ -197,7 +207,7 @@ gss_eap_saml_assertion_provider::setAttribute(int complete,
 }
 
 bool
-gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value)
+gss_eap_saml_assertion_provider::deleteAttribute(const gss_buffer_t value GSSEAP_UNUSED)
 {
     delete m_assertion;
     m_assertion = NULL;
@@ -223,12 +233,34 @@ gss_eap_saml_assertion_provider::getExpiryTime(void) const
     return expiryTime;
 }
 
+OM_uint32
+gss_eap_saml_assertion_provider::mapException(OM_uint32 *minor,
+                                              std::exception &e) const
+{
+    if (typeid(e) == typeid(SecurityPolicyException))
+        *minor = GSSEAP_SAML_SEC_POLICY_FAILURE;
+    else if (typeid(e) == typeid(BindingException))
+        *minor = GSSEAP_SAML_BINDING_FAILURE;
+    else if (typeid(e) == typeid(ProfileException))
+        *minor = GSSEAP_SAML_PROFILE_FAILURE;
+    else if (typeid(e) == typeid(FatalProfileException))
+        *minor = GSSEAP_SAML_FATAL_PROFILE_FAILURE;
+    else if (typeid(e) == typeid(RetryableProfileException))
+        *minor = GSSEAP_SAML_RETRY_PROFILE_FAILURE;
+    else if (typeid(e) == typeid(MetadataException))
+        *minor = GSSEAP_SAML_METADATA_FAILURE;
+    else
+        return GSS_S_CONTINUE_NEEDED;
+
+    return GSS_S_FAILURE;
+}
+
 bool
 gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
                                               int *authenticated,
                                               int *complete,
                                               gss_buffer_t value,
-                                              gss_buffer_t display_value,
+                                              gss_buffer_t display_value GSSEAP_UNUSED,
                                               int *more) const
 {
     string str;
@@ -257,7 +289,7 @@ gss_eap_saml_assertion_provider::getAttribute(const gss_buffer_t attr,
 
 gss_any_t
 gss_eap_saml_assertion_provider::mapToAny(int authenticated,
-                                          gss_buffer_t type_id) const
+                                          gss_buffer_t type_id GSSEAP_UNUSED) const
 {
     if (authenticated && !m_authenticated)
         return (gss_any_t)NULL;
@@ -266,7 +298,7 @@ gss_eap_saml_assertion_provider::mapToAny(int authenticated,
 }
 
 void
-gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id,
+gss_eap_saml_assertion_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
                                                        gss_any_t input) const
 {
     delete ((saml2::Assertion *)input);
@@ -313,7 +345,7 @@ gss_eap_saml_assertion_provider::init(void)
 {
     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML_ASSERTION,
                                        "urn:ietf:params:gss-eap:saml-aaa-assertion",
-                                       gss_eap_saml_assertion_provider::createAttrContext);
+                                       createAttrContext);
     return true;
 }
 
@@ -329,14 +361,25 @@ gss_eap_saml_assertion_provider::createAttrContext(void)
     return new gss_eap_saml_assertion_provider;
 }
 
+saml2::Assertion *
+gss_eap_saml_assertion_provider::initAssertion(void)
+{
+    delete m_assertion;
+    m_assertion = saml2::AssertionBuilder::buildAssertion();
+    m_authenticated = false;
+
+    return m_assertion;
+}
+
 /*
  * gss_eap_saml_attr_provider is for retrieving the underlying attributes.
  */
 bool
 gss_eap_saml_attr_provider::getAssertion(int *authenticated,
-                                         const saml2::Assertion **pAssertion) const
+                                         saml2::Assertion **pAssertion,
+                                         bool createIfAbsent) const
 {
-    const gss_eap_saml_assertion_provider *saml;
+    gss_eap_saml_assertion_provider *saml;
 
     if (authenticated != NULL)
         *authenticated = false;
@@ -353,14 +396,24 @@ gss_eap_saml_attr_provider::getAssertion(int *authenticated,
     if (pAssertion != NULL)
         *pAssertion = saml->getAssertion();
 
-    return (saml->getAssertion() != NULL);
+    if (saml->getAssertion() == NULL) {
+        if (createIfAbsent) {
+            if (authenticated != NULL)
+                *authenticated = false;
+            if (pAssertion != NULL)
+                *pAssertion = saml->initAssertion();
+        } else
+            return false;
+    }
+
+    return true;
 }
 
 bool
 gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
                                               void *data) const
 {
-    const saml2::Assertion *assertion;
+    saml2::Assertion *assertion;
     int authenticated;
 
     if (!getAssertion(&authenticated, &assertion))
@@ -381,13 +434,13 @@ gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
      *   using the same name syntax.
      */
     /* For each attribute statement, look for an attribute match */
-    const vector <saml2::AttributeStatement *>&statements =
-        assertion->getAttributeStatements();
+    const vector <saml2::AttributeStatement *> &statements =
+        const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
 
     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
         s != statements.end();
         ++s) {
-        const vector<saml2::Attribute*>attrs =
+        const vector<saml2::Attribute*> &attrs =
             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
 
         for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
@@ -419,37 +472,116 @@ gss_eap_saml_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAtt
     return true;
 }
 
-ssize_t
-gss_eap_saml_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
+static BaseRefVectorOf<XMLCh> *
+decomposeAttributeName(const gss_buffer_t attr)
 {
-    return -1;
+    XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
+    XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
+
+    BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
+
+    delete qualifiedAttr;
+
+    if (components->size() != 2) {
+        delete components;
+        components = NULL;
+    }
+
+    return components;
 }
 
 bool
-gss_eap_saml_attr_provider::setAttribute(int complete,
+gss_eap_saml_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
                                          const gss_buffer_t attr,
                                          const gss_buffer_t value)
 {
-    return false;
+    saml2::Assertion *assertion;
+    saml2::Attribute *attribute;
+    saml2::AttributeValue *attributeValue;
+    saml2::AttributeStatement *attributeStatement;
+
+    if (!getAssertion(NULL, &assertion, true))
+        return false;
+
+    if (assertion->getAttributeStatements().size() != 0) {
+        attributeStatement = assertion->getAttributeStatements().front();
+    } else {
+        attributeStatement = saml2::AttributeStatementBuilder::buildAttributeStatement();
+        assertion->getAttributeStatements().push_back(attributeStatement);
+    }
+
+    /* Check the attribute name consists of name format | whsp | name */
+    BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
+    if (components == NULL)
+        return false;
+
+    attribute = saml2::AttributeBuilder::buildAttribute();
+    attribute->setNameFormat(components->elementAt(0));
+    attribute->setName(components->elementAt(1));
+
+    XMLCh *xmlValue = new XMLCh[value->length + 1];
+    XMLString::transcode((const char *)value->value, xmlValue, attr->length);
+
+    attributeValue = saml2::AttributeValueBuilder::buildAttributeValue();
+    attributeValue->setTextContent(xmlValue);
+
+    attribute->getAttributeValues().push_back(attributeValue);
+
+    assert(attributeStatement != NULL);
+    attributeStatement->getAttributes().push_back(attribute);
+
+    delete components;
+    delete xmlValue;
+
+    return true;
 }
 
 bool
-gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t value)
+gss_eap_saml_attr_provider::deleteAttribute(const gss_buffer_t attr)
 {
-    return false;
-}
+    saml2::Assertion *assertion;
+    bool ret = false;
 
-static BaseRefVectorOf<XMLCh> *
-decomposeAttributeName(const gss_buffer_t attr)
-{
-    XMLCh *qualifiedAttr = new XMLCh[attr->length + 1];
-    XMLString::transcode((const char *)attr->value, qualifiedAttr, attr->length);
+    if (!getAssertion(NULL, &assertion) ||
+        assertion->getAttributeStatements().size() == 0)
+        return false;
 
-    BaseRefVectorOf<XMLCh> *components = XMLString::tokenizeString(qualifiedAttr);
+    /* Check the attribute name consists of name format | whsp | name */
+    BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
+    if (components == NULL)
+        return false;
 
-    delete qualifiedAttr;
+    /* For each attribute statement, look for an attribute match */
+    const vector<saml2::AttributeStatement *> &statements =
+        const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
 
-    return components;
+    for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
+        s != statements.end();
+        ++s) {
+        const vector<saml2::Attribute *> &attrs =
+            const_cast<const saml2::AttributeStatement *>(*s)->getAttributes();
+        ssize_t index = -1, i = 0;
+
+        /* There's got to be an easier way to do this */
+        for (vector<saml2::Attribute *>::const_iterator a = attrs.begin();
+             a != attrs.end();
+             ++a) {
+            if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
+                XMLString::equals((*a)->getName(), components->elementAt(1))) {
+                index = i;
+                break;
+            }
+            ++i;
+        }
+        if (index != -1) {
+            (*s)->getAttributes().erase((*s)->getAttributes().begin() + index);
+            ret = true;
+        }
+    }
+
+    delete components;
+
+    return ret;
 }
 
 bool
@@ -458,7 +590,7 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
                                          int *complete,
                                          const saml2::Attribute **pAttribute) const
 {
-    const saml2::Assertion *assertion;
+    saml2::Assertion *assertion;
 
     if (authenticated != NULL)
         *authenticated = false;
@@ -472,23 +604,21 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
 
     /* Check the attribute name consists of name format | whsp | name */
     BaseRefVectorOf<XMLCh> *components = decomposeAttributeName(attr);
-    if (components == NULL || components->size() != 2) {
-        delete components;
+    if (components == NULL)
         return false;
-    }
 
     /* For each attribute statement, look for an attribute match */
-    const vector <saml2::AttributeStatement *>&statements =
-        assertion->getAttributeStatements();
+    const vector <saml2::AttributeStatement *> &statements =
+        const_cast<const saml2::Assertion *>(assertion)->getAttributeStatements();
     const saml2::Attribute *ret = NULL;
 
     for (vector<saml2::AttributeStatement *>::const_iterator s = statements.begin();
         s != statements.end();
         ++s) {
-        const vector<saml2::Attribute*>& attrs =
+        const vector<saml2::Attribute *> &attrs =
             const_cast<const saml2::AttributeStatement*>(*s)->getAttributes();
 
-        for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
+        for (vector<saml2::Attribute *>::const_iterator a = attrs.begin(); a != attrs.end(); ++a) {
             if (XMLString::equals((*a)->getNameFormat(), components->elementAt(0)) &&
                 XMLString::equals((*a)->getName(), components->elementAt(1))) {
                 ret = *a;
@@ -553,15 +683,15 @@ gss_eap_saml_attr_provider::getAttribute(const gss_buffer_t attr,
 }
 
 gss_any_t
-gss_eap_saml_attr_provider::mapToAny(int authenticated,
-                                     gss_buffer_t type_id) const
+gss_eap_saml_attr_provider::mapToAny(int authenticated GSSEAP_UNUSED,
+                                     gss_buffer_t type_id GSSEAP_UNUSED) const
 {
     return (gss_any_t)NULL;
 }
 
 void
-gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
-                                                  gss_any_t input) const
+gss_eap_saml_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
+                                                  gss_any_t input GSSEAP_UNUSED) const
 {
 }
 
@@ -584,7 +714,7 @@ gss_eap_saml_attr_provider::init(void)
 {
     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_SAML,
                                        "urn:ietf:params:gss-eap:saml-attr",
-                                       gss_eap_saml_attr_provider::createAttrContext);
+                                       createAttrContext);
     return true;
 }
 
@@ -617,5 +747,7 @@ gssEapSamlAttrProvidersFinalize(OM_uint32 *minor)
 {
     gss_eap_saml_attr_provider::finalize();
     gss_eap_saml_assertion_provider::finalize();
+
+    *minor = 0;
     return GSS_S_COMPLETE;
 }