2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML1MessageRule.cpp
20 * SAML 1.x message extraction rule
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"
34 #include <log4cpp/Category.hh>
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
45 class SAML_DLLLOCAL SAML1MessageRule : public SecurityPolicyRule
48 SAML1MessageRule(const DOMElement* e) {}
49 virtual ~SAML1MessageRule() {}
51 void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
54 SecurityPolicyRule* SAML_DLLLOCAL SAML1MessageRuleFactory(const DOMElement* const & e)
56 return new SAML1MessageRule(e);
60 void SAML1MessageRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
62 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML1Message");
64 const QName& q = message.getElementQName();
65 policy.setMessageQName(&q);
67 if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML1P_NS)) {
68 log.debug("not a SAML 1.x protocol message");
74 const RootObject& samlRoot = dynamic_cast<const RootObject&>(message);
75 policy.setMessageID(samlRoot.getID());
76 policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
78 log.debug("extracting issuer from message");
80 // Only samlp:Response is known to carry issuer (via payload) in standard SAML 1.x.
81 const XMLCh* protocol = NULL;
82 if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
83 // Should be a samlp:Response.
84 const vector<saml1::Assertion*>& assertions = dynamic_cast<const saml1p::Response&>(samlRoot).getAssertions();
85 if (!assertions.empty()) {
86 const saml1::Assertion* a = assertions.front();
88 auto_ptr<saml2::Issuer> issuer(saml2::IssuerBuilder::buildIssuer());
89 issuer->setName(a->getIssuer());
90 policy.setIssuer(issuer.get());
91 issuer.release(); // owned by policy now
92 pair<bool,int> minor = a->getMinorVersion();
93 protocol = (minor.first && minor.second==0) ?
94 samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM;
100 log.warn("issuer identity not extracted");
104 if (log.isDebugEnabled()) {
105 auto_ptr_char iname(policy.getIssuer()->getName());
106 log.debug("message from (%s)", iname.get());
109 if (policy.getMetadataProvider() && policy.getRole()) {
110 log.debug("searching metadata for message issuer...");
111 const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(policy.getIssuer()->getName());
113 auto_ptr_char temp(policy.getIssuer()->getName());
114 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
118 log.debug("matched message issuer against metadata, searching for applicable role...");
119 const RoleDescriptor* roledesc=entity->getRoleDescriptor(*policy.getRole(), protocol);
121 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
124 policy.setIssuerMetadata(roledesc);
129 log.warn("caught a bad_cast while examining message");