3d70d87a6607e6fe195778324ca198c7b1083c9a
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1MessageRule.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 "RootObject.h"
26 #include "saml1/binding/SAML1MessageRule.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 <log4cpp/Category.hh>
35
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
41 using namespace std;
42
43 namespace opensaml {
44     SecurityPolicyRule* SAML_DLLLOCAL SAML1MessageRuleFactory(const DOMElement* const & e)
45     {
46         return new SAML1MessageRule(e);
47     }
48 };
49
50 void SAML1MessageRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
51 {
52     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML1Message");
53     
54     const QName& q = message.getElementQName(); 
55     policy.setMessageQName(&q);
56     
57     try {
58         const RootObject& samlRoot = dynamic_cast<const RootObject&>(message);
59         policy.setMessageID(samlRoot.getID());
60         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
61
62         if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML1P_NS)) {
63             log.warn("not a SAML 1.x protocol message");
64             throw BindingException("Message was not a recognized SAML 1.x protocol element.");
65         }
66
67         log.debug("extracting issuer from message");
68
69         // Only samlp:Response is known to carry issuer (via payload) in standard SAML 1.x.
70         const XMLCh* protocol = NULL;
71         if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
72             // Should be a samlp:Response.
73             const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(samlRoot).getAssertions();
74             if (!assertions.empty()) {
75                 const saml1::Assertion* a = assertions.front();
76                 if (a->getIssuer()) {
77                     auto_ptr<saml2::Issuer> issuer(saml2::IssuerBuilder::buildIssuer());
78                     issuer->setName(a->getIssuer());
79                     policy.setIssuer(issuer.get());
80                     issuer.release();   // owned by policy now
81                     pair<bool,int> minor = a->getMinorVersion();
82                     protocol = (minor.first && minor.second==0) ?
83                         samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM;
84                 }
85             }
86         }
87         
88         if (!protocol) {
89             log.warn("issuer identity not extracted");
90             return;
91         }
92
93         if (log.isDebugEnabled()) {
94             auto_ptr_char iname(policy.getIssuer()->getName());
95             log.debug("message from (%s)", iname.get());
96         }
97         
98         if (policy.getMetadataProvider() && policy.getRole()) {
99             log.debug("searching metadata for message issuer...");
100             const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
101             if (!entity) {
102                 auto_ptr_char temp(policy.getIssuer()->getName());
103                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
104                 return;
105             }
106     
107             log.debug("matched message issuer against metadata, searching for applicable role...");
108             const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), protocol);
109             if (!roledesc) {
110                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
111                 return;
112             }
113             policy.setIssuerMetadata(roledesc);
114         }
115     }
116     catch (bad_cast&) {
117         // Just trap it.
118         log.warn("caught a bad_cast while examining message");
119     }
120 }