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 * SAML2MessageRule.cpp
20 * SAML 2.0 message extraction rule
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"
31 #include <xmltooling/logging.h>
33 using namespace opensaml::saml2md;
34 using namespace opensaml::saml2p;
35 using namespace opensaml::saml2;
36 using namespace opensaml;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
42 class SAML_DLLLOCAL SAML2MessageRule : public SecurityPolicyRule
45 SAML2MessageRule(const DOMElement* e) {}
46 virtual ~SAML2MessageRule() {}
48 void evaluate(const xmltooling::XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const;
51 SecurityPolicyRule* SAML_DLLLOCAL SAML2MessageRuleFactory(const DOMElement* const & e)
53 return new SAML2MessageRule(e);
57 void SAML2MessageRule::evaluate(const XMLObject& message, const GenericRequest* request, SecurityPolicy& policy) const
59 Category& log=Category::getInstance(SAML_LOGCAT".SecurityPolicyRule.SAML2Message");
61 const QName& q = message.getElementQName();
62 policy.setMessageQName(&q);
64 if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS)&&
65 !XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20_NS)) {
70 const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
71 policy.setMessageID(samlRoot.getID());
72 policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
74 log.debug("extracting issuer from message");
75 const Issuer* issuer = samlRoot.getIssuer();
77 policy.setIssuer(issuer);
79 else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
80 // No issuer in the message, so we have to try the Response approach.
81 const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
82 if (!assertions.empty()) {
83 issuer = assertions.front()->getIssuer();
85 policy.setIssuer(issuer);
90 log.warn("issuer identity not extracted");
94 if (log.isDebugEnabled()) {
95 auto_ptr_char iname(issuer->getName());
96 log.debug("message from (%s)", iname.get());
99 if (policy.getIssuerMetadata()) {
100 log.debug("metadata for issuer already set, leaving in place");
104 if (policy.getMetadataProvider() && policy.getRole()) {
105 if (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY)) {
106 log.warn("non-system entity issuer, skipping metadata lookup");
110 log.debug("searching metadata for message issuer...");
111 const EntityDescriptor* entity = policy.getMetadataProvider()->getEntityDescriptor(issuer->getName());
113 auto_ptr_char temp(issuer->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(), samlconstants::SAML20P_NS);
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");