Attempt at a metadata generation handler.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / AssertionConsumerService.cpp
index 7d675b6..18ef907 100644 (file)
@@ -40,6 +40,9 @@
 # include <saml/saml1/core/Assertions.h>
 # include <saml/util/CommonDomainCookie.h>
 using namespace samlconstants;
+using opensaml::saml2md::EntityDescriptor;
+using opensaml::saml2md::IDPSSODescriptor;
+using opensaml::saml2md::SPSSODescriptor;
 #else
 # include "lite/CommonDomainCookie.h"
 #endif
@@ -56,6 +59,8 @@ AssertionConsumerService::AssertionConsumerService(const DOMElement* e, const ch
         ,m_decoder(NULL), m_role(samlconstants::SAML20MD_NS, opensaml::saml2md::IDPSSODescriptor::LOCAL_NAME)
 #endif
 {
+    if (!e)
+        return;
     string address(appId);
     address += getString("Location").second;
     setAddress(address.c_str());
@@ -246,6 +251,20 @@ void AssertionConsumerService::checkAddress(
 
 #ifndef SHIBSP_LITE
 
+void AssertionConsumerService::generateMetadata(SPSSODescriptor& role, const char* handlerURL) const {
+    const char* loc = getString("Location").second;
+    string hurl(handlerURL);
+    if (*loc != '/')
+        hurl += '/';
+    hurl += loc;
+    auto_ptr_XMLCh widen(hurl.c_str());
+    saml2md::AssertionConsumerService* ep = saml2md::AssertionConsumerServiceBuilder::buildAssertionConsumerService();
+    ep->setLocation(widen.get());
+    ep->setBinding(getXMLString("Binding").second);
+    ep->setIndex(getXMLString("index").second);
+    role.getAssertionConsumerServices().push_back(ep);
+}
+
 class SHIBSP_DLLLOCAL DummyContext : public ResolutionContext
 {
 public:
@@ -281,12 +300,31 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
     const vector<const Assertion*>* tokens
     ) const
 {
-    // First we do the extraction of any pushed information.
+    const saml2md::EntityDescriptor* entity = issuer ? dynamic_cast<const saml2md::EntityDescriptor*>(issuer->getParent()) : NULL;
+
+    // First we do the extraction of any pushed information, including from metadata.
     vector<Attribute*> resolvedAttributes;
     AttributeExtractor* extractor = application.getAttributeExtractor();
     if (extractor) {
-        m_log.debug("extracting pushed attributes...");
         Locker extlocker(extractor);
+        if (entity) {
+            pair<bool,const char*> prefix = application.getString("metadataAttributePrefix");
+            if (prefix.first) {
+                m_log.debug("extracting metadata-derived attributes...");
+                try {
+                    extractor->extractAttributes(application, issuer, *entity, resolvedAttributes);
+                    for (vector<Attribute*>::iterator a = resolvedAttributes.begin(); a != resolvedAttributes.end(); ++a) {
+                        vector<string>& ids = (*a)->getAliases();
+                        for (vector<string>::iterator id = ids.begin(); id != ids.end(); ++id)
+                            *id = prefix.second + *id;
+                    }
+                }
+                catch (exception& ex) {
+                    m_log.error("caught exception extracting attributes: %s", ex.what());
+                }
+            }
+        }
+        m_log.debug("extracting pushed attributes...");
         if (v1nameid) {
             try {
                 extractor->extractAttributes(application, issuer, *v1nameid, resolvedAttributes);
@@ -332,28 +370,28 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
     
     try {
         AttributeResolver* resolver = application.getAttributeResolver();
-        if (!resolver && !resolvedAttributes.empty()) {
-            m_log.info("no AttributeResolver available, skipping resolution");
-            return new DummyContext(resolvedAttributes);
+        if (resolver) {
+            m_log.debug("resolving attributes...");
+
+            Locker locker(resolver);
+            auto_ptr<ResolutionContext> ctx(
+                resolver->createResolutionContext(
+                    application,
+                    entity,
+                    protocol,
+                    nameid,
+                    authncontext_class,
+                    authncontext_decl,
+                    tokens,
+                    &resolvedAttributes
+                    )
+                );
+            resolver->resolveAttributes(*ctx.get());
+            // Copy over any pushed attributes.
+            if (!resolvedAttributes.empty())
+                ctx->getResolvedAttributes().insert(ctx->getResolvedAttributes().end(), resolvedAttributes.begin(), resolvedAttributes.end());
+            return ctx.release();
         }
-        
-        m_log.debug("resolving attributes...");
-
-        Locker locker(resolver);
-        auto_ptr<ResolutionContext> ctx(
-            resolver->createResolutionContext(
-                application,
-                dynamic_cast<const saml2md::EntityDescriptor*>(issuer->getParent()),
-                protocol,
-                nameid,
-                authncontext_class,
-                authncontext_decl,
-                tokens,
-                &resolvedAttributes
-                )
-            );
-        resolver->resolveAttributes(*ctx.get());
-        return ctx.release();
     }
     catch (exception& ex) {
         m_log.error("attribute resolution failed: %s", ex.what());
@@ -363,6 +401,45 @@ ResolutionContext* AssertionConsumerService::resolveAttributes(
         return new DummyContext(resolvedAttributes);
     return NULL;
 }
+
+void AssertionConsumerService::extractMessageDetails(const Assertion& assertion, const XMLCh* protocol, opensaml::SecurityPolicy& policy) const
+{
+    policy.setMessageID(assertion.getID());
+    policy.setIssueInstant(assertion.getIssueInstantEpoch());
+
+    if (XMLString::equals(assertion.getElementQName().getNamespaceURI(), samlconstants::SAML20P_NS)) {
+        const saml2::Assertion* a2 = dynamic_cast<const saml2::Assertion*>(&assertion);
+        if (a2) {
+            m_log.debug("extracting issuer from SAML 2.0 assertion");
+            policy.setIssuer(a2->getIssuer());
+        }
+    }
+    else {
+        const saml1::Assertion* a1 = dynamic_cast<const saml1::Assertion*>(&assertion);
+        if (a1) {
+            m_log.debug("extracting issuer from SAML 1.x assertion");
+            policy.setIssuer(a1->getIssuer());
+        }
+    }
+
+    if (policy.getIssuer() && !policy.getIssuerMetadata() && policy.getMetadataProvider()) {
+        m_log.debug("searching metadata for assertion issuer...");
+        const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
+        if (entity) {
+            m_log.debug("matched assertion issuer against metadata, searching for applicable role...");
+            const IDPSSODescriptor* idp=entity->getIDPSSODescriptor(protocol);
+            if (idp)
+                policy.setIssuerMetadata(idp);
+            else if (m_log.isWarnEnabled())
+                m_log.warn("unable to find compatible IdP role in metadata");
+        }
+        else if (m_log.isWarnEnabled()) {
+            auto_ptr_char iname(policy.getIssuer()->getName());
+            m_log.warn("no metadata found, can't establish identity of issuer (%s)", iname.get());
+        }
+    }
+}
+
 #endif
 
 void AssertionConsumerService::maintainHistory(SPRequest& request, const char* entityID, const char* cookieProps) const