2 * Copyright 2001-2009 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 * SAML2MessageDecoder.cpp
20 * Base class for SAML 2.0 MessageDecoders.
24 #include "binding/SecurityPolicy.h"
25 #include "saml2/binding/SAML2MessageDecoder.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;
41 SAML2MessageDecoder::SAML2MessageDecoder()
45 SAML2MessageDecoder::~SAML2MessageDecoder()
49 void SAML2MessageDecoder::extractMessageDetails(
50 const XMLObject& message, const GenericRequest& request, const XMLCh* protocol, SecurityPolicy& policy
53 // Only handle SAML 2.0 messages.
54 const xmltooling::QName& q = message.getElementQName();
55 if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS))
58 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2");
61 const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
62 policy.setMessageID(samlRoot.getID());
63 policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
65 log.debug("extracting issuer from SAML 2.0 protocol message");
66 const Issuer* issuer = samlRoot.getIssuer();
68 policy.setIssuer(issuer);
70 else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
71 // No issuer in the message, so we have to try the Response approach.
72 const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
73 if (!assertions.empty()) {
74 issuer = assertions.front()->getIssuer();
76 policy.setIssuer(issuer);
81 log.warn("issuer identity not extracted");
85 if (log.isDebugEnabled()) {
86 auto_ptr_char iname(issuer->getName());
87 log.debug("message from (%s)", iname.get());
90 if (policy.getIssuerMetadata()) {
91 log.debug("metadata for issuer already set, leaving in place");
95 if (policy.getMetadataProvider() && policy.getRole()) {
96 if (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY)) {
97 log.warn("non-system entity issuer, skipping metadata lookup");
101 log.debug("searching metadata for message issuer...");
102 MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
103 mc.entityID_unicode = issuer->getName();
104 mc.role = policy.getRole();
105 mc.protocol = protocol;
106 pair<const EntityDescriptor*,const RoleDescriptor*> entity = policy.getMetadataProvider()->getEntityDescriptor(mc);
108 auto_ptr_char temp(issuer->getName());
109 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
112 else if (!entity.second) {
113 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
116 policy.setIssuerMetadata(entity.second);
121 log.warn("caught a bad_cast while extracting message details");