Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2MessageDecoder.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * SAML2MessageDecoder.cpp
19  *
20  * Base class for SAML 2.0 MessageDecoders.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml2/binding/SAML2MessageDecoder.h"
27 #include "saml2/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30 #include "util/SAMLConstants.h"
31
32 #include <xmltooling/logging.h>
33
34 using namespace opensaml::saml2md;
35 using namespace opensaml::saml2p;
36 using namespace opensaml::saml2;
37 using namespace opensaml;
38 using namespace xmltooling::logging;
39 using namespace xmltooling;
40 using namespace std;
41
42 void SAML2MessageDecoder::extractMessageDetails(
43     const XMLObject& message, const GenericRequest& request, const XMLCh* protocol, SecurityPolicy& policy
44     ) const
45 {
46     // Only handle SAML 2.0 messages.
47     const xmltooling::QName& q = message.getElementQName();
48     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS))
49         return;
50
51     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2");
52
53     try {
54         const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
55         policy.setMessageID(samlRoot.getID());
56         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
57
58         log.debug("extracting issuer from SAML 2.0 protocol message");
59         const Issuer* issuer = samlRoot.getIssuer();
60         if (issuer) {
61             policy.setIssuer(issuer);
62         }
63         else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
64             // No issuer in the message, so we have to try the Response approach.
65             const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
66             if (!assertions.empty()) {
67                 issuer = assertions.front()->getIssuer();
68                 if (issuer)
69                     policy.setIssuer(issuer);
70             }
71         }
72
73         if (!issuer) {
74             log.warn("issuer identity not extracted");
75             return;
76         }
77
78         if (log.isDebugEnabled()) {
79             auto_ptr_char iname(issuer->getName());
80             log.debug("message from (%s)", iname.get());
81         }
82
83         if (policy.getIssuerMetadata()) {
84             log.debug("metadata for issuer already set, leaving in place");
85             return;
86         }
87
88         if (policy.getMetadataProvider() && policy.getRole()) {
89             if (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY)) {
90                 log.warn("non-system entity issuer, skipping metadata lookup");
91                 return;
92             }
93
94             log.debug("searching metadata for message issuer...");
95             MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
96             mc.entityID_unicode = issuer->getName();
97             mc.role = policy.getRole();
98             mc.protocol = protocol;
99             pair<const EntityDescriptor*,const RoleDescriptor*> entity = policy.getMetadataProvider()->getEntityDescriptor(mc);
100             if (!entity.first) {
101                 auto_ptr_char temp(issuer->getName());
102                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
103                 return;
104             }
105             else if (!entity.second) {
106                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
107                 return;
108             }
109             policy.setIssuerMetadata(entity.second);
110         }
111     }
112     catch (bad_cast&) {
113         // Just trap it.
114         log.warn("caught a bad_cast while extracting message details");
115     }
116 }