First set of logout base classes and non-building draft of SP-initiated logout.
[shibboleth/sp.git] / shibsp / handler / impl / SAML1Consumer.cpp
index c3df16b..5d66396 100644 (file)
  */
 
 #include "internal.h"
-#include "Application.h"
-#include "exceptions.h"
-#include "ServiceProvider.h"
-#include "SessionCache.h"
-#include "attribute/Attribute.h"
-#include "attribute/resolver/AttributeExtractor.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 {
 
@@ -57,12 +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,
@@ -70,8 +79,8 @@ namespace shibsp {
             const PropertySet* settings,
             const XMLObject& xmlObject
             ) const;
-
         bool m_post;
+#endif
     };
 
 #if defined (_MSC_VER)
@@ -85,6 +94,8 @@ namespace shibsp {
     
 };
 
+#ifndef SHIBSP_LITE
+
 string SAML1Consumer::implementProtocol(
     const Application& application,
     const HTTPRequest& httpRequest,
@@ -184,12 +195,31 @@ string SAML1Consumer::implementProtocol(
 
     m_log.debug("SSO profile processing completed successfully");
 
+    NameIdentifier* n = ssoStatement->getSubject()->getNameIdentifier();
+
+    // 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");
+    pair<bool,unsigned int> lifetime = sessionProps ? sessionProps->getUnsignedInt("lifetime") : pair<bool,unsigned int>(true,28800);
+    if (!lifetime.first || lifetime.second == 0)
+        lifetime.second = 28800;
+
     // 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 +228,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, resolvedAttributes, policy.getIssuerMetadata(), ssoStatement->getAuthenticationMethod());
+            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();
+    // Normalize the SAML 1.x NameIdentifier...
     auto_ptr<NameID> nameid(n ? NameIDBuilder::buildNameID() : NULL);
     if (n) {
         nameid->setName(n->getName());
@@ -212,7 +256,17 @@ string SAML1Consumer::implementProtocol(
     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,
+            (!response->getMinorVersion().first || response->getMinorVersion().second==1) ?
+                samlconstants::SAML11_PROTOCOL_ENUM : samlconstants::SAML10_PROTOCOL_ENUM,
+            nameid.get(),
+            ssoStatement->getAuthenticationMethod(),
+            NULL,
+            &tokens,
+            &resolvedAttributes
+            )
         );
 
     if (ctx.get()) {
@@ -227,33 +281,23 @@ 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,
+            now + lifetime.second,
             application,
             httpRequest.getRemoteAddr().c_str(),
             issuerMetadata,
+            (!response->getMinorVersion().first || response->getMinorVersion().second==1) ?
+                samlconstants::SAML11_PROTOCOL_ENUM : samlconstants::SAML10_PROTOCOL_ENUM,
             nameid.get(),
-            authnInstant.get(),
+            ssoStatement->getAuthenticationInstant() ? ssoStatement->getAuthenticationInstant()->getRawData() : NULL,
             NULL,
-            authnMethod.get(),
+            ssoStatement->getAuthenticationMethod(),
             NULL,
             &tokens,
             &resolvedAttributes
             );
-        resolvedAttributes.clear();  // Attributes are owned by cache now.
+        for_each(resolvedAttributes.begin(), resolvedAttributes.end(), cleanup_pair<string,Attribute>());
         return key;
     }
     catch (exception&) {
@@ -261,3 +305,5 @@ string SAML1Consumer::implementProtocol(
         throw;
     }
 }
+
+#endif