Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
policy_document.h
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
25namespace google {
26namespace cloud {
27namespace storage {
28GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
29/**
30 * Define a condition for a policy document.
31 */
33 public:
34 PolicyDocumentCondition() = default;
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
88inline bool operator==(PolicyDocumentCondition const& lhs,
89 PolicyDocumentCondition const& rhs) {
90 return lhs.elements() == rhs.elements();
91}
92
93inline bool operator<(PolicyDocumentCondition const& lhs,
94 PolicyDocumentCondition const& rhs) {
95 return lhs.elements() < rhs.elements();
96}
97
98inline bool operator!=(PolicyDocumentCondition const& lhs,
99 PolicyDocumentCondition const& rhs) {
100 return std::rel_ops::operator!=(lhs, rhs);
101}
102
103inline bool operator>(PolicyDocumentCondition const& lhs,
104 PolicyDocumentCondition const& rhs) {
105 return std::rel_ops::operator>(lhs, rhs);
106}
107
108inline bool operator<=(PolicyDocumentCondition const& lhs,
109 PolicyDocumentCondition const& rhs) {
110 return std::rel_ops::operator<=(lhs, rhs);
111}
112
113inline bool operator>=(PolicyDocumentCondition const& lhs,
114 PolicyDocumentCondition const& rhs) {
115 return std::rel_ops::operator>=(lhs, rhs);
116}
117
118std::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 */
130struct PolicyDocument {
131 std::chrono::system_clock::time_point expiration;
133};
134
135std::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 */
147struct PolicyDocumentV4 {
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
167std::ostream& operator<<(std::ostream& os, PolicyDocumentV4 const& rhs);
168
169/**
170 * Define a policy document result.
171 *
172 * `access_id` is 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
183std::ostream& operator<<(std::ostream& os, PolicyDocumentResult const& rhs);
184
185/**
186 * Define a policy document result V4.
187 *
188 * `access_id` is 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
203std::string FormatDateForForm(PolicyDocumentV4Result const&);
204
205std::ostream& operator<<(std::ostream& os, PolicyDocumentV4Result const& rhs);
206
207GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
208} // namespace storage
209} // namespace cloud
210} // namespace google
211
212#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_POLICY_DOCUMENT_H
Define a condition for a policy document.
Definition: policy_document.h:32
PolicyDocumentCondition(std::vector< std::string > elements)
Definition: policy_document.h:36
static std::vector< std::string > ExactMatchObject(std::string const &field, std::string const &value)
Creates an exact match condition, but in object form syntax.
Definition: policy_document.h:57
static std::vector< std::string > ContentLengthRange(std::int32_t min_range, std::int32_t max_range)
Creates an exact match condition, in the list form syntax.
Definition: policy_document.h:74
static std::vector< std::string > StartsWith(std::string const &field, std::string const &value)
Creates an exact match condition, in the list form syntax.
Definition: policy_document.h:65
std::vector< std::string > const & elements() const
Definition: policy_document.h:39
static std::vector< std::string > ExactMatch(std::string const &field, std::string const &value)
Creates an exact match condition, in the list form syntax.
Definition: policy_document.h:47
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
bool operator<(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:93
bool operator>(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:103
bool operator>=(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:113
std::string FormatDateForForm(PolicyDocumentV4Result const &)
Format the current date in the format expected by a POST form.
bool operator!=(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:98
bool operator<=(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:108
bool operator==(PolicyDocumentCondition const &lhs, PolicyDocumentCondition const &rhs)
Definition: policy_document.h:88
Define a policy document result.
Definition: policy_document.h:176
std::string access_id
Definition: policy_document.h:177
std::string signature
Definition: policy_document.h:180
std::string policy
Definition: policy_document.h:179
std::chrono::system_clock::time_point expiration
Definition: policy_document.h:178
Define a policy document result V4.
Definition: policy_document.h:192
std::string policy
Definition: policy_document.h:196
std::string access_id
Definition: policy_document.h:194
std::map< std::string, std::string > required_form_fields
Definition: policy_document.h:199
std::string signing_algorithm
Definition: policy_document.h:198
std::string url
Definition: policy_document.h:193
std::chrono::system_clock::time_point expiration
Definition: policy_document.h:195
std::string signature
Definition: policy_document.h:197
Define a policy document V4.
Definition: policy_document.h:147
std::vector< PolicyDocumentCondition > conditions
Definition: policy_document.h:164
PolicyDocumentV4(std::string bucket, std::string object, std::chrono::seconds expiration, std::chrono::system_clock::time_point timestamp=std::chrono::system_clock::now(), std::vector< PolicyDocumentCondition > conditions={})
Definition: policy_document.h:149
std::string bucket
Definition: policy_document.h:160
std::chrono::system_clock::time_point timestamp
Definition: policy_document.h:163
std::chrono::seconds expiration
Definition: policy_document.h:162
std::string object
Definition: policy_document.h:161
Define a policy document.
Definition: policy_document.h:130
std::chrono::system_clock::time_point expiration
Definition: policy_document.h:131
std::vector< PolicyDocumentCondition > conditions
Definition: policy_document.h:132