Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2MessageRule.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * SAML2MessageRule.cpp
19  * 
20  * SAML 2.0 message extraction rule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicyRule.h"
26 #include "saml2/core/Protocols.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
29 #include "util/SAMLConstants.h"
30
31 #include <xmltooling/logging.h>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml::saml2p;
35 using namespace opensaml::saml2;
36 using namespace opensaml;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace std;
40
41 namespace opensaml {
42     class SAML_DLLLOCAL SAML2MessageRule : public SecurityPolicyRule
43     {
44     public:
45         SAML2MessageRule(const DOMElement* e) {}
46         virtual ~SAML2MessageRule() {}
47         
48         const char* getType() const {
49             return SAML2MESSAGE_POLICY_RULE;
50         }
51         void evaluate(const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy) const;
52     };
53
54     SecurityPolicyRule* SAML_DLLLOCAL SAML2MessageRuleFactory(const DOMElement* const & e)
55     {
56         return new SAML2MessageRule(e);
57     }
58 };
59
60 void SAML2MessageRule::evaluate(
61     const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy
62     ) const
63 {
64     // Only handle SAML 2.0 protocol and 2.0 messages.
65     if (!XMLString::equals(protocol, samlconstants::SAML20P_NS))
66         return;
67     const QName& q = message.getElementQName();
68     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS)&&
69         !XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20_NS))
70         return;
71
72     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML2Message");
73     
74     try {
75         const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
76         policy.setMessageID(samlRoot.getID());
77         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
78
79         log.debug("extracting issuer from message");
80         const Issuer* issuer = samlRoot.getIssuer();
81         if (issuer) {
82             policy.setIssuer(issuer);
83         }
84         else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
85             // No issuer in the message, so we have to try the Response approach. 
86             const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
87             if (!assertions.empty()) {
88                 issuer = assertions.front()->getIssuer();
89                 if (issuer)
90                     policy.setIssuer(issuer);
91             }
92         }
93
94         if (!issuer) {
95             log.warn("issuer identity not extracted");
96             return;
97         }
98
99         if (log.isDebugEnabled()) {
100             auto_ptr_char iname(issuer->getName());
101             log.debug("message from (%s)", iname.get());
102         }
103
104         if (policy.getIssuerMetadata()) {
105             log.debug("metadata for issuer already set, leaving in place");
106             return;
107         }
108
109         if (policy.getMetadataProvider() && policy.getRole()) {
110             if (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY)) {
111                 log.warn("non-system entity issuer, skipping metadata lookup");
112                 return;
113             }
114             
115             log.debug("searching metadata for message issuer...");
116             const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(issuer->getName());
117             if (!entity) {
118                 auto_ptr_char temp(issuer->getName());
119                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
120                 return;
121             }
122     
123             log.debug("matched message issuer against metadata, searching for applicable role...");
124             const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), protocol);
125             if (!roledesc) {
126                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
127                 return;
128             }
129             policy.setIssuerMetadata(roledesc);
130         }
131     }
132     catch (bad_cast&) {
133         // Just trap it.
134         log.warn("caught a bad_cast while examining message");
135     }
136 }