Moved URLEncoder down to tooling lib, added exception->querystring method.
[shibboleth/cpp-opensaml.git] / saml / SAMLConfig.cpp
index 5d68230..d429d96 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "exceptions.h"
 #include "SAMLConfig.h"
 #include "binding/ArtifactMap.h"
+#include "binding/MessageDecoder.h"
 #include "binding/MessageEncoder.h"
 #include "binding/SAMLArtifact.h"
-#include "binding/URLEncoder.h"
+#include "binding/SecurityPolicyRule.h"
 #include "saml1/core/Assertions.h"
 #include "saml1/core/Protocols.h"
 #include "saml2/core/Protocols.h"
 #include "saml2/metadata/Metadata.h"
 #include "saml2/metadata/MetadataProvider.h"
-#include "security/TrustEngine.h"
 #include "util/SAMLConstants.h"
 
 #include <xmltooling/XMLToolingConfig.h>
@@ -66,9 +66,13 @@ extern "C" void SAML_API xmltooling_extension_term()
     SAMLConfig::getConfig().term(false);
 }
 
-DECL_EXCEPTION_FACTORY(ArtifactException,opensaml);
-DECL_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
-DECL_EXCEPTION_FACTORY(BindingException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
+DECL_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
 
 namespace opensaml {
    SAMLInternalConfig g_config;
@@ -90,12 +94,6 @@ void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
     m_artifactMap = artifactMap;
 }
 
-void SAMLConfig::setURLEncoder(URLEncoder* urlEncoder)
-{
-    delete m_urlEncoder;
-    m_urlEncoder = urlEncoder;
-}
-
 bool SAMLInternalConfig::init(bool initXMLTooling)
 {
 #ifdef _DEBUG
@@ -109,12 +107,14 @@ bool SAMLInternalConfig::init(bool initXMLTooling)
         log.debug("XMLTooling library initialized");
     }
 
-    REGISTER_EXCEPTION_FACTORY(ArtifactException,opensaml);
-    REGISTER_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
-    REGISTER_EXCEPTION_FACTORY(BindingException,opensaml);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ArtifactException,opensaml);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataException,opensaml::saml2md);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(MetadataFilterException,opensaml::saml2md);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(BindingException,opensaml);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(ProfileException,opensaml);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(FatalProfileException,opensaml);
+    REGISTER_XMLTOOLING_EXCEPTION_FACTORY(RetryableProfileException,opensaml);
 
-    registerMessageEncoders();
-    registerSAMLArtifacts();
     saml1::registerAssertionClasses();
     saml1p::registerProtocolClasses();
     saml2::registerAssertionClasses();
@@ -122,9 +122,10 @@ bool SAMLInternalConfig::init(bool initXMLTooling)
     saml2md::registerMetadataClasses();
     saml2md::registerMetadataProviders();
     saml2md::registerMetadataFilters();
-    registerTrustEngines();
-    
-    m_urlEncoder = new URLEncoder();
+    registerSAMLArtifacts();
+    registerMessageEncoders();
+    registerMessageDecoders();
+    registerSecurityPolicyRules();
 
     log.info("library initialization complete");
     return true;
@@ -137,21 +138,15 @@ void SAMLInternalConfig::term(bool termXMLTooling)
 #endif
     Category& log=Category::getInstance(SAML_LOGCAT".SAMLConfig");
 
-    saml1::AssertionSchemaValidators.destroyValidators();
-    saml1p::ProtocolSchemaValidators.destroyValidators();
-    saml2::AssertionSchemaValidators.destroyValidators();
-    saml2md::MetadataSchemaValidators.destroyValidators();
-    
-    TrustEngineManager.deregisterFactories();
+    MessageDecoderManager.deregisterFactories();
+    MessageEncoderManager.deregisterFactories();
+    SecurityPolicyRuleManager.deregisterFactories();
+    SAMLArtifactManager.deregisterFactories();
     MetadataFilterManager.deregisterFactories();
     MetadataProviderManager.deregisterFactories();
-    SAMLArtifactManager.deregisterFactories();
-    MessageEncoderManager.deregisterFactories();
 
     delete m_artifactMap;
     m_artifactMap = NULL;
-    delete m_urlEncoder;
-    m_urlEncoder = NULL;
 
     if (termXMLTooling) {
         XMLToolingConfig::getConfig().term();
@@ -199,9 +194,8 @@ string SAMLInternalConfig::hashSHA1(const char* s, bool toHex)
 
     auto_ptr<XSECCryptoHash> hasher(XSECPlatformUtils::g_cryptoProvider->hashSHA1());
     if (hasher.get()) {
-        auto_ptr<char> dup(strdup(s));
         unsigned char buf[21];
-        hasher->hash(reinterpret_cast<unsigned char*>(dup.get()),strlen(dup.get()));
+        hasher->hash(reinterpret_cast<unsigned char*>(const_cast<char*>(s)),strlen(s));
         if (hasher->finish(buf,20)==20) {
             string ret;
             if (toHex) {
@@ -236,3 +230,98 @@ void opensaml::log_openssl()
         code=ERR_get_error_line_data(&file,&line,&data,&flags);
     }
 }
+
+using namespace saml2md;
+
+void opensaml::annotateException(XMLToolingException* e, const EntityDescriptor* entity, bool rethrow)
+{
+    if (entity) {
+        auto_ptr_char id(entity->getEntityID());
+        e->addProperty("entityID",id.get());
+        const list<XMLObject*>& roles=entity->getOrderedChildren();
+        for (list<XMLObject*>::const_iterator child=roles.begin(); child!=roles.end(); ++child) {
+            const RoleDescriptor* role=dynamic_cast<RoleDescriptor*>(*child);
+            if (role && role->isValid()) {
+                const vector<ContactPerson*>& contacts=role->getContactPersons();
+                for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
+                    const XMLCh* ctype=(*c)->getContactType();
+                    if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
+                            || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
+                        GivenName* fname=(*c)->getGivenName();
+                        SurName* lname=(*c)->getSurName();
+                        auto_ptr_char first(fname ? fname->getName() : NULL);
+                        auto_ptr_char last(lname ? lname->getName() : NULL);
+                        if (first.get() && last.get()) {
+                            string contact=string(first.get()) + ' ' + last.get();
+                            e->addProperty("contactName",contact.c_str());
+                        }
+                        else if (first.get())
+                            e->addProperty("contactName",first.get());
+                        else if (last.get())
+                            e->addProperty("contactName",last.get());
+                        const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
+                        if (!emails.empty()) {
+                            auto_ptr_char email(emails.front()->getAddress());
+                            if (email.get())
+                                e->addProperty("contactEmail",email.get());
+                        }
+                        break;
+                    }
+                }
+                if (e->getProperty("contactName") || e->getProperty("contactEmail")) {
+                    auto_ptr_char eurl(role->getErrorURL());
+                    if (eurl.get()) {
+                        e->addProperty("errorURL",eurl.get());
+                    }
+                }
+                break;
+            }
+        }
+    }
+    
+    if (rethrow)
+        e->raise();
+}
+
+void opensaml::annotateException(XMLToolingException* e, const RoleDescriptor* role, bool rethrow)
+{
+    if (role) {
+        auto_ptr_char id(dynamic_cast<EntityDescriptor*>(role->getParent())->getEntityID());
+        e->addProperty("entityID",id.get());
+
+        const vector<ContactPerson*>& contacts=role->getContactPersons();
+        for (vector<ContactPerson*>::const_iterator c=contacts.begin(); c!=contacts.end(); ++c) {
+            const XMLCh* ctype=(*c)->getContactType();
+            if (ctype && (XMLString::equals(ctype,ContactPerson::CONTACT_SUPPORT)
+                    || XMLString::equals(ctype,ContactPerson::CONTACT_TECHNICAL))) {
+                GivenName* fname=(*c)->getGivenName();
+                SurName* lname=(*c)->getSurName();
+                auto_ptr_char first(fname ? fname->getName() : NULL);
+                auto_ptr_char last(lname ? lname->getName() : NULL);
+                if (first.get() && last.get()) {
+                    string contact=string(first.get()) + ' ' + last.get();
+                    e->addProperty("contactName",contact.c_str());
+                }
+                else if (first.get())
+                    e->addProperty("contactName",first.get());
+                else if (last.get())
+                    e->addProperty("contactName",last.get());
+                const vector<EmailAddress*>& emails=const_cast<const ContactPerson*>(*c)->getEmailAddresss();
+                if (!emails.empty()) {
+                    auto_ptr_char email(emails.front()->getAddress());
+                    if (email.get())
+                        e->addProperty("contactEmail",email.get());
+                }
+                break;
+            }
+        }
+
+        auto_ptr_char eurl(role->getErrorURL());
+        if (eurl.get()) {
+            e->addProperty("errorURL",eurl.get());
+        }
+    }
+    
+    if (rethrow)
+        e->raise();
+}