2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * SAML2MessageDecoder.cpp
24 * Base class for SAML 2.0 MessageDecoders.
28 #include "binding/SecurityPolicy.h"
29 #include "saml2/binding/SAML2MessageDecoder.h"
30 #include "saml2/core/Protocols.h"
31 #include "saml2/metadata/Metadata.h"
32 #include "saml2/metadata/MetadataProvider.h"
33 #include "util/SAMLConstants.h"
35 #include <xmltooling/logging.h>
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
45 SAML2MessageDecoder::SAML2MessageDecoder()
49 SAML2MessageDecoder::~SAML2MessageDecoder()
53 const XMLCh* SAML2MessageDecoder::getProtocolFamily() const
55 return samlconstants::SAML20P_NS;
58 void SAML2MessageDecoder::extractMessageDetails(
59 const XMLObject& message, const GenericRequest& request, const XMLCh* protocol, SecurityPolicy& policy
62 // Only handle SAML 2.0 messages.
63 const xmltooling::QName& q = message.getElementQName();
64 if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS))
67 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2");
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 SAML 2.0 protocol 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 MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
112 mc.entityID_unicode = issuer->getName();
113 mc.role = policy.getRole();
114 mc.protocol = protocol;
115 pair<const EntityDescriptor*,const RoleDescriptor*> entity = policy.getMetadataProvider()->getEntityDescriptor(mc);
117 auto_ptr_char temp(issuer->getName());
118 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
121 else if (!entity.second) {
122 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
125 policy.setIssuerMetadata(entity.second);
130 log.warn("caught a bad_cast while extracting message details");