First three string functors, added authn context to resolver/filter contexts.
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SAML1Consumer.cpp
index c3df16b..2efa16c 100644 (file)
@@ -26,6 +26,8 @@
 #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 "handler/AssertionConsumerService.h"
@@ -184,12 +186,23 @@ string SAML1Consumer::implementProtocol(
 
     m_log.debug("SSO profile processing completed successfully");
 
+    NameIdentifier* n = ssoStatement->getSubject()->getNameIdentifier();
+
     // 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);
@@ -198,10 +211,24 @@ string SAML1Consumer::implementProtocol(
                 m_log.error("caught exception extracting attributes: %s", ex.what());
             }
         }
+
+        AttributeFilter* filter = application.getAttributeFilter();
+        if (filter && !resolvedAttributes.empty()) {
+            BasicFilteringContext fc(application, policy.getIssuerMetadata());
+            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();
+            }
+        }
     }
 
     // First, normalize the SAML 1.x NameIdentifier...
-    NameIdentifier* n = ssoStatement->getSubject()->getNameIdentifier();
     auto_ptr<NameID> nameid(n ? NameIDBuilder::buildNameID() : NULL);
     if (n) {
         nameid->setName(n->getName());
@@ -209,10 +236,22 @@ string SAML1Consumer::implementProtocol(
         nameid->setNameQualifier(n->getNameQualifier());
     }
 
+    // Now we have to extract the authentication details for 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");
+    pair<bool,unsigned int> lifetime = sessionProps ? sessionProps->getUnsignedInt("lifetime") : make_pair(true,28800);
+    if (!lifetime.first)
+        lifetime.second = 28800;
+    auto_ptr_char authnInstant(
+        ssoStatement->getAuthenticationInstant() ? ssoStatement->getAuthenticationInstant()->getRawData() : NULL
+        );
+    auto_ptr_char authnMethod(ssoStatement->getAuthenticationMethod());
+
     const EntityDescriptor* issuerMetadata =
         policy.getIssuerMetadata() ? dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : NULL;
     auto_ptr<ResolutionContext> ctx(
-        resolveAttributes(application, issuerMetadata, nameid.get(), &tokens, &resolvedAttributes)
+        resolveAttributes(application, issuerMetadata, nameid.get(), authnMethod.get(), NULL, &tokens, &resolvedAttributes)
         );
 
     if (ctx.get()) {
@@ -227,18 +266,6 @@ string SAML1Consumer::implementProtocol(
     // Now merge in bad tokens for caching.
     tokens.insert(tokens.end(), badtokens.begin(), badtokens.end());
 
-    // Now we have to extract the authentication details for 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");
-    pair<bool,unsigned int> lifetime = sessionProps ? sessionProps->getUnsignedInt("lifetime") : make_pair(true,28800);
-    if (!lifetime.first)
-        lifetime.second = 28800;
-    auto_ptr_char authnInstant(
-        ssoStatement->getAuthenticationInstant() ? ssoStatement->getAuthenticationInstant()->getRawData() : NULL
-        );
-    auto_ptr_char authnMethod(ssoStatement->getAuthenticationMethod());
-
     try {
         string key = application.getServiceProvider().getSessionCache()->insert(
             lifetime.second ? now + lifetime.second : 0,