Revamped binding classes with security policy layer.
[shibboleth/cpp-opensaml.git] / saml / binding / SecurityPolicy.h
1 /*
2  *  Copyright 2001-2006 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  * @file saml/binding/SecurityPolicy.h
19  * 
20  * Overall policy used to verify the security of an incoming message.
21  */
22
23 #ifndef __saml_secpol_h__
24 #define __saml_secpol_h__
25
26 #include <saml/binding/SecurityPolicyRule.h>
27 #include <vector>
28
29 #if defined (_MSC_VER)
30     #pragma warning( push )
31     #pragma warning( disable : 4250 4251 )
32 #endif
33
34 namespace opensaml {
35
36     namespace saml2md {
37         class SAML_API MetadataProvider;
38     };
39     
40     /**
41      * A policy used to verify the security of an incoming message.
42      * 
43      * <p>Its security mechanisms may be used to examine the transport layer
44      * (e.g client certificates and HTTP basic auth passwords) or to check the
45      * payload of a request to ensure it meets certain criteria (e.g. valid
46      * digital signature, freshness, replay).
47      * 
48      * <p>Policy objects can be reused, but are not thread-safe. 
49      */
50     class SAML_API SecurityPolicy
51     {
52         MAKE_NONCOPYABLE(SecurityPolicy);
53     public:
54         /**
55          * Constructor for policy.
56          *
57          * @param rules             reference to array of policy rules to use 
58          * @param metadataProvider  locked MetadataProvider instance
59          * @param role              identifies the role (generally IdP or SP) of the policy peer 
60          * @param trustEngine       TrustEngine to authenticate policy peer
61          */
62         SecurityPolicy(
63             const std::vector<const SecurityPolicyRule*>& rules,
64             const saml2md::MetadataProvider* metadataProvider=NULL,
65             const xmltooling::QName* role=NULL,
66             const TrustEngine* trustEngine=NULL
67             ) : m_issuer(NULL), m_issuerRole(NULL), m_rules(rules), m_metadata(metadataProvider),
68                 m_role(role ? *role : xmltooling::QName()), m_trust(trustEngine) {
69         }
70         virtual ~SecurityPolicy();
71
72         /**
73          * Returns the locked MetadataProvider supplied to the policy.
74          * 
75          * @return the supplied MetadataProvider or NULL
76          */
77         const saml2md::MetadataProvider* getMetadataProvider() const {
78             return m_metadata;
79         }
80
81         /**
82          * Returns the peer role element/type supplied to the policy.
83          * 
84          * @return the peer role element/type, or an empty QName
85          */
86         const xmltooling::QName* getRole() const {
87             return &m_role;
88         }
89
90         /**
91          * Returns the TrustEngine supplied to the policy.
92          * 
93          * @return the supplied TrustEngine or NULL
94          */
95         const TrustEngine* getTrustEngine() const {
96             return m_trust;
97         }
98
99         /**
100          * Evaluates the rule against the given request and message,
101          * possibly populating issuer information in the policy object.
102          * 
103          * @param request           the protocol request
104          * @param message           the incoming message
105          * @return the identity of the message issuer, in one or more of two forms, or NULL
106          * 
107          * @throws BindingException thrown if the request/message do not meet the requirements of this rule
108          */
109         void evaluate(const GenericRequest& request, const xmltooling::XMLObject& message);
110
111         /**
112          * Gets the issuer of the message as determined by the registered policies.
113          * 
114          * @return issuer of the message as determined by the registered policies
115          */
116         const saml2::Issuer* getIssuer() const {
117             return m_issuer;
118         }
119
120         /**
121          * Gets the metadata for the role the issuer is operating in.
122          * 
123          * @return metadata for the role the issuer is operating in
124          */
125         const saml2md::RoleDescriptor* getIssuerMetadata() const {
126             return m_issuerRole;
127         }
128
129         /**
130          * Sets the issuer of the message as determined by external factors.
131          * The policy object takes ownership of the Issuer object.
132          * 
133          * @param issuer issuer of the message
134          */
135         void setIssuer(saml2::Issuer* issuer);
136         
137         /**
138          * Sets the metadata for the role the issuer is operating in.
139          * 
140          * @param issuerRole metadata for the role the issuer is operating in
141          */
142         void setIssuerMetadata(const saml2md::RoleDescriptor* issuerRole);
143
144     protected:
145         /**
146          * Returns true iff the two operands "match". Applications can override this method to
147          * support non-standard issuer matching for complex policies. 
148          * 
149          * <p>The default implementation does a basic comparison of the XML content, treating
150          * an unsupplied Format as "entityID".
151          * 
152          * @param issuer1   the first Issuer to match
153          * @param issuer2   the second Issuer to match
154          * @return  true iff the operands match
155          */
156         virtual bool issuerMatches(const saml2::Issuer* issuer1, const saml2::Issuer* issuer2) const;
157
158     private:
159         saml2::Issuer* m_issuer;
160         const saml2md::RoleDescriptor* m_issuerRole;
161         
162         std::vector<const SecurityPolicyRule*> m_rules;
163         const saml2md::MetadataProvider* m_metadata;
164         xmltooling::QName m_role;
165         const TrustEngine* m_trust;
166     };
167
168 };
169
170 #if defined (_MSC_VER)
171     #pragma warning( pop )
172 #endif
173
174 #endif /* __saml_secpol_h__ */