SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2MessageDecoder.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * SAML2MessageDecoder.cpp
23  *
24  * Base class for SAML 2.0 MessageDecoders.
25  */
26
27 #include "internal.h"
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"
34
35 #include <xmltooling/logging.h>
36
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;
43 using namespace std;
44
45 SAML2MessageDecoder::SAML2MessageDecoder()
46 {
47 }
48
49 SAML2MessageDecoder::~SAML2MessageDecoder()
50 {
51 }
52
53 const XMLCh* SAML2MessageDecoder::getProtocolFamily() const
54 {
55     return samlconstants::SAML20P_NS;
56 }
57
58 void SAML2MessageDecoder::extractMessageDetails(
59     const XMLObject& message, const GenericRequest& request, const XMLCh* protocol, SecurityPolicy& policy
60     ) const
61 {
62     // Only handle SAML 2.0 messages.
63     const xmltooling::QName& q = message.getElementQName();
64     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS))
65         return;
66
67     Category& log = Category::getInstance(SAML_LOGCAT ".MessageDecoder.SAML2");
68
69     try {
70         const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
71         policy.setMessageID(samlRoot.getID());
72         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
73
74         log.debug("extracting issuer from SAML 2.0 protocol message");
75         const Issuer* issuer = samlRoot.getIssuer();
76         if (issuer) {
77             policy.setIssuer(issuer);
78         }
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();
84                 if (issuer)
85                     policy.setIssuer(issuer);
86             }
87         }
88
89         if (!issuer) {
90             log.warn("issuer identity not extracted");
91             return;
92         }
93
94         if (log.isDebugEnabled()) {
95             auto_ptr_char iname(issuer->getName());
96             log.debug("message from (%s)", iname.get());
97         }
98
99         if (policy.getIssuerMetadata()) {
100             log.debug("metadata for issuer already set, leaving in place");
101             return;
102         }
103
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");
107                 return;
108             }
109
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);
116             if (!entity.first) {
117                 auto_ptr_char temp(issuer->getName());
118                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
119                 return;
120             }
121             else if (!entity.second) {
122                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
123                 return;
124             }
125             policy.setIssuerMetadata(entity.second);
126         }
127     }
128     catch (bad_cast&) {
129         // Just trap it.
130         log.warn("caught a bad_cast while extracting message details");
131     }
132 }