First set of logout base classes and non-building draft of SP-initiated logout.
[shibboleth/sp.git] / shibsp / attribute / resolver / impl / QueryAttributeResolver.cpp
index 33250ed..738d73b 100644 (file)
@@ -25,6 +25,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/AttributeResolver.h"
 #include "attribute/resolver/ResolutionContext.h"
@@ -64,16 +66,23 @@ namespace shibsp {
     {
     public:
         QueryContext(const Application& application, const Session& session)
-            : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(session.getNameID()) {
+                : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(NULL) {
+            m_protocol = XMLString::transcode(session.getProtocol());
+            m_class = XMLString::transcode(session.getAuthnContextClassRef());
+            m_decl = XMLString::transcode(session.getAuthnContextDeclRef());
         }
         
         QueryContext(
             const Application& application,
             const EntityDescriptor* issuer,
+            const XMLCh* protocol,
             const NameID* nameid,
+            const XMLCh* authncontext_class=NULL,
+            const XMLCh* authncontext_decl=NULL,
             const vector<const opensaml::Assertion*>* tokens=NULL,
             const multimap<string,Attribute*>* attributes=NULL
-            ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer), m_nameid(nameid) {
+            ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer),
+                m_protocol(protocol), m_nameid(nameid), m_class(authncontext_class), m_decl(authncontext_decl) {
 
             if (tokens) {
                 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
@@ -92,6 +101,11 @@ namespace shibsp {
         }
         
         ~QueryContext() {
+            if (m_session) {
+                XMLString::release((XMLCh**)&m_protocol);
+                XMLString::release((XMLCh**)&m_class);
+                XMLString::release((XMLCh**)&m_decl);
+            }
             if (m_metadata)
                 m_metadata->unlock();
             for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
@@ -117,12 +131,21 @@ namespace shibsp {
             }
             return NULL;
         }
+        const XMLCh* getProtocol() const {
+            return m_protocol;
+        }
         const NameID* getNameID() const {
-            return m_nameid;
+            return m_session ? m_session->getNameID() : m_nameid;
+        }
+        const XMLCh* getClassRef() const {
+            return m_class;
+        }
+        const XMLCh* getDeclRef() const {
+            return m_decl;
         }
         const Session* getSession() const {
             return m_session;
-        }        
+        }
         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
             return m_attributes;
         }
@@ -136,7 +159,10 @@ namespace shibsp {
         const Session* m_session;
         mutable MetadataProvider* m_metadata;
         mutable const EntityDescriptor* m_entity;
+        const XMLCh* m_protocol;
         const NameID* m_nameid;
+        const XMLCh* m_class;
+        const XMLCh* m_decl;
         multimap<string,shibsp::Attribute*> m_attributes;
         vector<opensaml::Assertion*> m_assertions;
     };
@@ -156,11 +182,14 @@ namespace shibsp {
         ResolutionContext* createResolutionContext(
             const Application& application,
             const EntityDescriptor* issuer,
+            const XMLCh* protocol,
             const NameID* nameid,
+            const XMLCh* authncontext_class=NULL,
+            const XMLCh* authncontext_decl=NULL,
             const vector<const opensaml::Assertion*>* tokens=NULL,
             const multimap<string,shibsp::Attribute*>* attributes=NULL
             ) const {
-            return new QueryContext(application,issuer,nameid,tokens,attributes);
+            return new QueryContext(application,issuer,protocol,nameid,authncontext_class,authncontext_decl,tokens,attributes);
         }
 
         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
@@ -169,6 +198,10 @@ namespace shibsp {
 
         void resolveAttributes(ResolutionContext& ctx) const;
 
+        void getAttributeIds(vector<string>& attributes) const {
+            // Nothing to do, only the extractor would actually generate them.
+        }
+
     private:
         bool SAML1Query(QueryContext& ctx) const;
         bool SAML2Query(QueryContext& ctx) const;
@@ -236,7 +269,7 @@ bool QueryResolver::SAML1Query(QueryContext& ctx) const
         version = 0;
     }
     if (!AA) {
-        m_log.info("no SAML 1.x AttributeAuthority role found in metadata");
+        m_log.debug("no SAML 1.x AttributeAuthority role found in metadata");
         return false;
     }
 
@@ -271,7 +304,7 @@ bool QueryResolver::SAML1Query(QueryContext& ctx) const
             request->setAttributeQuery(query);
             request->setMinorVersion(version);
 
-            SAML1SOAPClient client(soaper);
+            SAML1SOAPClient client(soaper, false);
             client.sendSAML(request, mcc, loc.get());
             response = client.receiveSAML();
         }
@@ -282,9 +315,15 @@ bool QueryResolver::SAML1Query(QueryContext& ctx) const
     }
 
     if (!response) {
-        m_log.error("unable to successfully query for attributes");
+        m_log.error("unable to obtain a SAML response from attribute authority");
         return false;
     }
+    else if (!response->getStatus() || !response->getStatus()->getStatusCode() || response->getStatus()->getStatusCode()->getValue()==NULL ||\r
+            *(response->getStatus()->getStatusCode()->getValue()) != saml1p::StatusCode::SUCCESS) {\r
+        delete response;\r
+        m_log.error("attribute authority returned a SAML error");\r
+        return true;\r
+    }\r
 
     const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
     if (assertions.size()>1)
@@ -321,9 +360,18 @@ bool QueryResolver::SAML1Query(QueryContext& ctx) const
             Locker extlocker(extractor);
             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
         }
+
+        AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
+        if (filter) {
+            BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
+            Locker filtlocker(filter);
+            filter->filterAttributes(fc, ctx.getResolvedAttributes());
+        }
     }
     catch (exception& ex) {
         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
+        for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
+        ctx.getResolvedAttributes().clear();
     }
 
     return true;
@@ -337,7 +385,7 @@ bool QueryResolver::SAML2Query(QueryContext& ctx) const
 
     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
     if (!AA) {
-        m_log.info("no SAML 2 AttributeAuthority role found in metadata");
+        m_log.debug("no SAML 2 AttributeAuthority role found in metadata");
         return false;
     }
 
@@ -367,7 +415,7 @@ bool QueryResolver::SAML2Query(QueryContext& ctx) const
             for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
                 query->getAttributes().push_back((*ad)->cloneAttribute());
 
-            SAML2SOAPClient client(soaper);
+            SAML2SOAPClient client(soaper, false);
             client.sendSAML(query, mcc, loc.get());
             srt = client.receiveSAML();
         }
@@ -378,7 +426,7 @@ bool QueryResolver::SAML2Query(QueryContext& ctx) const
     }
 
     if (!srt) {
-        m_log.error("unable to successfully query for attributes");
+        m_log.error("unable to obtain a SAML response from attribute authority");
         return false;
     }
     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
@@ -387,6 +435,12 @@ bool QueryResolver::SAML2Query(QueryContext& ctx) const
         m_log.error("message was not a samlp:Response");
         return true;
     }
+    else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
+            !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
+        delete srt;
+        m_log.error("attribute authority returned a SAML error");
+        return true;
+    }
 
     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
     if (assertions.size()>1)
@@ -423,9 +477,18 @@ bool QueryResolver::SAML2Query(QueryContext& ctx) const
             Locker extlocker(extractor);
             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
         }
+
+        AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
+        if (filter) {
+            BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
+            Locker filtlocker(filter);
+            filter->filterAttributes(fc, ctx.getResolvedAttributes());
+        }
     }
     catch (exception& ex) {
         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
+        for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
+        ctx.getResolvedAttributes().clear();
     }
 
     return true;
@@ -445,10 +508,11 @@ void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
 
     if (qctx.getNameID() && qctx.getEntityDescriptor()) {
         m_log.debug("attempting SAML 2.0 attribute query");
-        if (!SAML2Query(qctx)) {
-            m_log.debug("attempting SAML 1.x attribute query");
-            SAML1Query(qctx);
-        }
+        if (SAML2Query(qctx))
+            return;
+        m_log.debug("attempting SAML 1.x attribute query");
+        SAML1Query(qctx);
+        return;
     }
     m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");
 }