https://issues.shibboleth.net/jira/browse/SSPCPP-504
[shibboleth/cpp-sp.git] / shibsp / metadata / MetadataExtImpl.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  * MetadataExtImpl.cpp
23  * 
24  * Implementation classes for Shibboleth metadata extensions schema.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "metadata/MetadataExt.h"
30
31 #include <boost/lexical_cast.hpp>
32 #include <boost/lambda/bind.hpp>
33 #include <boost/lambda/if.hpp>
34 #include <boost/lambda/lambda.hpp>
35 #include <xmltooling/AbstractComplexElement.h>
36 #include <xmltooling/AbstractSimpleElement.h>
37 #include <xmltooling/impl/AnyElement.h>
38 #include <xmltooling/io/AbstractXMLObjectMarshaller.h>
39 #include <xmltooling/io/AbstractXMLObjectUnmarshaller.h>
40 #include <xmltooling/signature/KeyInfo.h>
41 #include <xmltooling/util/XMLHelper.h>
42
43 using namespace shibsp;
44 using namespace xmlsignature;
45 using namespace xmltooling;
46 using namespace std;
47
48 using xmlconstants::XMLSIG_NS;
49 using xmlconstants::XML_BOOL_NULL;
50 using shibspconstants::SHIBMD_NS;
51
52 #if defined (_MSC_VER)
53     #pragma warning( push )
54     #pragma warning( disable : 4250 4251 )
55 #endif
56
57 namespace shibsp {
58
59     class SHIBSP_DLLLOCAL ScopeImpl : public virtual Scope,
60         public AbstractSimpleElement,
61         public AbstractDOMCachingXMLObject,
62         public AbstractXMLObjectMarshaller,
63         public AbstractXMLObjectUnmarshaller
64     {
65         void init() {
66             m_Regexp=XML_BOOL_NULL;
67         }
68
69     public:
70
71         ScopeImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
72                 : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
73             init();
74         }
75             
76         ScopeImpl(const ScopeImpl& src)
77                 : AbstractXMLObject(src), AbstractSimpleElement(src), AbstractDOMCachingXMLObject(src) {
78             init();
79             IMPL_CLONE_BOOLEAN_ATTRIB(Regexp);
80         }
81         
82         IMPL_XMLOBJECT_CLONE(Scope);
83         IMPL_BOOLEAN_ATTRIB(Regexp);
84
85     protected:
86         void marshallAttributes(DOMElement* domElement) const {
87             MARSHALL_BOOLEAN_ATTRIB(Regexp,REGEXP,nullptr);
88         }
89
90         void processAttribute(const DOMAttr* attribute) {
91             PROC_BOOLEAN_ATTRIB(Regexp,REGEXP,nullptr);
92             AbstractXMLObjectUnmarshaller::processAttribute(attribute);
93         }
94     };
95
96     class SHIBSP_DLLLOCAL KeyAuthorityImpl : public virtual KeyAuthority,
97             public AbstractComplexElement,
98             public AbstractAttributeExtensibleXMLObject,
99             public AbstractDOMCachingXMLObject,
100             public AbstractXMLObjectMarshaller,
101             public AbstractXMLObjectUnmarshaller
102     {
103         void init() {
104             m_VerifyDepth=nullptr;
105         }
106     public:
107         virtual ~KeyAuthorityImpl() {
108             XMLString::release(&m_VerifyDepth);
109         }
110
111         KeyAuthorityImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType)
112                 : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
113             init();
114         }
115             
116         KeyAuthorityImpl(const KeyAuthorityImpl& src)
117                 : AbstractXMLObject(src), AbstractComplexElement(src),
118                     AbstractAttributeExtensibleXMLObject(src), AbstractDOMCachingXMLObject(src) {
119             init();
120             IMPL_CLONE_INTEGER_ATTRIB(VerifyDepth);
121             IMPL_CLONE_TYPED_CHILDREN(KeyInfo);
122         }
123         
124         IMPL_XMLOBJECT_CLONE(KeyAuthority);
125         IMPL_INTEGER_ATTRIB(VerifyDepth);
126         IMPL_TYPED_CHILDREN(KeyInfo,m_children.end());
127         
128     public:
129         void setAttribute(const xmltooling::QName& qualifiedName, const XMLCh* value, bool ID=false) {
130             if (!qualifiedName.hasNamespaceURI()) {
131                 if (XMLString::equals(qualifiedName.getLocalPart(),VERIFYDEPTH_ATTRIB_NAME)) {
132                     setVerifyDepth(value);
133                     return;
134                 }
135             }
136             AbstractAttributeExtensibleXMLObject::setAttribute(qualifiedName, value, ID);
137         }
138
139     protected:
140         void marshallAttributes(DOMElement* domElement) const {
141             MARSHALL_INTEGER_ATTRIB(VerifyDepth,VERIFYDEPTH,nullptr);
142             marshallExtensionAttributes(domElement);
143         }
144
145         void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
146             PROC_TYPED_CHILDREN(KeyInfo,XMLSIG_NS,false);
147             AbstractXMLObjectUnmarshaller::processChildElement(childXMLObject,root);
148         }
149
150         void processAttribute(const DOMAttr* attribute) {
151             unmarshallExtensionAttribute(attribute);
152         }
153     };
154
155 };
156
157 #if defined (_MSC_VER)
158     #pragma warning( pop )
159 #endif
160
161 // Builder Implementations
162
163 IMPL_XMLOBJECTBUILDER(Scope);
164 IMPL_XMLOBJECTBUILDER(KeyAuthority);
165
166 const XMLCh Scope::LOCAL_NAME[] =                       UNICODE_LITERAL_5(S,c,o,p,e);
167 const XMLCh Scope::REGEXP_ATTRIB_NAME[] =               UNICODE_LITERAL_6(r,e,g,e,x,p);
168 const XMLCh KeyAuthority::LOCAL_NAME[] =                UNICODE_LITERAL_12(K,e,y,A,u,t,h,o,r,i,t,y);
169 const XMLCh KeyAuthority::VERIFYDEPTH_ATTRIB_NAME[] =   UNICODE_LITERAL_11(V,e,r,i,f,y,D,e,p,t,h);