Draft artifact resolver.
[shibboleth/sp.git] / shibsp / handler / impl / SAML1Consumer.cpp
index 6b402e5..c2870ee 100644 (file)
  */
 
 #include "internal.h"
-#include "Application.h"
-#include "exceptions.h"
-#include "ServiceProvider.h"
-#include "SessionCache.h"
-#include "attribute/resolver/ResolutionContext.h"
 #include "handler/AssertionConsumerService.h"
 
-#include <saml/saml1/core/Assertions.h>
-#include <saml/saml1/core/Protocols.h>
-#include <saml/saml1/profile/BrowserSSOProfileValidator.h>
-#include <saml/saml2/metadata/Metadata.h>
-
-using namespace shibsp;
+#ifndef SHIBSP_LITE
+# include "exceptions.h"
+# include "Application.h"
+# include "ServiceProvider.h"
+# include "SessionCache.h"
+# include "attribute/Attribute.h"
+# include "attribute/filtering/AttributeFilter.h"
+# include "attribute/filtering/BasicFilteringContext.h"
+# include "attribute/resolver/AttributeExtractor.h"
+# include "attribute/resolver/ResolutionContext.h"
+# include <saml/saml1/core/Assertions.h>
+# include <saml/saml1/core/Protocols.h>
+# include <saml/saml1/profile/BrowserSSOProfileValidator.h>
+# include <saml/saml2/metadata/Metadata.h>
 using namespace opensaml::saml1;
 using namespace opensaml::saml1p;
 using namespace opensaml;
-using namespace xmltooling;
-using namespace log4cpp;
-using namespace std;
 using saml2::NameID;
 using saml2::NameIDBuilder;
 using saml2md::EntityDescriptor;
+#else
+# include "lite/SAMLConstants.h"
+#endif
+
+using namespace shibsp;
+using namespace xmltooling;
+using namespace log4cpp;
+using namespace std;
 
 namespace shibsp {
 
@@ -55,11 +63,15 @@ namespace shibsp {
     {
     public:
         SAML1Consumer(const DOMElement* e, const char* appId)
-            : AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".SAML1")) {
+                : AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".SAML1SSO")) {
+#ifndef SHIBSP_LITE
+            m_post = XMLString::equals(getString("Binding").second, samlconstants::SAML1_PROFILE_BROWSER_POST);
+#endif
         }
         virtual ~SAML1Consumer() {}
         
     private:
+#ifndef SHIBSP_LITE
         string implementProtocol(
             const Application& application,
             const HTTPRequest& httpRequest,
@@ -67,6 +79,8 @@ namespace shibsp {
             const PropertySet* settings,
             const XMLObject& xmlObject
             ) const;
+        bool m_post;
+#endif
     };
 
 #if defined (_MSC_VER)
@@ -80,6 +94,8 @@ namespace shibsp {
     
 };
 
+#ifndef SHIBSP_LITE
+
 string SAML1Consumer::implementProtocol(
     const Application& application,
     const HTTPRequest& httpRequest,
@@ -92,12 +108,15 @@ string SAML1Consumer::implementProtocol(
     m_log.debug("processing message against SAML 1.x SSO profile");
 
     // With the binding aspects now moved out to the MessageDecoder,
-    // the focus here is on the assertion content. For SAML 1.x,
+    // the focus here is on the assertion content. For SAML 1.x POST,
     // all the security comes from the protocol layer, and signing
     // the assertion isn't sufficient. So we can check the policy
     // object now and bail if it's not a secure message.
-    if (!policy.isSecure())
-        throw SecurityPolicyException("Security of SAML 1.x SSO response not established.");
+    if (m_post && !policy.isSecure())
+        throw SecurityPolicyException("Security of SAML 1.x SSO POST response not established.");
+        
+    // Remember whether we already established trust.
+    bool alreadySecured = policy.isSecure();
 
     // Check for errors...this will throw if it's not a successful message.
     checkError(&xmlObject);
@@ -114,6 +133,10 @@ string SAML1Consumer::implementProtocol(
     const AuthenticationStatement* ssoStatement=NULL;
     vector<const opensaml::Assertion*> tokens;
 
+    // Also track "bad" tokens that we'll cache but not use.
+    // This is necessary because there may be valid tokens not aimed at us.
+    vector<const opensaml::Assertion*> badtokens;
+
     // Profile validator.
     time_t now = time(NULL);
     BrowserSSOProfileValidator ssoValidator(application.getAudiences(), now);
@@ -125,13 +148,24 @@ string SAML1Consumer::implementProtocol(
         // Skip unsigned assertion?
         if (!(*a)->getSignature() && flag.first && flag.second) {
             m_log.warn("found unsigned assertion in SAML response, ignoring it per signedAssertions policy");
+            badtokens.push_back(*a);
             continue;
         }
 
         try {
+            // We clear the security flag, so we can tell whether the token was secured on its own.
+            policy.setSecure(false);
+            
             // Run the policy over the assertion. Handles issuer consistency, replay, freshness,
             // and signature verification, assuming the relevant rules are configured.
             policy.evaluate(*(*a));
+            
+            // If no security is in place now, we kick it.
+            if (!alreadySecured && !policy.isSecure()) {
+                m_log.warn("unable to establish security of assertion");
+                badtokens.push_back(*a);
+                continue;
+            }
 
             // Now do profile and core semantic validation to ensure we can use it for SSO.
             ssoValidator.validateAssertion(*(*a));
@@ -144,7 +178,8 @@ string SAML1Consumer::implementProtocol(
                 ssoStatement = (*a)->getAuthenticationStatements().front();
         }
         catch (exception& ex) {
-            m_log.warn("profile validation error in assertion: %s", ex.what());
+            m_log.warn("detected a problem with assertion: %s", ex.what());
+            badtokens.push_back(*a);
         }
     }
 
@@ -160,27 +195,9 @@ string SAML1Consumer::implementProtocol(
 
     m_log.debug("SSO profile processing completed successfully");
 
-    // We've successfully "accepted" at least one SSO token, along with any additional valid tokens.
-    // To complete processing, we need to resolve attributes and then create the session.
-
-    // First, normalize the SAML 1.x NameIdentifier...
-    auto_ptr<NameID> nameid(NameIDBuilder::buildNameID());
     NameIdentifier* n = ssoStatement->getSubject()->getNameIdentifier();
-    if (n) {
-        nameid->setName(n->getName());
-        nameid->setFormat(n->getFormat());
-        nameid->setNameQualifier(n->getNameQualifier());
-    }
-
-    const EntityDescriptor* issuerMetadata = dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent());
-    auto_ptr<ResolutionContext> ctx(
-        resolveAttributes(application, httpRequest, issuerMetadata, *nameid.get(), &tokens)
-        );
-
-    // Copy over any new tokens, but leave them in the context for cleanup.
-    tokens.insert(tokens.end(), ctx->getResolvedAssertions().begin(), ctx->getResolvedAssertions().end());
 
-    // Now we have to extract the authentication details for session setup.
+    // Now we have to extract the authentication details for attribute and session setup.
 
     // Session expiration for SAML 1.x is purely SP-driven, and the method is mapped to a ctx class.
     const PropertySet* sessionProps = application.getPropertySet("Sessions");
@@ -192,20 +209,93 @@ string SAML1Consumer::implementProtocol(
         );
     auto_ptr_char authnMethod(ssoStatement->getAuthenticationMethod());
 
-    vector<shibsp::Attribute*>& attrs = ctx->getResolvedAttributes();
-    string key = application.getServiceProvider().getSessionCache()->insert(
-        lifetime.second ? now + lifetime.second : 0,
-        application,
-        httpRequest.getRemoteAddr().c_str(),
-        issuerMetadata,
-        *nameid.get(),
-        authnInstant.get(),
-        NULL,
-        authnMethod.get(),
-        NULL,
-        &tokens,
-        &attrs
+    // We've successfully "accepted" at least one SSO token, along with any additional valid tokens.
+    // To complete processing, we need to extract and resolve attributes and then create the session.
+    multimap<string,Attribute*> resolvedAttributes;
+    AttributeExtractor* extractor = application.getAttributeExtractor();
+    if (extractor) {
+        m_log.debug("extracting pushed attributes...");
+        Locker extlocker(extractor);
+        if (n) {
+            try {
+                extractor->extractAttributes(application, policy.getIssuerMetadata(), *n, resolvedAttributes);
+            }
+            catch (exception& ex) {
+                m_log.error("caught exception extracting attributes: %s", ex.what());
+            }
+        }
+        for (vector<const opensaml::Assertion*>::const_iterator t = tokens.begin(); t!=tokens.end(); ++t) {
+            try {
+                extractor->extractAttributes(application, policy.getIssuerMetadata(), *(*t), resolvedAttributes);
+            }
+            catch (exception& ex) {
+                m_log.error("caught exception extracting attributes: %s", ex.what());
+            }
+        }
+
+        AttributeFilter* filter = application.getAttributeFilter();
+        if (filter && !resolvedAttributes.empty()) {
+            BasicFilteringContext fc(application, resolvedAttributes, policy.getIssuerMetadata(), authnMethod.get());
+            Locker filtlocker(filter);
+            try {
+                filter->filterAttributes(fc, resolvedAttributes);
+            }
+            catch (exception& ex) {
+                m_log.error("caught exception filtering attributes: %s", ex.what());
+                m_log.error("dumping extracted attributes due to filtering exception");
+                for_each(resolvedAttributes.begin(), resolvedAttributes.end(), cleanup_pair<string,shibsp::Attribute>());
+                resolvedAttributes.clear();
+            }
+        }
+    }
+
+    // Normalize the SAML 1.x NameIdentifier...
+    auto_ptr<NameID> nameid(n ? NameIDBuilder::buildNameID() : NULL);
+    if (n) {
+        nameid->setName(n->getName());
+        nameid->setFormat(n->getFormat());
+        nameid->setNameQualifier(n->getNameQualifier());
+    }
+
+    const EntityDescriptor* issuerMetadata =
+        policy.getIssuerMetadata() ? dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
+    auto_ptr<ResolutionContext> ctx(
+        resolveAttributes(application, issuerMetadata, nameid.get(), authnMethod.get(), NULL, &tokens, &resolvedAttributes)
         );
-    attrs.clear();  // Attributes are owned by cache now.
-    return key;
+
+    if (ctx.get()) {
+        // Copy over any new tokens, but leave them in the context for cleanup.
+        tokens.insert(tokens.end(), ctx->getResolvedAssertions().begin(), ctx->getResolvedAssertions().end());
+
+        // Copy over new attributes, and transfer ownership.
+        resolvedAttributes.insert(ctx->getResolvedAttributes().begin(), ctx->getResolvedAttributes().end());
+        ctx->getResolvedAttributes().clear();
+    }
+
+    // Now merge in bad tokens for caching.
+    tokens.insert(tokens.end(), badtokens.begin(), badtokens.end());
+
+    try {
+        string key = application.getServiceProvider().getSessionCache()->insert(
+            lifetime.second ? now + lifetime.second : 0,
+            application,
+            httpRequest.getRemoteAddr().c_str(),
+            issuerMetadata,
+            nameid.get(),
+            authnInstant.get(),
+            NULL,
+            authnMethod.get(),
+            NULL,
+            &tokens,
+            &resolvedAttributes
+            );
+        resolvedAttributes.clear();  // Attributes are owned by cache now.
+        return key;
+    }
+    catch (exception&) {
+        for_each(resolvedAttributes.begin(), resolvedAttributes.end(), cleanup_pair<string,Attribute>());
+        throw;
+    }
 }
+
+#endif