Google Cloud Storage C++ Client  1.42.0
A C++ Client Library for Google Cloud Storage
policy_document.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_POLICY_DOCUMENT_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_POLICY_DOCUMENT_H
17 
18 #include "google/cloud/storage/version.h"
19 #include <chrono>
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace google {
26 namespace cloud {
27 namespace storage {
29 /**
30  * Define a condition for a policy document.
31  */
33  public:
35  // NOLINTNEXTLINE(google-explicit-constructor)
36  PolicyDocumentCondition(std::vector<std::string> elements)
37  : elements_(std::move(elements)) {}
38 
39  std::vector<std::string> const& elements() const { return elements_; }
40 
41  //@{
42  /**
43  * @name Creates different types of PolicyDocumentCondition matchers.
44  */
45 
46  /// Creates an exact match condition, in the list form syntax.
47  static std::vector<std::string> ExactMatch(std::string const& field,
48  std::string const& value) {
49  std::vector<std::string> result;
50  result.emplace_back("eq");
51  result.emplace_back(std::string("$") + field);
52  result.emplace_back(value);
53  return result;
54  }
55 
56  /// Creates an exact match condition, but in object form syntax.
57  static std::vector<std::string> ExactMatchObject(std::string const& field,
58  std::string const& value) {
59  std::vector<std::string> result;
60  result.emplace_back(field);
61  result.emplace_back(value);
62  return result;
63  }
64 
65  static std::vector<std::string> StartsWith(std::string const& field,
66  std::string const& value) {
67  std::vector<std::string> result;
68  result.emplace_back("starts-with");
69  result.emplace_back(std::string("$") + field);
70  result.emplace_back(value);
71  return result;
72  }
73 
74  static std::vector<std::string> ContentLengthRange(std::int32_t min_range,
75  std::int32_t max_range) {
76  std::vector<std::string> result;
77  result.emplace_back("content-length-range");
78  result.emplace_back(std::to_string(min_range));
79  result.emplace_back(std::to_string(max_range));
80  return result;
81  }
82  //@}
83 
84  private:
85  std::vector<std::string> elements_;
86 };
87 
88 inline bool operator==(PolicyDocumentCondition const& lhs,
89  PolicyDocumentCondition const& rhs) {
90  return lhs.elements() == rhs.elements();
91 }
92 
93 inline bool operator<(PolicyDocumentCondition const& lhs,
94  PolicyDocumentCondition const& rhs) {
95  return lhs.elements() < rhs.elements();
96 }
97 
98 inline bool operator!=(PolicyDocumentCondition const& lhs,
99  PolicyDocumentCondition const& rhs) {
100  return std::rel_ops::operator!=(lhs, rhs);
101 }
102 
103 inline bool operator>(PolicyDocumentCondition const& lhs,
104  PolicyDocumentCondition const& rhs) {
105  return std::rel_ops::operator>(lhs, rhs);
106 }
107 
108 inline bool operator<=(PolicyDocumentCondition const& lhs,
109  PolicyDocumentCondition const& rhs) {
110  return std::rel_ops::operator<=(lhs, rhs);
111 }
112 
113 inline bool operator>=(PolicyDocumentCondition const& lhs,
114  PolicyDocumentCondition const& rhs) {
115  return std::rel_ops::operator>=(lhs, rhs);
116 }
117 
118 std::ostream& operator<<(std::ostream& os, PolicyDocumentCondition const& rhs);
119 
120 /**
121  * Define a policy document.
122  *
123  * Policy documents allow HTML forms to restrict uploads based on certain
124  * conditions. If the policy document is expired or the conditions are not
125  * satisfied, submitting the form will not succeed.
126  *
127  * @see https://cloud.google.com/storage/docs/xml-api/post-object#policydocument
128  * for general information on policy documents in Google Cloud Storage.
129  */
131  std::chrono::system_clock::time_point expiration;
133 };
134 
135 std::ostream& operator<<(std::ostream& os, PolicyDocument const& rhs);
136 
137 /**
138  * Define a policy document V4.
139  *
140  * Policy documents allow HTML forms to restrict uploads based on certain
141  * conditions. If the policy document is expired or the conditions are not
142  * satisfied, submitting the form will not succeed.
143  *
144  * @see https://cloud.google.com/storage/docs/xml-api/post-object#policydocument
145  * for general information on policy documents in Google Cloud Storage.
146  */
148  PolicyDocumentV4() = default;
149  PolicyDocumentV4(std::string bucket, std::string object,
150  std::chrono::seconds expiration,
151  std::chrono::system_clock::time_point timestamp =
152  std::chrono::system_clock::now(),
153  std::vector<PolicyDocumentCondition> conditions = {})
154  : bucket(std::move(bucket)),
155  object(std::move(object)),
156  expiration(std::move(expiration)),
157  timestamp(std::move(timestamp)),
158  conditions(std::move(conditions)) {}
159 
160  std::string bucket;
161  std::string object;
162  std::chrono::seconds expiration;
163  std::chrono::system_clock::time_point timestamp;
165 };
166 
167 std::ostream& operator<<(std::ostream& os, PolicyDocumentV4 const& rhs);
168 
169 /**
170  * Define a policy document result.
171  *
172  * `access_id` is the the Cloud Storage email form of the client ID. `policy`
173  * is the base64 encoded form of the plain-text policy document and `signature`
174  * is the signed policy document.
175  */
177  std::string access_id;
178  std::chrono::system_clock::time_point expiration;
179  std::string policy;
180  std::string signature;
181 };
182 
183 std::ostream& operator<<(std::ostream& os, PolicyDocumentResult const& rhs);
184 
185 /**
186  * Define a policy document result V4.
187  *
188  * `access_id` is the the Cloud Storage email form of the client ID. `policy`
189  * is the base64 encoded form of the plain-text policy document and `signature`
190  * is the signed policy document.
191  */
193  std::string url;
194  std::string access_id;
195  std::chrono::system_clock::time_point expiration;
196  std::string policy;
197  std::string signature;
198  std::string signing_algorithm;
199  std::map<std::string, std::string> required_form_fields;
200 };
201 
202 /// Format the current date in the format expected by a POST form
204 
205 std::ostream& operator<<(std::ostream& os, PolicyDocumentV4Result const& rhs);
206 
208 } // namespace storage
209 } // namespace cloud
210 } // namespace google
211 
212 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_POLICY_DOCUMENT_H