c1e632a7d32cce389304466ce65c6f63fbd113e7
[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 <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     class SAML_DLLLOCAL SAML2MessageRule : public SecurityPolicyRule
43     {
44     public:
45         SAML2MessageRule(const DOMElement* e) {}
46         virtual ~SAML2MessageRule() {}
47         
48         void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
49     };
50
51     SecurityPolicyRule* SAML_DLLLOCAL SAML2MessageRuleFactory(const DOMElement* const & e)
52     {
53         return new SAML2MessageRule(e);
54     }
55 };
56
57 void SAML2MessageRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
58 {
59     Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML2Message");
60     
61     const QName& q = message.getElementQName(); 
62     policy.setMessageQName(&q);
63     
64     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS)&&
65         !XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20_NS)) {
66         log.debug("not a SAML 2.0 protocol message or assertion");
67         return;
68     }
69
70     try {
71         const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
72         policy.setMessageID(samlRoot.getID());
73         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
74
75         log.debug("extracting issuer from message");
76         Issuer* issuer = samlRoot.getIssuer();
77         if (issuer && issuer->getName()) {
78             auto_ptr<Issuer> copy(issuer->cloneIssuer());
79             policy.setIssuer(copy.get());
80             copy.release();
81         }
82         else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
83             // No issuer in the message, so we have to try the Response approach. 
84             const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
85             if (!assertions.empty()) {
86                 issuer = assertions.front()->getIssuer();
87                 if (issuer && issuer->getName()) {
88                     auto_ptr<Issuer> copy(issuer->cloneIssuer());
89                     policy.setIssuer(copy.get());
90                     copy.release();
91                 }
92             }
93         }
94
95         if (!policy.getIssuer()) {
96             log.warn("issuer identity not extracted");
97             return;
98         }
99
100         if (log.isDebugEnabled()) {
101             auto_ptr_char iname(policy.getIssuer()->getName());
102             log.debug("message from (%s)", iname.get());
103         }
104
105         if (policy.getIssuerMetadata()) {
106             log.debug("metadata for issuer already set, leaving in place");
107             return;
108         }
109
110         if (policy.getMetadataProvider() && policy.getRole()) {
111             if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
112                 log.warn("non-system entity issuer, skipping metadata lookup");
113                 return;
114             }
115             
116             log.debug("searching metadata for message issuer...");
117             const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
118             if (!entity) {
119                 auto_ptr_char temp(policy.getIssuer()->getName());
120                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
121                 return;
122             }
123     
124             log.debug("matched message issuer against metadata, searching for applicable role...");
125             const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), samlconstants::SAML20P_NS);
126             if (!roledesc) {
127                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
128                 return;
129             }
130             policy.setIssuerMetadata(roledesc);
131         }
132     }
133     catch (bad_cast&) {
134         // Just trap it.
135         log.warn("caught a bad_cast while examining message");
136     }
137 }