Set fourth file version digit to signify rebuild.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / RequireValidUntilMetadataFilter.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  * RequireValidUntilMetadataFilter.cpp
23  * 
24  * MetadataFilter that enforces expiration requirements.
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataFilter.h"
30
31 #include <xmltooling/logging.h>
32 #include <xmltooling/util/NDC.h>
33
34 using namespace opensaml::saml2md;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace std;
38
39 namespace opensaml {
40     namespace saml2md {
41                 
42         class SAML_DLLLOCAL RequireValidUntilMetadataFilter : public MetadataFilter
43         {
44         public:
45             RequireValidUntilMetadataFilter(const DOMElement* e);
46             ~RequireValidUntilMetadataFilter() {}
47             
48             const char* getId() const { return REQUIREVALIDUNTIL_METADATA_FILTER; }
49             void doFilter(XMLObject& xmlObject) const;
50
51         private:
52             time_t m_maxValidityInterval;
53         }; 
54
55         MetadataFilter* SAML_DLLLOCAL RequireValidUntilMetadataFilterFactory(const DOMElement* const & e)
56         {
57             return new RequireValidUntilMetadataFilter(e);
58         }
59
60     };
61 };
62
63 static const XMLCh maxValidityInterval[] =  UNICODE_LITERAL_19(m,a,x,V,a,l,i,d,i,t,y,I,n,t,e,r,v,a,l);
64
65 RequireValidUntilMetadataFilter::RequireValidUntilMetadataFilter(const DOMElement* e)
66     : m_maxValidityInterval(XMLHelper::getAttrInt(e, 60 * 60 * 24 * 7, maxValidityInterval))
67 {
68 }
69
70 void RequireValidUntilMetadataFilter::doFilter(XMLObject& xmlObject) const
71 {
72     const TimeBoundSAMLObject* tbo = dynamic_cast<const TimeBoundSAMLObject*>(&xmlObject);
73     if (!tbo)
74         throw MetadataFilterException("Metadata root element was invalid.");
75
76     if (!tbo->getValidUntil())
77         throw MetadataFilterException("Metadata did not include a validUntil attribute.");
78
79     if (tbo->getValidUntilEpoch() - time(nullptr) > m_maxValidityInterval)
80         throw MetadataFilterException("Metadata validity interval is larger than permitted.");
81 }