Multi-line svn commit, see body.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1MessageRule.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  * SAML1MessageRule.cpp
19  * 
20  * SAML 1.x message extraction rule
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "RootObject.h"
26 #include "binding/SecurityPolicyRule.h"
27 #include "saml1/core/Assertions.h"
28 #include "saml1/core/Protocols.h"
29 #include "saml2/core/Assertions.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/MetadataProvider.h"
32 #include "util/SAMLConstants.h"
33
34 #include <xmltooling/logging.h>
35
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
41 using namespace std;
42
43 namespace opensaml {
44
45     class SAML_DLLLOCAL SAML1MessageRule : public SecurityPolicyRule
46     {
47     public:
48         SAML1MessageRule(const DOMElement* e) {}
49         virtual ~SAML1MessageRule() {}
50         
51         const char* getType() const {
52             return SAML1MESSAGE_POLICY_RULE;
53         }
54         void evaluate(const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy) const;
55     };
56
57     SecurityPolicyRule* SAML_DLLLOCAL SAML1MessageRuleFactory(const DOMElement* const & e)
58     {
59         return new SAML1MessageRule(e);
60     }
61 };
62
63 void SAML1MessageRule::evaluate(
64     const XMLObject& message, const GenericRequest* request, const XMLCh* protocol, SecurityPolicy& policy
65     ) const
66 {
67     // Only handle SAML 1.x protocol and SAML 1.x messages.
68     if (!XMLString::equals(protocol, samlconstants::SAML11_PROTOCOL_ENUM) &&
69         !XMLString::equals(protocol, samlconstants::SAML10_PROTOCOL_ENUM))
70         return;
71     const QName& q = message.getElementQName();
72     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML1P_NS) &&
73         !XMLString::equals(q.getNamespaceURI(), samlconstants::SAML1_NS))
74         return;
75     
76     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML1Message");
77     
78     try {
79         const RootObject& samlRoot = dynamic_cast<const RootObject&>(message);
80         policy.setMessageID(samlRoot.getID());
81         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
82
83         log.debug("extracting issuer from message");
84
85         const saml1::Assertion* a = NULL;
86
87         // Handle assertions directly.
88         if (XMLString::equals(q.getLocalPart(), saml1::Assertion::LOCAL_NAME))
89             a = dynamic_cast<const saml1::Assertion*>(&samlRoot);
90             
91         // Only samlp:Response is known to carry issuer (via payload) in standard SAML 1.x.
92         if (!a && XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
93             // Should be a samlp:Response.
94             const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(samlRoot).getAssertions();
95             if (!assertions.empty())
96                 a = assertions.front();
97         }
98
99         if (a) {
100             policy.setIssuer(a->getIssuer());
101         }
102         else {
103             log.warn("issuer identity not extracted");
104             return;
105         }
106
107         if (log.isDebugEnabled()) {
108             auto_ptr_char iname(a->getIssuer());
109             log.debug("message from (%s)", iname.get());
110         }
111         
112         if (policy.getIssuerMetadata()) {
113             log.debug("metadata for issuer already set, leaving in place");
114             return;
115         }
116         
117         if (policy.getMetadataProvider() && policy.getRole()) {
118             log.debug("searching metadata for message issuer...");
119             const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(a->getIssuer());
120             if (!entity) {
121                 auto_ptr_char temp(a->getIssuer());
122                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
123                 return;
124             }
125     
126             log.debug("matched message issuer against metadata, searching for applicable role...");
127             const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), protocol);
128             if (!roledesc) {
129                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
130                 return;
131             }
132             policy.setIssuerMetadata(roledesc);
133         }
134     }
135     catch (bad_cast&) {
136         // Just trap it.
137         log.warn("caught a bad_cast while examining message");
138     }
139 }