fe604c2f082addc5e2bea8b15969a4bd777a11e4
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2MessageRule.cpp
1 /*
2  *  Copyright 2001-2006 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 "saml2/binding/SAML2MessageRule.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 <log4cpp/Category.hh>
32
33 using namespace opensaml::saml2md;
34 using namespace opensaml::saml2p;
35 using namespace opensaml::saml2;
36 using namespace opensaml;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace opensaml {
42     SecurityPolicyRule* SAML_DLLLOCAL SAML2MessageRuleFactory(const DOMElement* const & e)
43     {
44         return new SAML2MessageRule(e);
45     }
46 };
47
48 void SAML2MessageRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
49 {
50     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML2Message");
51     
52     const QName& q = message.getElementQName(); 
53     policy.setMessageQName(&q);
54     
55     try {
56         const opensaml::RootObject& samlRoot = dynamic_cast<const opensaml::RootObject&>(message);
57         policy.setMessageID(samlRoot.getID());
58         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
59
60         if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS)) {
61             log.warn("not a SAML 2.0 protocol message");
62             throw BindingException("Message was not a recognized SAML 2.0 protocol element.");
63         }
64
65         log.debug("extracting issuer from message");
66         const saml2::RootObject& saml2Root = dynamic_cast<const saml2::RootObject&>(samlRoot);
67         Issuer* issuer = saml2Root.getIssuer();
68         if (issuer && issuer->getName()) {
69             auto_ptr<Issuer> copy(issuer->cloneIssuer());
70             policy.setIssuer(copy.get());
71             copy.release();
72         }
73         else {
74             // No issuer in the message, so we have to try the Response approach. 
75             const vector<Assertion*>& assertions = dynamic_cast<const Response&>(saml2Root).getAssertions();
76             if (!assertions.empty()) {
77                 issuer = assertions.front()->getIssuer();
78                 if (issuer && issuer->getName()) {
79                     auto_ptr<Issuer> copy(issuer->cloneIssuer());
80                     policy.setIssuer(copy.get());
81                     copy.release();
82                 }
83             }
84         }
85
86         if (!policy.getIssuer()) {
87             log.warn("issuer identity not extracted");
88             return;
89         }
90
91         if (log.isDebugEnabled()) {
92             auto_ptr_char iname(policy.getIssuer()->getName());
93             log.debug("message from (%s)", iname.get());
94         }
95
96         if (policy.getMetadataProvider() && policy.getRole()) {
97             if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
98                 log.warn("non-system entity issuer, skipping metadata lookup");
99                 return;
100             }
101             
102             log.debug("searching metadata for message issuer...");
103             const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
104             if (!entity) {
105                 auto_ptr_char temp(policy.getIssuer()->getName());
106                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
107                 return;
108             }
109     
110             log.debug("matched message issuer against metadata, searching for applicable role...");
111             const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), samlconstants::SAML20P_NS);
112             if (!roledesc) {
113                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
114                 return;
115             }
116             policy.setIssuerMetadata(roledesc);
117         }
118     }
119     catch (bad_cast&) {
120         // Just trap it.
121         log.warn("caught a bad_cast while examining message");
122     }
123 }