Add property for protocol family to encoders/decoders.
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2MessageDecoder.cpp
1 /*
2  *  Copyright 2001-2010 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 "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"
30
31 #include <xmltooling/logging.h>
32
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;
39 using namespace std;
40
41 SAML2MessageDecoder::SAML2MessageDecoder()
42 {
43 }
44
45 SAML2MessageDecoder::~SAML2MessageDecoder()
46 {
47 }
48
49 const XMLCh* SAML2MessageDecoder::getProtocolFamily() const
50 {
51     return samlconstants::SAML20P_NS;
52 }
53
54 void SAML2MessageDecoder::extractMessageDetails(
55     const XMLObject& message, const GenericRequest& request, const XMLCh* protocol, SecurityPolicy& policy
56     ) const
57 {
58     // Only handle SAML 2.0 messages.
59     const xmltooling::QName& q = message.getElementQName();
60     if (!XMLString::equals(q.getNamespaceURI(), samlconstants::SAML20P_NS))
61         return;
62
63     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2");
64
65     try {
66         const saml2::RootObject& samlRoot = dynamic_cast<const saml2::RootObject&>(message);
67         policy.setMessageID(samlRoot.getID());
68         policy.setIssueInstant(samlRoot.getIssueInstantEpoch());
69
70         log.debug("extracting issuer from SAML 2.0 protocol message");
71         const Issuer* issuer = samlRoot.getIssuer();
72         if (issuer) {
73             policy.setIssuer(issuer);
74         }
75         else if (XMLString::equals(q.getLocalPart(), Response::LOCAL_NAME)) {
76             // No issuer in the message, so we have to try the Response approach.
77             const vector<saml2::Assertion*>& assertions = dynamic_cast<const Response&>(samlRoot).getAssertions();
78             if (!assertions.empty()) {
79                 issuer = assertions.front()->getIssuer();
80                 if (issuer)
81                     policy.setIssuer(issuer);
82             }
83         }
84
85         if (!issuer) {
86             log.warn("issuer identity not extracted");
87             return;
88         }
89
90         if (log.isDebugEnabled()) {
91             auto_ptr_char iname(issuer->getName());
92             log.debug("message from (%s)", iname.get());
93         }
94
95         if (policy.getIssuerMetadata()) {
96             log.debug("metadata for issuer already set, leaving in place");
97             return;
98         }
99
100         if (policy.getMetadataProvider() && policy.getRole()) {
101             if (issuer->getFormat() && !XMLString::equals(issuer->getFormat(), NameIDType::ENTITY)) {
102                 log.warn("non-system entity issuer, skipping metadata lookup");
103                 return;
104             }
105
106             log.debug("searching metadata for message issuer...");
107             MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
108             mc.entityID_unicode = issuer->getName();
109             mc.role = policy.getRole();
110             mc.protocol = protocol;
111             pair<const EntityDescriptor*,const RoleDescriptor*> entity = policy.getMetadataProvider()->getEntityDescriptor(mc);
112             if (!entity.first) {
113                 auto_ptr_char temp(issuer->getName());
114                 log.warn("no metadata found, can't establish identity of issuer (%s)", temp.get());
115                 return;
116             }
117             else if (!entity.second) {
118                 log.warn("unable to find compatible role (%s) in metadata", policy.getRole()->toString().c_str());
119                 return;
120             }
121             policy.setIssuerMetadata(entity.second);
122         }
123     }
124     catch (bad_cast&) {
125         // Just trap it.
126         log.warn("caught a bad_cast while extracting message details");
127     }
128 }